Skip to content

1681-engine / index / AnimStateMachine

Class: AnimStateMachine<State, Clip, P> ​

Defined in: src/anim/AnimStateMachine.ts:45

Chooses which clip an Animator plays from named states and the transitions between them, the way an animation graph in a larger engine does: the game sets parameters (a bool, a number, a one-shot trigger) and the machine moves between states when a transition's conditions hold, when a clip has played through, or from any state at all by priority. Frame markers fire events (a footstep on frame 3) for the game to hang sounds and effects on.

Pure and driven by update(dt), so it runs under node with a fake clock. The definition is compiled into typed arrays once; an update allocates nothing.

ts
const sm = new AnimStateMachine(sheet.clips, {
  initial: 'idle',
  params: { speed: 0, grounded: true, jump: 'trigger' },
  states: { idle: { clip: 'idle' }, run: { clip: 'run', markers: [{ frame: 2, name: 'step' }] }, jump: { clip: 'jump' } },
  transitions: [
    { from: 'idle', to: 'run', when: { speed: ['>', 0] } },
    { from: 'run', to: 'idle', when: { speed: 0 } },
    { from: '*', to: 'jump', when: { jump: true, grounded: true }, priority: 10 },
  ],
});
sm.onEvent = (name) => name === 'step' && sound.play('step');
sm.set('speed', 3);
sm.update(dt);              // seconds; then show sm.frame

Type Parameters ​

Type ParameterDefault type
State extends stringstring
Clip extends stringstring
P extends AnimParamsAnimParams

Constructors ​

Constructor ​

ts
new AnimStateMachine<State, Clip, P>(clips, options): AnimStateMachine<State, Clip, P>;

Defined in: src/anim/AnimStateMachine.ts:61

Parameters ​

ParameterType
clipsRecord<Clip, AnimationClip>
optionsAnimStateMachineOptions<State, Clip, P>

Returns ​

AnimStateMachine<State, Clip, P>

Properties ​

animator ​

ts
readonly animator: Animator<Clip>;

Defined in: src/anim/AnimStateMachine.ts:47

The animator it drives; read frame off it (or off the machine).


onEvent ​

ts
onEvent: ((event, state, frame) => void) | null = null;

Defined in: src/anim/AnimStateMachine.ts:49

Fired for every marker playback reaches, the state's first frame's on entering it.


onStateChange ​

ts
onStateChange: ((from, to) => void) | null = null;

Defined in: src/anim/AnimStateMachine.ts:51

Fired after each change of state.

Accessors ​

frame ​

Get Signature ​

ts
get frame(): string | number;

Defined in: src/anim/AnimStateMachine.ts:78

The frame to show now (a sheet frame index or name, as the clip lists it).

Returns ​

string | number


normalizedTime ​

Get Signature ​

ts
get normalizedTime(): number;

Defined in: src/anim/AnimStateMachine.ts:83

How much of this state's clip has played: 0 at entry, 1 after one pass, 2 after two loops.

Returns ​

number


state ​

Get Signature ​

ts
get state(): State;

Defined in: src/anim/AnimStateMachine.ts:73

The state playing.

Returns ​

State

Methods ​

get() ​

ts
get(name): number | boolean;

Defined in: src/anim/AnimStateMachine.ts:96

A parameter's value: bools as booleans, triggers as whether they are set.

Parameters ​

ParameterType
namekeyof P & string

Returns ​

number | boolean


play() ​

ts
play(state): this;

Defined in: src/anim/AnimStateMachine.ts:114

Goes to state now, whatever the transitions say (a respawn, a cut scene).

Parameters ​

ParameterType
stateState

Returns ​

this


resetParams() ​

ts
resetParams(): this;

Defined in: src/anim/AnimStateMachine.ts:122

Back to the initial values of every parameter (triggers cleared).

Returns ​

this


resetTrigger() ​

ts
resetTrigger(name): this;

Defined in: src/anim/AnimStateMachine.ts:108

Parameters ​

ParameterType
namekeyof P & string

Returns ​

this


set() ​

ts
set(name, value): this;

Defined in: src/anim/AnimStateMachine.ts:88

Sets a bool or number parameter.

Parameters ​

ParameterType
namekeyof P & string
valuenumber | boolean

Returns ​

this


trigger() ​

ts
trigger(name): this;

Defined in: src/anim/AnimStateMachine.ts:103

Fires a trigger: the next update's transitions see it once.

Parameters ​

ParameterType
namekeyof P & string

Returns ​

this


update() ​

ts
update(dt): void;

Defined in: src/anim/AnimStateMachine.ts:131

Advances the clip by dt seconds, fires the markers it passed, then takes at most one transition (so a chain of them never runs through several states in one frame). update(0) only checks the transitions.

Parameters ​

ParameterType
dtnumber

Returns ​

void

Released under the MIT License.