A finite state machine that lives with a piece of the scene: a character that is standing,
running or jumping, an enemy that patrols, chases and goes back, a door, a boss, a level's
phases.
Each state says what it does on the way in (onEnter), every frame it lasts (onUpdate) and on
the way out (onExit), and the machine is in exactly one of them at a time. The names come from
the object, so go('jum') is a type error.
It is a hook like the rest, so it works wherever one does: the body of a scene, of a piece made
with useSpawn (a Hero), or of a behaviour. It belongs to that object: it runs with its
updates, in the order it was written among them, stops in a paused scene, and goes when the
object goes.
The state it is in is saved with the object, like useData, and a scene opened again starts in
it (calling its onEnter), with the time it had been there.
States inside a state. Given { parent, state }, the machine lives inside that state of
another: it enters its initial when the other enters that state, and leaves when the other
leaves it, from the inside out (onExit of its state, then of the other's) and in from the
outside in. Each frame the other's onUpdate runs first, so what holds for the whole state (a
guard that has lost the hero leaves combat, whichever move it was in) is written once there, and
then its own. Each has its own names, time and onChange; a change asked for from inside a
change of any of them waits until that change is over. It can live in the same object as the
other or in a piece of its own, and another machine can live inside one of its states in turn.
Call it in the body, like the rest of the hooks.
Example
declare const sheet: TSpriteAtlas;
const Hero = ({ x, y }: { x: number; y: number }) => {
const sprite = createSprite({ atlas: sheet, frame: 0, transform: { x, y } });
const anim = useSpriteAnimation(sprite, {
clips: {
idle: { frames: [0, 1], fps: 4 },
run: { frames: [2, 3, 4, 5], fps: 12 },
jump: { frames: [6], fps: 1 },
hurt: { frames: [7, 8], fps: 12, loop: false },
},
play: 'idle',
});
const keys = useKeyboard();
const hero = useStateMachine('idle', {
idle: {
onEnter: () => anim.play('idle'),
onUpdate: () => {
if (keys.isDown('ArrowRight')) hero.go('run');
if (keys.justPressed('Space')) hero.go('jump');
},
},
run: {
onEnter: () => anim.play('run'),
onUpdate: (delta) => {
sprite.transform.x += 90 * delta;
if (!keys.isDown('ArrowRight')) hero.go('idle');
},
},
jump: {
onEnter: () => anim.play('jump'),
onUpdate: () => {
if (hero.time > 0.6) hero.go('idle');
},
},
hurt: {
onEnter: () => anim.play('hurt'),
onUpdate: () => {
if (hero.time > 0.4) hero.go('idle');
},
},
});
// What the level can ask of it.
return { hurt: () => hero.go('hurt', { restart: true }) };
};
export const Level: TSceneFn = () => {
const hero = useSpawn(Hero, 'Hero')({ x: 40, y: 160 });
usePointer().onDown(() => hero.api.hurt());
return createScene();
};
Example
declare const banner: TText;
declare const bossDefeated: TGameSignal<void>;
// A scene's own phases.
export const Boss: TSceneFn = () => {
const scene = useScene();
const fight = useStateMachine('intro', {
intro: {
onEnter: () => { banner.text = 'READY?'; },
onUpdate: () => { if (fight.time > 2) fight.go('fight'); },
},
fight: {
onEnter: () => { banner.text = ''; },
},
won: {
onEnter: () => { banner.text = 'YOU WIN'; },
onUpdate: () => { if (fight.time > 3) scene.change('Map'); },
},
});
useSignal(bossDefeated, () => fight.go('won'));
useWatch(() => console.log('now', fight.state), [fight]);
return createScene();
};
Example
declare const lostHero: () => boolean;
// States inside a state: the guard is calm or in combat, and each has moves of its own.
const guard = useStateMachine('calm', {
calm: {},
combat: {
// Holds for every move of the combat, written once.
onUpdate: () => { if (lostHero()) guard.go('calm'); },
},
});
const calm = useStateMachine('patrol', {
patrol: {},
search: { onUpdate: () => { if (calm.time > 2) calm.go('back'); } },
back: {},
}, { parent: guard, state: 'calm' });
const combat = useStateMachine('approach', {
approach: {},
swing: {},
recover: { onUpdate: () => { if (combat.time > 0.5) combat.go('approach'); } },
}, { parent: guard, state: 'combat' });
// From anywhere: into combat, straight at a swing.
combat.go('swing');