diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index ae62529..8f189ab 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -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); } } diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index 40f4edc..8facffc 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -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 textures = new HashMap(); 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()); } /** diff --git a/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java b/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java index b20aa87..101be81 100644 --- a/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java +++ b/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java @@ -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);