add idle controller state

This commit is contained in:
Quillraven
2025-05-30 23:58:02 +02:00
parent bcbac0fa90
commit 3c06b914e3
4 changed files with 39 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ import io.github.com.quillraven.asset.AssetService;
import io.github.com.quillraven.screen.LoadingScreen;
import java.util.HashMap;
import java.util.Map;
public class GdxGame extends Game {
public static final float WORLD_HEIGHT = 9f;
@@ -31,7 +32,7 @@ public class GdxGame extends Game {
private GLProfiler glProfiler;
private InputMultiplexer inputMultiplexer;
private final HashMap<Class<? extends Screen>, Screen> screenCache = new HashMap<>();
private final Map<Class<? extends Screen>, Screen> screenCache = new HashMap<>();
@Override
public void create() {

View File

@@ -0,0 +1,11 @@
package io.github.com.quillraven.input;
public class IdleControllerState implements ControllerState {
@Override
public void keyDown(Command command) {
}
@Override
public void keyUp(Command command) {
}
}

View File

@@ -1,8 +1,12 @@
package io.github.com.quillraven.input;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.utils.GdxRuntimeException;
import java.util.HashMap;
import java.util.Map;
public class KeyboardController extends InputAdapter {
@@ -14,18 +18,32 @@ public class KeyboardController extends InputAdapter {
);
private final boolean[] commandState;
private final Map<Class<? extends ControllerState>, ControllerState> stateCache;
private ControllerState activeState;
public KeyboardController(ControllerState initialState) {
public KeyboardController(Class<? extends ControllerState> initialState, ImmutableArray<Entity> controllerEntities) {
this.commandState = new boolean[Command.values().length];
this.stateCache = new HashMap<>();
this.activeState = null;
this.stateCache.put(IdleControllerState.class, new IdleControllerState());
this.stateCache.put(GameControllerState.class, new GameControllerState(controllerEntities));
setActiveState(initialState);
}
public void setActiveState(ControllerState controllerState) {
public void setActiveState(Class<? extends ControllerState> stateClass) {
ControllerState state = stateCache.get(stateClass);
if (state == null) {
throw new GdxRuntimeException("State " + stateClass.getSimpleName() + " not found in cache");
}
for (Command command : Command.values()) {
if (this.activeState != null && this.commandState[command.ordinal()]) {
this.activeState.keyUp(command);
}
this.commandState[command.ordinal()] = false;
}
this.activeState = controllerState;
this.activeState = state;
}
@Override

View File

@@ -34,6 +34,7 @@ public class GameScreen extends ScreenAdapter {
private final Engine engine;
private final TiledAshleySpawner tiledAshleySpawner;
private final World physicWorld;
private final KeyboardController keyboardController;
public GameScreen(GdxGame game) {
this.game = game;
@@ -42,6 +43,8 @@ public class GameScreen extends ScreenAdapter {
this.physicWorld.setAutoClearForces(false);
this.engine = new Engine();
this.tiledAshleySpawner = new TiledAshleySpawner(this.engine, this.physicWorld);
ImmutableArray<Entity> controllerEntities = this.engine.getEntitiesFor(Family.all(Controller.class).get());
this.keyboardController = new KeyboardController(GameControllerState.class, controllerEntities);
// add ECS systems
this.engine.addSystem(new PhysicMoveSystem());
@@ -57,9 +60,8 @@ public class GameScreen extends ScreenAdapter {
@Override
public void show() {
this.game.getInputMultiplexer().clear();
ImmutableArray<Entity> controllerEntities = this.engine.getEntitiesFor(Family.all(Controller.class).get());
GameControllerState gameControllerState = new GameControllerState(controllerEntities);
this.game.getInputMultiplexer().addProcessor(new KeyboardController(gameControllerState));
this.game.getInputMultiplexer().addProcessor(keyboardController);
keyboardController.setActiveState(GameControllerState.class);
Consumer<TiledMap> renderConsumer = this.engine.getSystem(RenderSystem.class)::setMap;
Consumer<TiledMap> ashleySpawnerConsumer = this.tiledAshleySpawner::loadMapObjects;