ts-fence
v1.2.1
Published
typescript statemachine
Downloads
3
Maintainers
Readme
The descriptive statemachine for typescript
$ npm i -D ts-fence
import { StateMachine, StateTransition, IStateMachineDescription } from 'ts-fence'
interface IPlayer {
health: number
}
interface PlayerStateMachineDescription extends IStateMachineDescription {
player: IPlayer
}
const player: IPlayer = {
health: 100
}
const _description: PlayerStateMachineDescription = {
player,
[StateMachine.STARTING_STATE]: 'idle',
[StateMachine.STATES]: {
// describe states here
idle: {
[StateMachine.ON_ENTER]: () => {},
// describe state triggers and actions here
stabbed({ scope, stateMachine }: { scope: PlayerStateMachineDescription, stateMachine: StateMachine }, damage: number) {
scope.player.health = scope.player.health - damage < 0 ? 0 : scope.player.health - damage;
if (scope.player.health === 0) {
stateMachine.die()
}
},
die: new StateTransition('game-over', (): any => undefined),
[StateMachine.ON_EXIT]: () => {},
},
'game-over': {
...
}
}
}