This package contains classes for creating and using finite state machines with Ash. Specifically, it contains classes for creating states based on the components on an entity, and for switching between those states.
npm i @ash.ts/fsm
yarn add @ash.ts/fsm
TypeDoc generated API docs
A finite state machine is an instance of the EntityStateMachine class. You pass it a reference to the entity it will manage when constructing it. You will usually store the state machine in a component on the entity so it can be recovered from within any system that is operating on the entity.
A state machine is configured with states, and the state can be changed by calling the state machine's
method. States are identified by a string, which is assigned when the state is created and used to identify the state
when calling the
States are instances of the EntityState class. They may be added to the EntityStateMachine using the
EntityStateMachine.addState() method, or they may be created and added in one call using the
A state is a set of components that should be added to the entity when that state is entered, and removed when that state exits (unless they are also required for the next state).
States within the state machine are configured with component providers. A component provider has a method to provide a component to the entity, and a second method to identify if two component providers are equivalent - i.e. if they would provide an equivalent component.
A component provider implements the
The standard method for adding a component provider to a state is
There are shortcuts for using the standard component providers
uses the ComponentTypeProvider to provide a new instance of instanceType. instanceType should be the same as or componentType of a type that extends componentType.
If instanceType is the same as componentType then it may be omitted.
uses the ComponentInstanceProvider to provide the same instance every time it is called.
uses the ComponentSingletonProvider to create a new instance of instanceType and provide that same instance every time it is called. The instance is not created until it is first required.
The methods can be chained together to create a fluent interface. For example
;fsm.createState'playing'.addMotion.withInstancenew Motion0, 0, 0, 15.addMotionControls.withInstancenew MotionControlsKeyboard.LEFT, Keyboard.RIGHT, Keyboard.UP, 100, 3.addGun.withInstancenew Gun8, 0, 03, 2.addGunControls.withInstancenew GunControlsKeyboard.SPACE.addCollision.withInstancenew Collision9.addDisplay.withInstancenew Displaynew SpaceshipView;fsm.createState'destroyed'.addDeathThroes.withInstancenew DeathThroes5.addDisplay.withInstancenew DisplaydeathView.addAnimation.withInstancenew AnimationdeathView;fsm.changeState'playing';