learn-dlang/lesson#28 - Player movement.../ourGame/source/game/gstatemanager.d

56 lines
1.1 KiB
D

module game.gstatemanager;
import game.data;
class GStateManager {
// private class instance
private static GStateManager instance;
// IState interface
private IState[GameState] state;
// current game state
private GameState currGameState;
// private constructor
private this() { }
// return the instance; create the instance, if it wasn't created yet
static GStateManager getInstance() {
if(instance is null) {
instance = new GStateManager();
}
return instance;
}
// add game state
void add(IState state, GameState gs) {
this.state[gs] = state;
}
// remove game state
void remove(GameState gs) {
this.state.remove(gs);
}
// set game state
void setState(GameState gs) {
currGameState = gs;
}
// return the current game state
GameState getState() {
return currGameState;
}
// execute the current game state code
void execute() {
if(state is null) {
return;
}
state[currGameState].run();
}
}