Box2D implemented. Viewport also is a lot smaller

This commit is contained in:
Allexit 2015-04-09 21:16:49 +03:00
parent 91135acfce
commit 822d2fa7bc
5 changed files with 100 additions and 33 deletions

View File

@ -7,25 +7,40 @@ import com.badlogic.ashley.core.Family;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.systems.RenderingSystem;
import com.saltosion.gladiator.util.GlobalStrings;
import com.saltosion.gladiator.util.Global;
import com.saltosion.gladiator.util.SpriteLoader;
public class GladiatorBrawler extends ApplicationAdapter {
private Engine engine;
private World world;
private float physics_accumulator = 0f;
private Entity player;
@Override
public void create () {
// Initializing Physics
world = new World(new Vector2(0, -10), true);
// Initialize the Engine
engine = new Engine();
engine.addSystem(new RenderingSystem());
engine.addSystem(new RenderingSystem(world));
engine.addEntityListener(Family.getFor(CRenderedObject.class, CPhysics.class),
new EntityListener() {
@ -43,6 +58,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
}
});
// Initialize player
initializePlayer();
@ -52,6 +68,51 @@ public class GladiatorBrawler extends ApplicationAdapter {
@Override
public void render () {
engine.update(Gdx.graphics.getDeltaTime());
physicsStep(Gdx.graphics.getDeltaTime());
}
private void physicsStep(float deltaTime) {
float frameTime = Math.max(deltaTime, 0.25f);
physics_accumulator += frameTime;
if (physics_accumulator >= Global.PHYSICS_TIMESTEP) {
world.step(Global.PHYSICS_TIMESTEP, 6, 2);
physics_accumulator -= Global.PHYSICS_TIMESTEP;
}
}
public void initializePlayer() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(5, 5);
Body body = world.createBody(bodyDef);
PolygonShape box = new PolygonShape();
box.setAsBox(2, 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f;
Fixture fixture = body.createFixture(fixtureDef);
box.dispose();
player = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite player1 = SpriteLoader.loadSprite(Global.PLAYERIMG, 0, 0, 64, 64);
Sprite player2 = SpriteLoader.loadSprite(Global.PLAYERIMG, 1, 0, 64, 64);
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
renderedObject.addSequence("Idle", sequence);
renderedObject.setCurrentSequence("Idle");
player.add(renderedObject);
player.add(new CPhysics());
player.getComponent(CPhysics.class).position.set(body.getPosition());
engine.addEntity(player);
}
@Override
@ -59,22 +120,6 @@ public class GladiatorBrawler extends ApplicationAdapter {
super.resize(width, height);
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
float aspectratio = ((float)width)/((float)height);
rs.setViewport((int)(rs.VPHEIGHT_CONST*aspectratio), rs.VPHEIGHT_CONST);
}
public void initializePlayer() {
player = new Entity();
CRenderedObject renderedObject = new CRenderedObject();
Sprite player1 = SpriteLoader.loadSprite(GlobalStrings.PLAYERIMG, 0, 0, 64, 64);
Sprite player2 = SpriteLoader.loadSprite(GlobalStrings.PLAYERIMG, 1, 0, 64, 64);
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
renderedObject.addSequence("Idle", sequence);
renderedObject.setCurrentSequence("Idle");
player.add(renderedObject);
player.add(new CPhysics());
player.getComponent(CPhysics.class).position.set(50, 50);
engine.addEntity(player);
rs.setViewport((int)(Global.VPHEIGHT_CONST*aspectratio), Global.VPHEIGHT_CONST);
}
}

View File

@ -9,11 +9,16 @@ import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.saltosion.gladiator.SpriteSequence;
import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.components.CRenderedObject;
import com.saltosion.gladiator.util.Global;
import com.saltosion.gladiator.util.SpriteLoader;
public class RenderingSystem extends EntitySystem {
@ -23,15 +28,23 @@ public class RenderingSystem extends EntitySystem {
private SpriteBatch batch;
private OrthographicCamera camera;
public final int VPHEIGHT_CONST = 252;
private Box2DDebugRenderer debugRenderer;
private World world;
public RenderingSystem(World world) {
this.world = world;
}
@Override
public void addedToEngine(Engine engine) {
debugRenderer = new Box2DDebugRenderer();
updateEntities(engine);
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(false, 448, 252);
camera.setToOrtho(false, 1, 1);
}
public void setViewport(int width, int height) {
@ -41,11 +54,10 @@ public class RenderingSystem extends EntitySystem {
@Override
public void update(float deltaTime) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
for (int i=0; i<entities.size(); i++) {
CRenderedObject renderedObject = rom.get(entities.get(i));
SpriteSequence currSequence = renderedObject.getSequence(renderedObject.getCurrentSequence());
@ -54,13 +66,18 @@ public class RenderingSystem extends EntitySystem {
CPhysics physics = pm.get(entities.get(i));
batch.draw(currSprite, physics.position.x, physics.position.y);
int spriteHeight = currSprite.getRegionHeight();
int spriteWidth = currSprite.getRegionWidth();
currSprite.setPosition(physics.position.x-spriteWidth/2, physics.position.y-spriteHeight/2);
currSprite.draw(batch);
float nextFrame = renderedObject.getCurrentFrame() + deltaTime*currSequence.getPlayspeed();
renderedObject.setCurrentFrame(nextFrame%currSequence.frameCount());
}
}
batch.end();
debugRenderer.render(world, camera.combined);
}
public void updateEntities(Engine engine) {

View File

@ -1,10 +1,13 @@
package com.saltosion.gladiator.util;
public class GlobalStrings {
public class Global {
public static final String STATICPLAYER = "STATICPLAYER";
public static final String PLAYERIMG = "PLAYERIMG";
public static final String GAME_NAME = "Gladiator Brawl";
public static final int VPHEIGHT_CONST = 14;
public static final float PHYSICS_TIMESTEP = 1/45f;
}

View File

@ -12,8 +12,8 @@ public class SpriteLoader {
public static HashMap<String, Texture> textures = new HashMap<String, Texture>();
static {
loadTexture(GlobalStrings.STATICPLAYER, "sprites/staticplayer.png");
loadTexture(GlobalStrings.PLAYERIMG, "sprites/player/player.png");
loadTexture(Global.STATICPLAYER, "sprites/staticplayer.png");
loadTexture(Global.PLAYERIMG, "sprites/player/player.png");
}
/**
@ -27,7 +27,9 @@ public class SpriteLoader {
*/
public static Sprite loadSprite(String texKey, int x, int y, int width, int height) {
TextureRegion tr = new TextureRegion(textures.get(texKey), x*width, y*height, width, height);
return new Sprite(tr);
Sprite s = new Sprite(tr);
s.setScale(1/16f);
return s;
}
/**
@ -36,7 +38,7 @@ public class SpriteLoader {
* @return
*/
public static Sprite loadSprite(String texKey) {
return new Sprite(textures.get(texKey));
return loadSprite(texKey, 0, 0, textures.get(texKey).getWidth(), textures.get(texKey).getHeight());
}
/**

View File

@ -3,12 +3,12 @@ package com.saltosion.gladiator.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.saltosion.gladiator.GladiatorBrawler;
import com.saltosion.gladiator.util.GlobalStrings;
import com.saltosion.gladiator.util.Global;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = GlobalStrings.GAME_NAME;
config.title = Global.GAME_NAME;
config.width = 1280;
config.height = 720;
new LwjglApplication(new GladiatorBrawler(), config);