procedural-bot-animations 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * A small typed finite state machine for bot/mascot behavior (idle,
3
+ * thinking, success, error, loading, sleeping, or any custom set). Framework-
4
+ * and renderer-agnostic: it only tracks state and timers, you decide in
5
+ * `onEnter`/`onExit` or in a `bind*` helper what that means visually.
6
+ */
7
+ export function createStateMachine(config) {
8
+ let current = config.initial;
9
+ let timerMs = null;
10
+ let timerTarget = null;
11
+ const context = (config.context ?? {});
12
+ const listeners = new Set();
13
+ function enter(state) {
14
+ const def = config.states[state];
15
+ timerMs = def.after ? def.after.ms : null;
16
+ timerTarget = def.after ? def.after.to : null;
17
+ }
18
+ function transitionTo(next) {
19
+ if (next === current)
20
+ return;
21
+ const previous = current;
22
+ const prevDef = config.states[previous];
23
+ prevDef.onExit?.(context);
24
+ current = next;
25
+ enter(next);
26
+ config.states[next].onEnter?.(context);
27
+ for (const fn of listeners)
28
+ fn(current, previous);
29
+ }
30
+ enter(current);
31
+ config.states[current].onEnter?.(context);
32
+ return {
33
+ getState: () => current,
34
+ getContext: () => context,
35
+ send(event) {
36
+ const currentDef = config.states[current];
37
+ for (const t of config.transitions) {
38
+ if ((t.from === "*" || t.from === current) && t.event === event) {
39
+ if (t.guard && !t.guard(context))
40
+ continue;
41
+ if (currentDef.interruptibleBy &&
42
+ currentDef.interruptibleBy !== "*" &&
43
+ !currentDef.interruptibleBy.includes(t.to)) {
44
+ continue;
45
+ }
46
+ transitionTo(t.to);
47
+ return true;
48
+ }
49
+ }
50
+ return false;
51
+ },
52
+ goto(state) {
53
+ transitionTo(state);
54
+ },
55
+ tick(dtMs) {
56
+ if (timerMs === null || timerTarget === null)
57
+ return;
58
+ timerMs -= dtMs;
59
+ if (timerMs <= 0) {
60
+ transitionTo(timerTarget);
61
+ }
62
+ },
63
+ subscribe(fn) {
64
+ listeners.add(fn);
65
+ return () => listeners.delete(fn);
66
+ },
67
+ };
68
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * The slice of an Open Bot Avatar definition this engine needs: one
3
+ * silhouette path plus eye placement, in a 0 0 100 100 viewBox (see
4
+ * https://github.com/DominikScholz/open-bot-avatar/blob/main/docs/avatar-contract.md).
5
+ * Written as a local, duck-typed interface — not imported from
6
+ * `open-bot-avatar` — so this package has zero required runtime dependency
7
+ * on it. Open Bot Avatar (its editor and `generate-avatar` skill) is used
8
+ * only to *model* the shape; this engine renders and animates it itself.
9
+ */
10
+ export interface AvatarDefinitionLike {
11
+ body: {
12
+ path: string;
13
+ };
14
+ eyes: {
15
+ x: number;
16
+ y: number;
17
+ spacing: number;
18
+ scale: number;
19
+ };
20
+ }
21
+ export interface EyePose {
22
+ /** Slide, in viewBox units, on top of the base eye position. */
23
+ offsetX: number;
24
+ offsetY: number;
25
+ /** Multiplies the neutral eye size. 1 = neutral. */
26
+ scaleX: number;
27
+ scaleY: number;
28
+ /** Degrees. */
29
+ rotate: number;
30
+ /** 0 = fully open, 1 = fully shut (blink/squint-to-closed). */
31
+ shut: number;
32
+ /** 0..1 crossfade from the neutral pill shape to a happy upward arc. */
33
+ arcAmount: number;
34
+ }
35
+ export interface BodyPose {
36
+ /** Viewbox-unit translation, applied before any host-level transform. */
37
+ x: number;
38
+ y: number;
39
+ rotate: number;
40
+ scaleX: number;
41
+ scaleY: number;
42
+ }
43
+ export interface Pose {
44
+ body: BodyPose;
45
+ leftEye: EyePose;
46
+ rightEye: EyePose;
47
+ }
48
+ export declare function neutralEyePose(): EyePose;
49
+ export declare function neutralBodyPose(): BodyPose;
50
+ export declare function neutralPose(): Pose;
51
+ /** Adds `delta` onto `base` in place, used to layer moods/reactions additively. Multiplicative fields (scale) multiply instead of add. */
52
+ export declare function addEyePose(base: EyePose, delta: Partial<EyePose>): EyePose;
53
+ export declare function addBodyPose(base: BodyPose, delta: Partial<BodyPose>): BodyPose;
54
+ export declare function addPose(base: Pose, delta: PoseDelta): Pose;
55
+ export interface PoseDelta {
56
+ body?: Partial<BodyPose>;
57
+ leftEye?: Partial<EyePose>;
58
+ rightEye?: Partial<EyePose>;
59
+ /** Same delta applied to both eyes; merged before the per-eye fields above. */
60
+ eyes?: Partial<EyePose>;
61
+ }
62
+ /** Resolves a `PoseDelta`'s `eyes` convenience field into both `leftEye`/`rightEye`. */
63
+ export declare function resolveDelta(delta: PoseDelta): Required<Pick<PoseDelta, "body" | "leftEye" | "rightEye">>;
package/dist/types.js ADDED
@@ -0,0 +1,46 @@
1
+ export function neutralEyePose() {
2
+ return { offsetX: 0, offsetY: 0, scaleX: 1, scaleY: 1, rotate: 0, shut: 0, arcAmount: 0 };
3
+ }
4
+ export function neutralBodyPose() {
5
+ return { x: 0, y: 0, rotate: 0, scaleX: 1, scaleY: 1 };
6
+ }
7
+ export function neutralPose() {
8
+ return { body: neutralBodyPose(), leftEye: neutralEyePose(), rightEye: neutralEyePose() };
9
+ }
10
+ /** Adds `delta` onto `base` in place, used to layer moods/reactions additively. Multiplicative fields (scale) multiply instead of add. */
11
+ export function addEyePose(base, delta) {
12
+ return {
13
+ offsetX: base.offsetX + (delta.offsetX ?? 0),
14
+ offsetY: base.offsetY + (delta.offsetY ?? 0),
15
+ scaleX: base.scaleX * (delta.scaleX ?? 1),
16
+ scaleY: base.scaleY * (delta.scaleY ?? 1),
17
+ rotate: base.rotate + (delta.rotate ?? 0),
18
+ shut: Math.max(base.shut, delta.shut ?? 0),
19
+ arcAmount: Math.max(base.arcAmount, delta.arcAmount ?? 0),
20
+ };
21
+ }
22
+ export function addBodyPose(base, delta) {
23
+ return {
24
+ x: base.x + (delta.x ?? 0),
25
+ y: base.y + (delta.y ?? 0),
26
+ rotate: base.rotate + (delta.rotate ?? 0),
27
+ scaleX: base.scaleX * (delta.scaleX ?? 1),
28
+ scaleY: base.scaleY * (delta.scaleY ?? 1),
29
+ };
30
+ }
31
+ export function addPose(base, delta) {
32
+ return {
33
+ body: addBodyPose(base.body, delta.body ?? {}),
34
+ leftEye: addEyePose(base.leftEye, delta.leftEye ?? {}),
35
+ rightEye: addEyePose(base.rightEye, delta.rightEye ?? {}),
36
+ };
37
+ }
38
+ /** Resolves a `PoseDelta`'s `eyes` convenience field into both `leftEye`/`rightEye`. */
39
+ export function resolveDelta(delta) {
40
+ const eyes = delta.eyes ?? {};
41
+ return {
42
+ body: delta.body ?? {},
43
+ leftEye: { ...eyes, ...(delta.leftEye ?? {}) },
44
+ rightEye: { ...eyes, ...(delta.rightEye ?? {}) },
45
+ };
46
+ }
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "procedural-bot-animations",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Procedural motion and a state machine for Open Bot Avatar: idle wander, breathing, spring bounce, velocity lean, and typed bot states.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "scripts": {
21
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
22
+ "typecheck": "tsc --noEmit",
23
+ "test": "vitest run",
24
+ "test:watch": "vitest",
25
+ "check": "npm run typecheck && npm run test && npm run build"
26
+ },
27
+ "keywords": [
28
+ "avatar",
29
+ "bot",
30
+ "mascot",
31
+ "procedural-animation",
32
+ "state-machine",
33
+ "open-bot-avatar"
34
+ ],
35
+ "license": "MIT",
36
+ "author": "Jp-mainieri",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/Jp-mainieri/procedural-bot-animations.git"
40
+ },
41
+ "homepage": "https://github.com/Jp-mainieri/procedural-bot-animations",
42
+ "peerDependencies": {
43
+ "open-bot-avatar": ">=0.1.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "open-bot-avatar": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^3.2.4"
53
+ },
54
+ "engines": {
55
+ "node": ">=18"
56
+ }
57
+ }