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... states) { return Arrays.>stream(states).anyMatch(state -> state.isInstance(getCurrentState())); } }