Created
August 19, 2018 14:45
-
-
Save m3mnoch/02032b9950bb3b3f0c9d406f0f0ba3e9 to your computer and use it in GitHub Desktop.
quick dcl state machine example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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