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.
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.frameType Parameters ​
| Type Parameter | Default type |
|---|---|
State extends string | string |
Clip extends string | string |
P extends AnimParams | AnimParams |
Constructors ​
Constructor ​
new AnimStateMachine<State, Clip, P>(clips, options): AnimStateMachine<State, Clip, P>;Defined in: src/anim/AnimStateMachine.ts:61
Parameters ​
| Parameter | Type |
|---|---|
clips | Record<Clip, AnimationClip> |
options | AnimStateMachineOptions<State, Clip, P> |
Returns ​
AnimStateMachine<State, Clip, P>
Properties ​
animator ​
readonly animator: Animator<Clip>;Defined in: src/anim/AnimStateMachine.ts:47
The animator it drives; read frame off it (or off the machine).
onEvent ​
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 ​
onStateChange: ((from, to) => void) | null = null;Defined in: src/anim/AnimStateMachine.ts:51
Fired after each change of state.
Accessors ​
frame ​
Get Signature ​
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 ​
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 ​
get state(): State;Defined in: src/anim/AnimStateMachine.ts:73
The state playing.
Returns ​
State
Methods ​
get() ​
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 ​
| Parameter | Type |
|---|---|
name | keyof P & string |
Returns ​
number | boolean
play() ​
play(state): this;Defined in: src/anim/AnimStateMachine.ts:114
Goes to state now, whatever the transitions say (a respawn, a cut scene).
Parameters ​
| Parameter | Type |
|---|---|
state | State |
Returns ​
this
resetParams() ​
resetParams(): this;Defined in: src/anim/AnimStateMachine.ts:122
Back to the initial values of every parameter (triggers cleared).
Returns ​
this
resetTrigger() ​
resetTrigger(name): this;Defined in: src/anim/AnimStateMachine.ts:108
Parameters ​
| Parameter | Type |
|---|---|
name | keyof P & string |
Returns ​
this
set() ​
set(name, value): this;Defined in: src/anim/AnimStateMachine.ts:88
Sets a bool or number parameter.
Parameters ​
| Parameter | Type |
|---|---|
name | keyof P & string |
value | number | boolean |
Returns ​
this
trigger() ​
trigger(name): this;Defined in: src/anim/AnimStateMachine.ts:103
Fires a trigger: the next update's transitions see it once.
Parameters ​
| Parameter | Type |
|---|---|
name | keyof P & string |
Returns ​
this
update() ​
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 ​
| Parameter | Type |
|---|---|
dt | number |
Returns ​
void