vanilladocs
vanillapunch.com
API / base/fsm

base/fsm

유한 상태 기계.

Finite state machine.

FiniteStateMachineextends Object

짝Counterpart: vanilla-engine FiniteStateMachine

유한 상태 기계.

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

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
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());
생성자constructorconstructor()#0.1.0 부터since 0.1.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine FiniteStateMachine.new

메서드methodaddState(stateName, handlers = {})#0.3.0 부터since 0.3.0소스source

상태 등록.

  • handlers: { enter?, tick?, exit? } enter(previousStateName), tick(timeDelta, stateSeconds), exit(nextStateName)

Registers a state.

  • handlers: { enter?, tick?, exit? } enter(previousStateName), tick(timeDelta, stateSeconds), exit(nextStateName)
stateNamestring
상태 이름. (이미 있으면 처리 함수를 바꿔 넣는다)State name. (replaces the handlers if it already exists)
handlersobject
{ enter, tick, exit } 처리 함수. (모두 생략할 수 있다, 기본 {}){ enter, tick, exit } handlers. (all optional, default {})

짝Counterpart: vanilla-engine FiniteStateMachine.add_state

메서드methodchangeState(nextStateName, isForced = false)#0.3.0 부터since 0.3.0소스source

상태 전이.

  • 같은 상태로의 전이는 기본적으로 무시한다. (isForced 가 참이면 exit / enter 를 다시 부른다)
  • 전이 가드가 거짓을 반환하면 전이하지 않는다.

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.
nextStateNamestring
옮겨 갈 상태 이름. (등록하지 않은 이름이면 전이하지 않는다)Name of the state to move to. (no transition if the name is not registered)
isForcedboolean
참이면 같은 상태로도 다시 전이한다. (기본 false, 전이 가드는 그대로 본다)If true, transitions again even to the same state. (default false, the transition guard still applies)

반환Returnsboolean 전이 성공 여부.Whether the transition succeeded.

짝Counterpart: vanilla-engine FiniteStateMachine.change_state

메서드methodtick(timeDelta)#0.1.0 부터since 0.1.0소스source

갱신.

Update.

timeDeltanumber
지난 프레임과의 간격. (초, 상태에 머문 시간에 더하고 현재 상태의 tick 에 넘긴다)Interval since the last frame. (seconds, added to the time in the state and passed to the current state's tick)

짝Counterpart: vanilla-engine FiniteStateMachine.tick

메서드methodgetStateName()#0.3.0 부터since 0.3.0소스source

현재 상태 이름 반환.

Returns the current state name.

반환Returnsstring | null 현재 상태 이름. (아직 전이하지 않았으면 null)Current state name. (null if no transition yet)

짝Counterpart: vanilla-engine FiniteStateMachine.get_state_name

메서드methodgetStateSeconds()#0.3.0 부터since 0.3.0소스source

현재 상태에 머문 시간 반환. (초)

Returns the time spent in the current state. (seconds)

반환Returnsnumber 현재 상태에 머문 시간. (초, 전이할 때 0 으로 돌아간다)Time spent in the current state. (seconds, reset to 0 on transition)

짝Counterpart: vanilla-engine FiniteStateMachine.get_state_seconds

메서드methodhasState(stateName)#0.3.0 부터since 0.3.0소스source

상태 존재 여부.

Whether the state exists.

stateNamestring
찾을 상태 이름.State name to find.

반환Returnsboolean 등록한 상태이면 참.True if the state is registered.

짝Counterpart: vanilla-engine FiniteStateMachine.has_state

메서드methodsetTransitionGuard(transitionGuard)#0.3.0 부터since 0.3.0소스source

전이 가드 설정. (previous, next) => boolean

Sets the transition guard. (previous, next) => boolean

transitionGuardFunction
(previousStateName, nextStateName) => boolean. (거짓을 돌려주면 전이하지 않는다, null 이면 가드 없음)(previousStateName, nextStateName) => boolean. (no transition if it returns false, no guard if null)

짝Counterpart: vanilla-engine FiniteStateMachine.set_transition_guard

예제Example쓰러진 뒤로는 전이 막기Block transitions after death
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());
메서드methodsetTransitionEvent(transitionEvent)#0.1.0 부터since 0.1.0소스source

전이 알림 설정. (previous, next) => void, exit 뒤, enter 앞에 불린다.

Sets the transition notification. (previous, next) => void, called after exit, before enter.

transitionEventFunction
(previousStateName, nextStateName) => void. (null 이면 부르지 않는다)(previousStateName, nextStateName) => void. (not called if null)

짝Counterpart: vanilla-engine FiniteStateMachine.set_transition_event