# base/fsm (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/fsm/

Finite state machine.

## FiniteStateMachine extends Object

Finite state machine.
- Standardizes the "string state + switch statement + manual transition" that several games each built separately.
- Registers enter / tick / exit handling per state and transitions with changeState().
- setTransitionGuard() can block disallowed transitions.

Example: Register states, update and transition

```js
import { FiniteStateMachine } from "./build/vanilla.js";

const stateMachine = new FiniteStateMachine();
stateMachine.addState("idle", {
	enter: (previousStateName) => {
		console.log("enter idle from", previousStateName);
	},
});
stateMachine.addState("attack", {
	tick: (timeDelta, stateSeconds) => {
		if (stateSeconds >= 1) {
			stateMachine.changeState("idle");
		}
	},
	exit: (nextStateName) => {
		console.log("exit attack to", nextStateName);
	},
});
stateMachine.changeState("idle");
stateMachine.changeState("attack");
stateMachine.tick(0.5);
console.log(stateMachine.getStateName(), stateMachine.getStateSeconds());
stateMachine.tick(0.5);
console.log(stateMachine.getStateName());
```

- `constructor()` (constructor, since 0.1.0): Constructor.
- `addState(stateName, handlers = {})` (method, since 0.3.0): Registers a state.
  - handlers: { enter?, tick?, exit? }
    enter(previousStateName), tick(timeDelta, stateSeconds), exit(nextStateName)
  - `stateName` string: State name. (replaces the handlers if it already exists)
  - `handlers` object: { enter, tick, exit } handlers. (all optional, default {})
- `changeState(nextStateName, isForced = false)` (method, since 0.3.0): Transitions the state.
  - A transition to the same state is ignored by default. (if isForced is true, calls exit / enter again)
  - If the transition guard returns false, no transition happens.
  - `nextStateName` string: Name of the state to move to. (no transition if the name is not registered)
  - `isForced` boolean: If true, transitions again even to the same state. (default false, the transition guard still applies)
  - returns boolean: Whether the transition succeeded.
- `tick(timeDelta)` (method, since 0.1.0): Update.
  - `timeDelta` number: Interval since the last frame. (seconds, added to the time in the state and passed to the current state's tick)
- `getStateName()` (method, since 0.3.0): Returns the current state name.
  - returns string | null: Current state name. (null if no transition yet)
- `getStateSeconds()` (method, since 0.3.0): Returns the time spent in the current state. (seconds)
  - returns number: Time spent in the current state. (seconds, reset to 0 on transition)
- `hasState(stateName)` (method, since 0.3.0): Whether the state exists.
  - `stateName` string: State name to find.
  - returns boolean: True if the state is registered.
- `setTransitionGuard(transitionGuard)` (method, since 0.3.0): Sets the transition guard. (previous, next) => boolean
  - `transitionGuard` Function: (previousStateName, nextStateName) => boolean. (no transition if it returns false, no guard if null)

  Example: Block transitions after death

  ```js
  import { FiniteStateMachine } from "./build/vanilla.js";
  
  const stateMachine = new FiniteStateMachine();
  stateMachine.addState("alive");
  stateMachine.addState("dead");
  stateMachine.setTransitionGuard((previousStateName, nextStateName) => {
  	if (previousStateName === "dead") {
  		return false;
  	}
  	return true;
  });
  stateMachine.changeState("alive");
  stateMachine.changeState("dead");
  const isChanged = stateMachine.changeState("alive");
  console.log(isChanged, stateMachine.getStateName());
  ```

- `setTransitionEvent(transitionEvent)` (method, since 0.1.0): Sets the transition notification. (previous, next) => void, called after exit, before enter.
  - `transitionEvent` Function: (previousStateName, nextStateName) => void. (not called if null)
