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 index fe7b332..5ea5e98 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Controller.java +++ b/core/src/main/java/io/github/com/quillraven/component/Controller.java @@ -1,6 +1,28 @@ package io.github.com.quillraven.component; import com.badlogic.ashley.core.Component; +import com.badlogic.ashley.core.ComponentMapper; +import io.github.com.quillraven.input.Command; + +import java.util.ArrayList; +import java.util.List; public class Controller implements Component { + public static final ComponentMapper MAPPER = ComponentMapper.getFor(Controller.class); + + private final List pressedCommands; + private final List releasedCommands; + + public Controller() { + this.pressedCommands = new ArrayList<>(); + this.releasedCommands = new ArrayList<>(); + } + + public List getPressedCommands() { + return pressedCommands; + } + + public List getReleasedCommands() { + return releasedCommands; + } } 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 index 2ce8e82..5348b41 100644 --- a/core/src/main/java/io/github/com/quillraven/input/Command.java +++ b/core/src/main/java/io/github/com/quillraven/input/Command.java @@ -5,5 +5,6 @@ public enum Command { RIGHT, DOWN, UP, - SELECT + SELECT, + CANCEL } 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 index ded8cba..0b3c73e 100644 --- a/core/src/main/java/io/github/com/quillraven/input/GameControllerState.java +++ b/core/src/main/java/io/github/com/quillraven/input/GameControllerState.java @@ -5,7 +5,6 @@ import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import io.github.com.quillraven.component.Controller; -import io.github.com.quillraven.component.Move; public class GameControllerState implements ControllerState { @@ -15,33 +14,17 @@ public class GameControllerState implements ControllerState { this.controllerEntities = engine.getEntitiesFor(Family.all(Controller.class).get()); } - private void moveEntities(float dx, float dy) { - for (Entity entity : controllerEntities) { - Move move = Move.MAPPER.get(entity); - if (move != null) { - move.getDirection().x += dx; - move.getDirection().y += dy; - } - } - } - @Override public void keyDown(Command command) { - switch (command) { - case UP -> moveEntities(0f, 1f); - case DOWN -> moveEntities(0f, -1f); - case LEFT -> moveEntities(-1f, 0f); - case RIGHT -> moveEntities(1f, 0f); + for (Entity entity : controllerEntities) { + Controller.MAPPER.get(entity).getPressedCommands().add(command); } } @Override public void keyUp(Command command) { - switch (command) { - case UP -> moveEntities(0f, -1f); - case DOWN -> moveEntities(0f, 1f); - case LEFT -> moveEntities(1f, 0f); - case RIGHT -> moveEntities(-1f, 0f); + for (Entity entity : controllerEntities) { + Controller.MAPPER.get(entity).getReleasedCommands().add(command); } } } 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 index d32a204..9de1d72 100644 --- a/core/src/main/java/io/github/com/quillraven/input/KeyboardController.java +++ b/core/src/main/java/io/github/com/quillraven/input/KeyboardController.java @@ -15,7 +15,8 @@ public class KeyboardController extends InputAdapter { Map.entry(Input.Keys.S, Command.DOWN), Map.entry(Input.Keys.A, Command.LEFT), Map.entry(Input.Keys.D, Command.RIGHT), - Map.entry(Input.Keys.SPACE, Command.SELECT) + Map.entry(Input.Keys.SPACE, Command.SELECT), + Map.entry(Input.Keys.ESCAPE, Command.CANCEL) ); private final boolean[] commandState; 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 b3ecc3f..6acbd9b 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 @@ -2,8 +2,6 @@ package io.github.com.quillraven.screen; import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.EntitySystem; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.math.Vector2; @@ -16,6 +14,7 @@ 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.CameraSystem; +import io.github.com.quillraven.system.ControllerSystem; import io.github.com.quillraven.system.FacingSystem; import io.github.com.quillraven.system.FsmSystem; import io.github.com.quillraven.system.PhysicDebugRenderSystem; @@ -55,6 +54,7 @@ public class GameScreen extends ScreenAdapter { this.engine.addSystem(new CameraSystem(game.getCamera())); this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera())); this.engine.addSystem(new PhysicDebugRenderSystem(this.physicWorld, game.getCamera())); + this.engine.addSystem(new ControllerSystem(game)); } @Override @@ -84,11 +84,6 @@ public class GameScreen extends ScreenAdapter { public void render(float delta) { delta = Math.min(1 / 30f, delta); engine.update(delta); - - // TODO refactor - if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) { - this.game.setScreen(MenuScreen.class); - } } @Override diff --git a/core/src/main/java/io/github/com/quillraven/system/ControllerSystem.java b/core/src/main/java/io/github/com/quillraven/system/ControllerSystem.java new file mode 100644 index 0000000..6895707 --- /dev/null +++ b/core/src/main/java/io/github/com/quillraven/system/ControllerSystem.java @@ -0,0 +1,56 @@ +package io.github.com.quillraven.system; + +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.Family; +import com.badlogic.ashley.systems.IteratingSystem; +import io.github.com.quillraven.GdxGame; +import io.github.com.quillraven.component.Controller; +import io.github.com.quillraven.component.Move; +import io.github.com.quillraven.input.Command; +import io.github.com.quillraven.screen.MenuScreen; + +public class ControllerSystem extends IteratingSystem { + private final GdxGame game; + + public ControllerSystem(GdxGame game) { + super(Family.all(Controller.class).get()); + this.game = game; + } + + @Override + protected void processEntity(Entity entity, float deltaTime) { + Controller controller = Controller.MAPPER.get(entity); + if (controller.getPressedCommands().isEmpty() && controller.getReleasedCommands().isEmpty()) { + return; + } + + for (Command command : controller.getPressedCommands()) { + switch (command) { + case UP -> moveEntity(entity, 0f, 1f); + case DOWN -> moveEntity(entity, 0f, -1f); + case LEFT -> moveEntity(entity, -1f, 0f); + case RIGHT -> moveEntity(entity, 1f, 0f); + case CANCEL -> game.setScreen(MenuScreen.class); + } + } + controller.getPressedCommands().clear(); + + for (Command command : controller.getReleasedCommands()) { + switch (command) { + case UP -> moveEntity(entity, 0f, -1f); + case DOWN -> moveEntity(entity, 0f, 1f); + case LEFT -> moveEntity(entity, 1f, 0f); + case RIGHT -> moveEntity(entity, -1f, 0f); + } + } + controller.getReleasedCommands().clear(); + } + + private void moveEntity(Entity entity, float dx, float dy) { + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.getDirection().x += dx; + move.getDirection().y += dy; + } + } +}