ts-fence
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Build Status Coverage Status semantic-release npm version Donate

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': {
      ...
    }
  }
}

Package Sidebar

Install

npm i ts-fence

Weekly Downloads

5

Version

1.2.1

License

MIT

Unpacked Size

32.8 kB

Total Files

16

Last publish

Collaborators

  • ovaar