ecspresso
    Preparing search index...

    Function createStateMachinePlugin

    • Create a state machine plugin for ECSpresso.

      Provides:

      • Lifecycle hooks (onEnter, onExit, onUpdate) per state
      • Guard-based automatic transitions evaluated each tick
      • Event-based transitions via sendEvent()
      • Direct transitions via transitionTo()
      • stateTransition events published on every transition

      Type Parameters

      • S extends string = string
      • G extends string = "stateMachine"

      Parameters

      Returns Plugin<
          WorldConfigFrom<
              StateMachineComponentTypes<S>,
              StateMachineEventTypes<S>,
          >,
          EmptyConfig,
          "state-machine-update",
          G,
      >

      const ecs = ECSpresso.create()
      .withPlugin(createStateMachinePlugin())
      .build();

      const fsm = defineStateMachine('enemy', {
      initial: 'idle',
      states: {
      idle: {
      transitions: [{ target: 'chase', guard: ({ ecs, entityId }) => playerNearby(ecs, entityId) }],
      },
      chase: {
      onUpdate: ({ ecs, entityId, dt }) => moveTowardPlayer(ecs, entityId, dt),
      on: { playerLost: 'idle' },
      },
      },
      });

      ecs.spawn({
      ...createStateMachine(fsm),
      position: { x: 0, y: 0 },
      });