Need private packages and team management tools?Check out npm Teams »

ts-fence

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

Install

npm i ts-fence

DownloadsWeekly Downloads

2

Version

1.2.1

License

MIT

Unpacked Size

32.8 kB

Total Files

16

Last publish

Collaborators

  • avatar