refactor game input handling via controller component

This commit is contained in:
Quillraven
2025-06-01 18:05:42 +02:00
parent 878eb924a5
commit cdb23860b8
6 changed files with 88 additions and 30 deletions

View File

@@ -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<Controller> MAPPER = ComponentMapper.getFor(Controller.class);
private final List<Command> pressedCommands;
private final List<Command> releasedCommands;
public Controller() {
this.pressedCommands = new ArrayList<>();
this.releasedCommands = new ArrayList<>();
}
public List<Command> getPressedCommands() {
return pressedCommands;
}
public List<Command> getReleasedCommands() {
return releasedCommands;
}
}

View File

@@ -5,5 +5,6 @@ public enum Command {
RIGHT,
DOWN,
UP,
SELECT
SELECT,
CANCEL
}

View File

@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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;
}
}
}