This commit is contained in:
Laura
2026-02-15 13:25:50 +01:00
parent 235ee7e7a1
commit a7620c09f8
32 changed files with 455 additions and 91 deletions
@@ -0,0 +1,39 @@
package com.softwarerat.API.data.gameState;
import java.util.Arrays;
public class GameStateManager {
private final IGameState[] gameStates;
private int currentStateId;
public GameStateManager(IGameState... gameStates) {
this.gameStates = gameStates;
}
public IGameState getCurrentState() {
if (this.currentStateId < 0 || this.currentStateId >= this.gameStates.length)
return null;
return this.gameStates[this.currentStateId];
}
public IGameState nextState() {
if (this.currentStateId + 1 == this.gameStates.length)
throw new IllegalStateException("No states remaining");
IGameState oldState = getCurrentState();
this.currentStateId++;
IGameState newState = getCurrentState();
oldState.Stop();
newState.Start(this);
return newState;
}
@SafeVarargs
public final boolean isCurrentGameState(Class<? extends IGameState>... states) {
return Arrays.<Class<? extends IGameState>>stream(states).anyMatch(state -> state.isInstance(getCurrentState()));
}
}