Files
GameAPI/src/main/java/com/softwarerat/API/data/gameState/GameStateManager.java
T
2026-02-15 13:25:50 +01:00

40 lines
1.0 KiB
Java

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