# base/fsm (vanilla.js 0.4.0)

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

유한 상태 기계.

## FiniteStateMachine extends Object

유한 상태 기계.
- 여러 게임이 "문자열 상태 + switch 문 + 수동 전이" 를 제각각 만들던 것을 표준화했다.
- 상태마다 enter / tick / exit 처리를 등록하고 changeState() 로 전이한다.
- setTransitionGuard() 로 허용되지 않은 전이를 막을 수 있다.

예제: 상태 등록하고 갱신하며 전이하기

```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): 생성.
- `addState(stateName, handlers = {})` (method, since 0.3.0): 상태 등록.
  - handlers: { enter?, tick?, exit? }
    enter(previousStateName), tick(timeDelta, stateSeconds), exit(nextStateName)
  - `stateName` string: 상태 이름. (이미 있으면 처리 함수를 바꿔 넣는다)
  - `handlers` object: { enter, tick, exit } 처리 함수. (모두 생략할 수 있다, 기본 {})
- `changeState(nextStateName, isForced = false)` (method, since 0.3.0): 상태 전이.
  - 같은 상태로의 전이는 기본적으로 무시한다. (isForced 가 참이면 exit / enter 를 다시 부른다)
  - 전이 가드가 거짓을 반환하면 전이하지 않는다.
  - `nextStateName` string: 옮겨 갈 상태 이름. (등록하지 않은 이름이면 전이하지 않는다)
  - `isForced` boolean: 참이면 같은 상태로도 다시 전이한다. (기본 false, 전이 가드는 그대로 본다)
  - returns boolean: 전이 성공 여부.
- `tick(timeDelta)` (method, since 0.1.0): 갱신.
  - `timeDelta` number: 지난 프레임과의 간격. (초, 상태에 머문 시간에 더하고 현재 상태의 tick 에 넘긴다)
- `getStateName()` (method, since 0.3.0): 현재 상태 이름 반환.
  - returns string | null: 현재 상태 이름. (아직 전이하지 않았으면 null)
- `getStateSeconds()` (method, since 0.3.0): 현재 상태에 머문 시간 반환. (초)
  - returns number: 현재 상태에 머문 시간. (초, 전이할 때 0 으로 돌아간다)
- `hasState(stateName)` (method, since 0.3.0): 상태 존재 여부.
  - `stateName` string: 찾을 상태 이름.
  - returns boolean: 등록한 상태이면 참.
- `setTransitionGuard(transitionGuard)` (method, since 0.3.0): 전이 가드 설정. (previous, next) => boolean
  - `transitionGuard` Function: (previousStateName, nextStateName) => boolean. (거짓을 돌려주면 전이하지 않는다, null 이면 가드 없음)

  예제: 쓰러진 뒤로는 전이 막기

  ```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): 전이 알림 설정. (previous, next) => void, exit 뒤, enter 앞에 불린다.
  - `transitionEvent` Function: (previousStateName, nextStateName) => void. (null 이면 부르지 않는다)
