Skip to content

Instantly share code, notes, and snippets.

@m3mnoch
Created August 19, 2018 14:45
Show Gist options
  • Save m3mnoch/02032b9950bb3b3f0c9d406f0f0ba3e9 to your computer and use it in GitHub Desktop.
Save m3mnoch/02032b9950bb3b3f0c9d406f0f0ba3e9 to your computer and use it in GitHub Desktop.
quick dcl state machine example
export default interface IState {
onUpdate(deltaTime: number): void;
onEnter(param?: any): void;
onExit(): void;
}
export namespace GameStateMachine {
const sceneStates: { [id: string]: IState } = {};
let myCurrentState: IState;
export function change(stateName: string, param?: any): void {
if (stateName in sceneStates) {
if (myCurrentState) myCurrentState.onExit();
myCurrentState = sceneStates[stateName];
myCurrentState.onEnter(param);
} else {
console.log("state does not exist. one of these days, handle it here.");
}
}
export function add(name: string, state: IState): void {
sceneStates[name] = state;
}
}
{
GameStateMachine.add(GameConfig.states.RESET, new StateReset());
GameStateMachine.add(GameConfig.states.SPINNER, new StateSpinnerTest());
GameStateMachine.add(GameConfig.states.SKELETON, new StateSkeleton());
// kick it off.
GameStateMachine.change(GameConfig.states.RESET);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment