USM (Universal State Machine) for games/3D sites with adapters for Three.js, input, and UI.
npm install @nappycat/usmTiny, predictable finite-state machine (FSM) for games, 3D sites, and interactive apps.
Ships as ESM + CJS + UMD with full type declarations. Includes adapters for Three.js, keyboard, pointer, and UI.
Why TS? Clear APIs + great IntelliSense + safer releases — but users still get plain JavaScript at runtime.
---
bash
npm i @nappycat/usm
`Quick start (ESM)
`ts
import { USM } from '@nappycat/usm';type Ctx = { score: number };
const machine = new USM({
id: 'demo',
initial: 'menu',
context: { score: 0 },
states: {
menu: { on: { START: 'play' } },
play: {
enter(ctx){ ctx.score = 0; },
tick(dt, ctx){ / game loop here / },
on: { GAME_OVER: 'over' }
},
over: { on: { RESTART: 'play' } }
}
});
machine.start();
`Adapters
- threeAdapter({ THREE, scene, camera, renderer, mode:'container'|'window' })
Handles DPR clamp + resize + camera.aspect updates.
- keyboardAdapter({ down, up, preventRepeat, combo })
Tracks Set of pressed keys (KeyboardEvent.code), dispatches events.
- pointerAdapter({ target, bind, onDrag, onTap, onWheel })
Unifies mouse/touch/pen, swipe detection, wheel.
- uiAdapter()
Syncs for CSS.`ts
import { adapters } from '@nappycat/usm';
// machine = new USM({ ..., adapters: [ adapters.keyboardAdapter({...}), ... ] })
`CDN / UMD usage
`html
``