diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 7e820c3..444d699 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -3,7 +3,6 @@ package com.saltosion.gladiator; import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.EntityListener; -import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.Family; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; @@ -18,21 +17,26 @@ 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.input.InputHandler; +import com.saltosion.gladiator.systems.MovementSystem; import com.saltosion.gladiator.systems.RenderingSystem; -import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.AppUtil; +import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.SpriteLoader; +import com.saltosion.gladiator.util.SpriteSequence; public class GladiatorBrawler extends ApplicationAdapter { private Engine engine; private World world; - private float physics_accumulator = 0f; + private InputHandler inputHandler; + private float physics_accumulator = 0f; private Entity player; @Override - public void create () { + public void create () { // Initializing Physics world = new World(new Vector2(0, -10), true); @@ -42,19 +46,24 @@ public class GladiatorBrawler extends ApplicationAdapter { engine = new Engine(); engine.addSystem(new RenderingSystem(world)); + engine.addSystem(new MovementSystem()); engine.addEntityListener(Family.getFor(), new EntityListener() { @Override public void entityRemoved(Entity entity) { RenderingSystem rs = engine.getSystem(RenderingSystem.class); - rs.updateEntities(engine); + rs.updateEntities(engine); + MovementSystem ms = engine.getSystem(MovementSystem.class); + ms.updateEntities(engine); } @Override public void entityAdded(Entity entity) { RenderingSystem rs = engine.getSystem(RenderingSystem.class); rs.updateEntities(engine); + MovementSystem ms = engine.getSystem(MovementSystem.class); + ms.updateEntities(engine); } }); @@ -65,6 +74,10 @@ public class GladiatorBrawler extends ApplicationAdapter { initializePlayer(); initializeTerrain(); + // Initialize input + + inputHandler = new InputHandler(); + Gdx.input.setInputProcessor(inputHandler); } @Override @@ -76,9 +89,9 @@ public class GladiatorBrawler extends ApplicationAdapter { 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; + if (physics_accumulator >= AppUtil.PHYSICS_TIMESTEP) { + world.step(AppUtil.PHYSICS_TIMESTEP, 6, 2); + physics_accumulator -= AppUtil.PHYSICS_TIMESTEP; } } @@ -86,24 +99,35 @@ public class GladiatorBrawler extends ApplicationAdapter { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(5, 5); + bodyDef.fixedRotation = true; Body body = world.createBody(bodyDef); + + PolygonShape bottomBox = new PolygonShape(); + bottomBox.setAsBox(1.95f, .5f, new Vector2(0, -1.5f), 0); - PolygonShape box = new PolygonShape(); - box.setAsBox(2, 2); + FixtureDef bottomDef = new FixtureDef(); + bottomDef.shape = bottomBox; + bottomDef.density = 75f; + bottomDef.friction = 7f; + Fixture bottom = body.createFixture(bottomDef); - FixtureDef fixtureDef = new FixtureDef(); - fixtureDef.shape = box; - fixtureDef.density = 0.5f; - fixtureDef.friction = 0.4f; - Fixture fixture = body.createFixture(fixtureDef); + PolygonShape topBox = new PolygonShape(); + topBox.setAsBox(2, 1.5f, new Vector2(0, .5f), 0); - box.dispose(); + FixtureDef topDef = new FixtureDef(); + topDef.shape = topBox; + topDef.density = 75f; + topDef.friction = 0f; + Fixture top = body.createFixture(topDef); + + bottomBox.dispose(); + topBox.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); + Sprite player1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 64, 64); + Sprite player2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 64, 64); SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2); renderedObject.addSequence("Idle", sequence); renderedObject.setCurrentSequence("Idle"); @@ -112,6 +136,8 @@ public class GladiatorBrawler extends ApplicationAdapter { player.getComponent(CPhysics.class).body = body; engine.addEntity(player); + + AppUtil.player = player; } public void initializeTerrain() { @@ -128,6 +154,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)(Global.VPHEIGHT_CONST*aspectratio), Global.VPHEIGHT_CONST); + rs.setViewport((int)(AppUtil.VPHEIGHT_CONST*aspectratio), AppUtil.VPHEIGHT_CONST); } } diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index aff27f6..6104008 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -7,6 +7,9 @@ import com.badlogic.gdx.physics.box2d.Body; public class CPhysics extends Component { public Body body; + public Vector2 velocity = new Vector2(0, 0); - + public boolean movingLeft = false; + public boolean movingRight = false; + } diff --git a/core/src/com/saltosion/gladiator/components/CRenderedObject.java b/core/src/com/saltosion/gladiator/components/CRenderedObject.java index c5db95e..d9c9d08 100644 --- a/core/src/com/saltosion/gladiator/components/CRenderedObject.java +++ b/core/src/com/saltosion/gladiator/components/CRenderedObject.java @@ -4,7 +4,7 @@ import java.util.HashMap; import com.badlogic.ashley.core.Component; import com.badlogic.gdx.graphics.g2d.Sprite; -import com.saltosion.gladiator.SpriteSequence; +import com.saltosion.gladiator.util.SpriteSequence; public class CRenderedObject extends Component { private HashMap spritesequences = new HashMap(); diff --git a/core/src/com/saltosion/gladiator/input/IRMoveLeft.java b/core/src/com/saltosion/gladiator/input/IRMoveLeft.java new file mode 100644 index 0000000..790fa7f --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/IRMoveLeft.java @@ -0,0 +1,23 @@ +package com.saltosion.gladiator.input; + +import com.badlogic.ashley.core.Family; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.util.AppUtil; + +public class IRMoveLeft implements InputReceiver { + + @Override + public boolean pressed() { + CPhysics physics = AppUtil.player.getComponent(CPhysics.class); + physics.movingLeft = true; + return true; + } + + @Override + public boolean released() { + CPhysics physics = AppUtil.player.getComponent(CPhysics.class); + physics.movingLeft = false; + return false; + } +} diff --git a/core/src/com/saltosion/gladiator/input/IRMoveRight.java b/core/src/com/saltosion/gladiator/input/IRMoveRight.java new file mode 100644 index 0000000..2ac1029 --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/IRMoveRight.java @@ -0,0 +1,23 @@ +package com.saltosion.gladiator.input; + +import com.badlogic.ashley.core.Family; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.util.AppUtil; + +public class IRMoveRight implements InputReceiver { + + @Override + public boolean pressed() { + CPhysics physics = AppUtil.player.getComponent(CPhysics.class); + physics.movingRight = true; + return true; + } + + @Override + public boolean released() { + CPhysics physics = AppUtil.player.getComponent(CPhysics.class); + physics.movingRight = false; + return false; + } +} diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java new file mode 100644 index 0000000..60f4816 --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -0,0 +1,66 @@ +package com.saltosion.gladiator.input; + +import java.util.HashMap; + +import com.badlogic.gdx.Input.Keys; +import com.badlogic.gdx.InputProcessor; +import com.saltosion.gladiator.util.Name; + +public class InputHandler implements InputProcessor { + + public HashMap keys = new HashMap(); + + public InputHandler() { + keys.put(Keys.A, Name.MOVE_LEFT); + keys.put(Keys.D, Name.MOVE_RIGHT); + } + + @Override + public boolean keyDown(int keycode) { + if (!keys.containsKey(keycode)) { + return false; + } + String actionName = keys.get(keycode); + return InputReceivers.getReceiver(actionName).pressed(); + } + + @Override + public boolean keyUp(int keycode) { + if (!keys.containsKey(keycode)) { + return false; + } + String actionName = keys.get(keycode); + return InputReceivers.getReceiver(actionName).released(); + } + + @Override + public boolean keyTyped(char character) { + return false; + } + + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + return false; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + return false; + } + + @Override + public boolean touchDragged(int screenX, int screenY, int pointer) { + return false; + } + + @Override + public boolean mouseMoved(int screenX, int screenY) { + return false; + } + + @Override + public boolean scrolled(int amount) { + return false; + } + +} diff --git a/core/src/com/saltosion/gladiator/input/InputReceiver.java b/core/src/com/saltosion/gladiator/input/InputReceiver.java new file mode 100644 index 0000000..13ce98c --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/InputReceiver.java @@ -0,0 +1,6 @@ +package com.saltosion.gladiator.input; + +public interface InputReceiver { + public boolean pressed(); + public boolean released(); +} diff --git a/core/src/com/saltosion/gladiator/input/InputReceivers.java b/core/src/com/saltosion/gladiator/input/InputReceivers.java new file mode 100644 index 0000000..260df8c --- /dev/null +++ b/core/src/com/saltosion/gladiator/input/InputReceivers.java @@ -0,0 +1,18 @@ +package com.saltosion.gladiator.input; + +import java.util.HashMap; + +import com.saltosion.gladiator.util.Name; + +public class InputReceivers { + public static HashMap inputreceivers = new HashMap(); + + static { + inputreceivers.put(Name.MOVE_LEFT, new IRMoveLeft()); + inputreceivers.put(Name.MOVE_RIGHT, new IRMoveRight()); + } + + public static InputReceiver getReceiver(String key) { + return inputreceivers.get(key); + } +} diff --git a/core/src/com/saltosion/gladiator/systems/MovementSystem.java b/core/src/com/saltosion/gladiator/systems/MovementSystem.java new file mode 100644 index 0000000..25a0a6c --- /dev/null +++ b/core/src/com/saltosion/gladiator/systems/MovementSystem.java @@ -0,0 +1,60 @@ +package com.saltosion.gladiator.systems; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Engine; +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.EntitySystem; +import com.badlogic.ashley.core.Family; +import com.badlogic.ashley.utils.ImmutableArray; +import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.components.CPhysics; + +public class MovementSystem extends EntitySystem { + + private ComponentMapper pm = ComponentMapper.getFor(CPhysics.class); + private ImmutableArray entities; + + private final Vector2 RIGHT = new Vector2(1, 0); + private final float MOVEMENT_SPEED = 850; + private final float MAX_MOVEMENT_SPEED = 10; + + private Vector2 newVel = new Vector2(0, 0); + private Vector2 bodyVel = new Vector2(0, 0); + + @Override + public void addedToEngine(Engine engine) { + updateEntities(engine); + } + + @Override + public void update(float deltaTime) { + for (int i=0; i textures = new HashMap(); static { - loadTexture(Global.STATICPLAYER, "sprites/staticplayer.png"); - loadTexture(Global.PLAYERIMG, "sprites/player/player.png"); + loadTexture(Name.STATICPLAYER, "sprites/staticplayer.png"); + loadTexture(Name.PLAYERIMG, "sprites/player/player.png"); } /** diff --git a/core/src/com/saltosion/gladiator/util/SpriteSequence.java b/core/src/com/saltosion/gladiator/util/SpriteSequence.java new file mode 100644 index 0000000..6d59a32 --- /dev/null +++ b/core/src/com/saltosion/gladiator/util/SpriteSequence.java @@ -0,0 +1,44 @@ +package com.saltosion.gladiator.util; + +import java.util.ArrayList; + +import com.badlogic.gdx.graphics.g2d.Sprite; + +public class SpriteSequence { + + private ArrayList sprites = new ArrayList(); + private float defaultPlayspeed = 1; + + /** + * A static single image. + * @param sprite + */ + public SpriteSequence(Sprite sprite) { + sprites.add(sprite); + defaultPlayspeed = 0; + } + + public SpriteSequence(float playspeed) { + this.defaultPlayspeed = playspeed; + if (sprites != null) { + this.sprites = sprites; + } + } + + public SpriteSequence addSprite(Sprite s) { + sprites.add(s); + return this; + } + + public Sprite getSprite(int index) { + return sprites.get(index); + } + + public float getPlayspeed() { + return defaultPlayspeed; + } + + public int frameCount() { + return sprites.size(); + } +} diff --git a/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java b/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java index 101be81..1a8942c 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.Global; +import com.saltosion.gladiator.util.Name; public class DesktopLauncher { public static void main (String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); - config.title = Global.GAME_NAME; + config.title = Name.GAME_NAME; config.width = 1280; config.height = 720; new LwjglApplication(new GladiatorBrawler(), config);