diff --git a/assets/maps/mainmap.tmx b/assets/maps/mainmap.tmx index cd364a6..b630c08 100644 --- a/assets/maps/mainmap.tmx +++ b/assets/maps/mainmap.tmx @@ -1,5 +1,5 @@ - + @@ -26,7 +26,11 @@ - + + + + + diff --git a/core/src/main/java/io/github/com/quillraven/GdxGame.java b/core/src/main/java/io/github/com/quillraven/GdxGame.java index 1cd8a67..e1c7dcd 100644 --- a/core/src/main/java/io/github/com/quillraven/GdxGame.java +++ b/core/src/main/java/io/github/com/quillraven/GdxGame.java @@ -3,6 +3,7 @@ package io.github.com.quillraven; import com.badlogic.gdx.Application; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.InputMultiplexer; import com.badlogic.gdx.Screen; import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; import com.badlogic.gdx.graphics.GL20; @@ -28,12 +29,15 @@ public class GdxGame extends Game { private OrthographicCamera camera; private Viewport viewport; private GLProfiler glProfiler; + private InputMultiplexer inputMultiplexer; private final HashMap, Screen> screenCache = new HashMap<>(); @Override public void create() { Gdx.app.setLogLevel(Application.LOG_DEBUG); + inputMultiplexer = new InputMultiplexer(); + Gdx.input.setInputProcessor(inputMultiplexer); batch = new SpriteBatch(); assetService = new AssetService(new InternalFileHandleResolver()); @@ -108,4 +112,8 @@ public class GdxGame extends Game { public Viewport getViewport() { return viewport; } + + public InputMultiplexer getInputMultiplexer() { + return inputMultiplexer; + } } diff --git a/core/src/main/java/io/github/com/quillraven/component/Controller.java b/core/src/main/java/io/github/com/quillraven/component/Controller.java new file mode 100644 index 0000000..fe7b332 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/component/Controller.java @@ -0,0 +1,6 @@ +package io.github.com.quillraven.component; + +import com.badlogic.ashley.core.Component; + +public class Controller implements Component { +} diff --git a/core/src/main/java/io/github/com/quillraven/input/Command.java b/core/src/main/java/io/github/com/quillraven/input/Command.java new file mode 100644 index 0000000..40ef8e3 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/input/Command.java @@ -0,0 +1,8 @@ +package io.github.com.quillraven.input; + +public enum Command { + LEFT, + RIGHT, + DOWN, + UP +} diff --git a/core/src/main/java/io/github/com/quillraven/input/ControllerState.java b/core/src/main/java/io/github/com/quillraven/input/ControllerState.java new file mode 100644 index 0000000..000db78 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/input/ControllerState.java @@ -0,0 +1,7 @@ +package io.github.com.quillraven.input; + +public interface ControllerState { + void keyDown(Command command); + + void keyUp(Command command); +} diff --git a/core/src/main/java/io/github/com/quillraven/input/GameControllerState.java b/core/src/main/java/io/github/com/quillraven/input/GameControllerState.java new file mode 100644 index 0000000..01aefb8 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/input/GameControllerState.java @@ -0,0 +1,92 @@ +package io.github.com.quillraven.input; + +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.utils.ImmutableArray; +import io.github.com.quillraven.component.Move; + +public class GameControllerState implements ControllerState { + + private final ImmutableArray controllerEntities; + + public GameControllerState(ImmutableArray controllerEntities) { + this.controllerEntities = controllerEntities; + } + + @Override + public void keyDown(Command command) { + switch (command) { + case UP: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().y += 1f; + } + } + break; + case DOWN: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().y -= 1f; + } + } + break; + case LEFT: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().x -= 1f; + } + } + break; + case RIGHT: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().x += 1f; + } + } + break; + default: + } + } + + @Override + public void keyUp(Command command) { + switch (command) { + case UP: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().y -= 1f; + } + } + break; + case DOWN: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().y += 1f; + } + } + break; + case LEFT: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().x += 1f; + } + } + break; + case RIGHT: + for (Entity entity : controllerEntities) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().x -= 1f; + } + } + break; + default: + } + } +} diff --git a/core/src/main/java/io/github/com/quillraven/input/KeyboardController.java b/core/src/main/java/io/github/com/quillraven/input/KeyboardController.java new file mode 100644 index 0000000..84bca73 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/input/KeyboardController.java @@ -0,0 +1,52 @@ +package io.github.com.quillraven.input; + +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; + +import java.util.Map; + +public class KeyboardController extends InputAdapter { + private static final Map KEY_MAPPING = Map.ofEntries( + Map.entry(Input.Keys.W, Command.UP), + Map.entry(Input.Keys.S, Command.DOWN), + Map.entry(Input.Keys.A, Command.LEFT), + Map.entry(Input.Keys.D, Command.RIGHT) + ); + + private final boolean[] commandState; + private ControllerState activeState; + + public KeyboardController(ControllerState initialState) { + this.commandState = new boolean[Command.values().length]; + setActiveState(initialState); + } + + public void setActiveState(ControllerState controllerState) { + for (Command command : Command.values()) { + this.commandState[command.ordinal()] = false; + } + this.activeState = controllerState; + } + + @Override + public boolean keyDown(int keycode) { + Command command = KEY_MAPPING.get(keycode); + if (command == null) return false; + + this.commandState[command.ordinal()] = true; + this.activeState.keyDown(command); + return true; + } + + @Override + public boolean keyUp(int keycode) { + Command command = KEY_MAPPING.get(keycode); + if (command == null) return false; + // if a button was not pressed before, ignore it + if (!this.commandState[command.ordinal()]) return false; + + this.commandState[command.ordinal()] = false; + this.activeState.keyUp(command); + return true; + } +} diff --git a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java index af5056d..eebe7d5 100644 --- a/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java +++ b/core/src/main/java/io/github/com/quillraven/screen/GameScreen.java @@ -1,7 +1,10 @@ package io.github.com.quillraven.screen; 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.ScreenAdapter; import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.math.Vector2; @@ -9,6 +12,9 @@ import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.utils.Disposable; import io.github.com.quillraven.GdxGame; import io.github.com.quillraven.asset.MapAsset; +import io.github.com.quillraven.component.Controller; +import io.github.com.quillraven.input.GameControllerState; +import io.github.com.quillraven.input.KeyboardController; import io.github.com.quillraven.system.AnimationSystem; import io.github.com.quillraven.system.FacingSystem; import io.github.com.quillraven.system.FsmSystem; @@ -16,19 +22,20 @@ import io.github.com.quillraven.system.PhysicDebugRenderSystem; import io.github.com.quillraven.system.PhysicMoveSystem; import io.github.com.quillraven.system.PhysicSystem; import io.github.com.quillraven.system.RenderSystem; -import io.github.com.quillraven.system.TestSystem; import io.github.com.quillraven.tiled.TiledAshleySpawner; import io.github.com.quillraven.tiled.TiledService; import java.util.function.Consumer; public class GameScreen extends ScreenAdapter { + private final GdxGame game; private final TiledService tiledService; private final Engine engine; private final TiledAshleySpawner tiledAshleySpawner; private final World physicWorld; public GameScreen(GdxGame game) { + this.game = game; this.tiledService = new TiledService(game.getAssetService()); this.physicWorld = new World(Vector2.Zero, true); this.physicWorld.setAutoClearForces(false); @@ -42,12 +49,16 @@ public class GameScreen extends ScreenAdapter { this.engine.addSystem(new FsmSystem()); this.engine.addSystem(new AnimationSystem(game.getAssetService())); this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera())); - this.engine.addSystem(new TestSystem(this.tiledService)); this.engine.addSystem(new PhysicDebugRenderSystem(this.physicWorld, game.getCamera())); } @Override public void show() { + this.game.getInputMultiplexer().clear(); + ImmutableArray controllerEntities = this.engine.getEntitiesFor(Family.all(Controller.class).get()); + GameControllerState gameControllerState = new GameControllerState(controllerEntities); + this.game.getInputMultiplexer().addProcessor(new KeyboardController(gameControllerState)); + Consumer renderConsumer = this.engine.getSystem(RenderSystem.class)::setMap; Consumer ashleySpawnerConsumer = this.tiledAshleySpawner::loadMapObjects; this.tiledService.setMapChangeConsumer(renderConsumer.andThen(ashleySpawnerConsumer)); diff --git a/core/src/main/java/io/github/com/quillraven/system/TestSystem.java b/core/src/main/java/io/github/com/quillraven/system/TestSystem.java deleted file mode 100644 index 55452fd..0000000 --- a/core/src/main/java/io/github/com/quillraven/system/TestSystem.java +++ /dev/null @@ -1,49 +0,0 @@ -package io.github.com.quillraven.system; - -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.Gdx; -import com.badlogic.gdx.Input; -import io.github.com.quillraven.component.Move; -import io.github.com.quillraven.tiled.TiledService; - -public class TestSystem extends EntitySystem { - private final TiledService tiledService; - - public TestSystem(TiledService tiledService) { - super(); - this.tiledService = tiledService; - } - - @Override - public void update(float deltaTime) { - if (Gdx.input.isKeyJustPressed(Input.Keys.W)) { - ImmutableArray entities = getEngine().getEntitiesFor(Family.all(Move.class).get()); - for (Entity entity : entities) { - Move.MAPPER.get(entity).getDirection().set(0f, 1f); - } - } else if (Gdx.input.isKeyJustPressed(Input.Keys.S)) { - ImmutableArray entities = getEngine().getEntitiesFor(Family.all(Move.class).get()); - for (Entity entity : entities) { - Move.MAPPER.get(entity).getDirection().set(0f, -1f); - } - } else if (Gdx.input.isKeyJustPressed(Input.Keys.A)) { - ImmutableArray entities = getEngine().getEntitiesFor(Family.all(Move.class).get()); - for (Entity entity : entities) { - Move.MAPPER.get(entity).getDirection().set(-1f, 0f); - } - } else if (Gdx.input.isKeyJustPressed(Input.Keys.D)) { - ImmutableArray entities = getEngine().getEntitiesFor(Family.all(Move.class).get()); - for (Entity entity : entities) { - Move.MAPPER.get(entity).getDirection().set(1f, 0f); - } - } else if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) { - ImmutableArray entities = getEngine().getEntitiesFor(Family.all(Move.class).get()); - for (Entity entity : entities) { - Move.MAPPER.get(entity).getDirection().setZero(); - } - } - } -} diff --git a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java index 85bd879..70a2fa6 100644 --- a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java +++ b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java @@ -26,6 +26,7 @@ import io.github.com.quillraven.GdxGame; import io.github.com.quillraven.asset.AtlasAsset; import io.github.com.quillraven.component.Animation2D; import io.github.com.quillraven.component.Animation2D.AnimationType; +import io.github.com.quillraven.component.Controller; import io.github.com.quillraven.component.Facing; import io.github.com.quillraven.component.Facing.FacingDirection; import io.github.com.quillraven.component.Fsm; @@ -127,6 +128,7 @@ public class TiledAshleySpawner { entity); addEntityAnimation(tile, entity); addEntityMove(tile, entity); + addEntityController(tileMapObject, entity); entity.add(new Facing(FacingDirection.DOWN)); entity.add(new Fsm(entity)); entity.add(new Graphic(textureRegion, Color.WHITE.cpy())); @@ -134,6 +136,13 @@ public class TiledAshleySpawner { this.engine.addEntity(entity); } + private void addEntityController(TiledMapTileMapObject tileMapObject, Entity entity) { + boolean controller = tileMapObject.getProperties().get("controller", false, Boolean.class); + if (!controller) return; + + entity.add(new Controller()); + } + private void addEntityMove(TiledMapTile tile, Entity entity) { float speed = tile.getProperties().get("speed", 0f, Float.class); if (speed == 0f) return;