A state machine picks every animation
Behaviour lives in an XState v5 machine: egg states for wobble, crack and hatch, then parallel awake, asleep and sick states once the pet is alive. The machine doesn't own stats. It reads and changes them through a StoreBridge into the Zustand store, which keeps persistence in one place.
Every transition, plus a mood re-check every nine seconds, sets currentAnim in the machine's context. The canvas just reads that value, so an animation can never disagree with the state that caused it.
export interface StoreBridge {
readStats(): {
hunger: number;
happiness: number;
energy: number;
hygiene: number;
health: number;
};
isAsleep(): boolean;
isSick(): boolean;
feed(kind: 'meal' | 'snack'): { ok: boolean; reason?: string };
play(bonus?: number): { ok: boolean; reason?: string };
// …
deriveMood():
| 'hungry' | 'sad' | 'tired' | 'dirty' | 'sick'
| 'sleeping' | 'great' | 'good' | 'neutral';
}