// Import StateMachine and State classesvarStateMachine=require('kwstatemachine').StateMachine;varState=require('kwstatemachine').State;// Define your States by extending State classclassGameStartextendsState{// Here you define what is a possible next state by returning trueisValidNextState(state){returnstateinstanceofGamePlay;}// This is entry point to StateonEnter(){this.stateMachine.enter(gamePlay,{value: "passed"});}// This will be executed if you attempt and success to// move to next stateonExit(){}// This can be used to execute tick type loops (e.g.: draw() in p5js)onUpdate(){}}classGamePlayextendsState{isValidNextState(state){returnstateinstanceofGameEnd;}onEnter(data){this.data=data;}onExit(){}onUpdate(){}}classGameEndextendsState{onEnter(){}onExit(){}onUpdate(){}}// Once you got all your States defined, you need// to extend StateMachine, you can then add update to run// onUpdate() on your active stateclassGameStateMachineextendsStateMachine{update(){this.currentState.onUpdate();}}// create statesvargameStart=newGameStart();vargamePlay=newGamePlay();vargameEnd=newGameEnd();// create state machine and add statesvarsm=newGameStateMachine([gameStart,gamePlay,gameEnd]);// enter initial statesm.enter(gameStart);