refactor
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user