返回 CodeWhale
dot-field.ts
根目录 / pet / src / core / dot-field.ts
1 import { stableHash } from './model.js';
2 import { DOT_BODY } from './dot-body.js';
3 import { DOT_PIXEL_BODY } from './dot-pixel-body.js';
4 import { FORM_HUE, FORM_SCALE, FORM_STILL_TIME, formPoint, type FormKind } from './dot-forms.js';
5 import type { EngineOwnerProjection } from './pet-engine.js';
6
7 export type BodyStyle = 'smooth' | 'pixel';
8 export interface DotOptions {
9 bodyStyle?: BodyStyle;
10 }
11 export interface DotField {
12 points: number[][];
13 materials: number[][];
14 morph: number;
15 bodyStyle: BodyStyle;
16 }
17 const clamp = (n: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n));
18 const ease = (n: number) => {
19 const u = clamp(n);
20 return u * u * (3 - 2 * u);
21 };
22
23 function form(a: EngineOwnerProjection, timeMs: number): FormKind | null {
24 if (a.freshness !== 'fresh' || !a.observed) return null;
25 if (a.authoritativePresence === 'needs_you') return 'waiting';
26 if (a.authoritativePresence === 'done') return timeMs - (a.observedAtMs ?? timeMs) < 2600 ? 'done' : null;
27 if (a.authoritativePresence === 'idle' || !a.activityKind) return null;
28 return a.activityKind === 'tool' ? 'executing' : a.activityKind;
29 }
30
31 export function dotFieldKey(a: EngineOwnerProjection): string {
32 return [
33 a.freshness,
34 a.authoritativePresence,
35 a.activityKind,
36 a.actionId,
37 a.parallelAgentCount,
38 a.doneEffectId,
39 a.activeSpans.length === 0 && !!a.failedToolAge,
40 ].join(':');
41 }
42
43 function pigment(hue: number, light: number, satScale = 0.72): number[] {
44 const h = (((hue % 360) + 360) % 360) / 60;
45 const c = (1 - Math.abs(2 * light - 1)) * satScale;
46 const x = c * (1 - Math.abs((h % 2) - 1));
47 const m = light - c / 2;
48 const rgb =
49 h < 1 ? [c, x, 0] : h < 2 ? [x, c, 0] : h < 3 ? [0, c, x] : h < 4 ? [0, x, c] : h < 5 ? [x, 0, c] : [c, 0, x];
50 return rgb.map((v) => Math.round(clamp(v + m, 0, 1) * 255));
51 }
52
53 export function projectDotField(
54 activity: EngineOwnerProjection,
55 timeMs: number,
56 still = false,
57 expressionAtMs = 0,
58 options: DotOptions = {},
59 ): DotField {
60 const kind = form(activity, timeMs);
61 const identity = activity.actionId || activity.activityKind || 'whale';
62 const variant = (stableHash(identity) % 997) / 997;
63 const t = still ? 0 : timeMs / 1000;
64 const bodyStyle = options.bodyStyle ?? 'smooth';
65 const body = bodyStyle === 'pixel' ? DOT_PIXEL_BODY : DOT_BODY;
66 const fresh = activity.freshness === 'fresh' && activity.observed;
67 const failed = fresh && !activity.activeSpans.length && !!activity.failedToolAge;
68 const elapsed = Math.max(0, timeMs - expressionAtMs) / 1000;
69 const phase = (elapsed + 1.7) % 8.4;
70 const formTime = still ? FORM_STILL_TIME : kind === 'done' ? (timeMs - (activity.observedAtMs ?? timeMs)) / 1000 : elapsed;
71 const morph = !kind
72 ? 0
73 : still
74 ? 1
75 : kind === 'done'
76 ? 1 - ease((timeMs - (activity.observedAtMs ?? timeMs) - 800) / 900)
77 : ease(phase / 1.7) * (1 - ease((phase - 5.4) / 0.9));
78 const agents = activity.parallelAgentCount >= 3 ? 3 : 2;
79 const hue = failed ? 8 : kind ? FORM_HUE[kind] : 206;
80 const points: number[][] = [];
81 const materials: number[][] = [];
82
83 for (let i = 0; i < body.length; i++) {
84 const [hx, hy] = body[i]!;
85 const tail = clamp((-hx - hy + 0.1) / 0.65);
86 const homeX = hx * 1.9 + (still ? 0 : 0.008 * Math.sin(t * 0.75 + hy * 5));
87 const homeY =
88 hy * 1.9 +
89 (still ? 0 : 0.009 * Math.sin(t * 1.1 + hx * 4) + tail * 0.012 * Math.sin(t * 1.55 + hx * 7));
90
91 const inThroat = hy > 0.06 && hx > -0.18;
92 let light = inThroat
93 ? 0.80 + 0.08 * clamp((hx + hy) / 0.7)
94 : 0.58 + 0.16 * clamp((hx + 0.45) / 0.9) + 0.03 * Math.cos(hy * 7);
95 if (!still) light = clamp(light + 0.022 * Math.sin(hx * 9 - hy * 5 - t * 1.1), 0.52, 0.92);
96 const baseHue = inThroat ? 196 : 208 + clamp((-hy - hx + 0.35) / 0.9) * 14;
97 const base = bodyStyle === 'pixel' ? DOT_PIXEL_BODY[i]!.slice(2) : pigment(baseHue, light, inThroat ? 0.82 : 0.74);
98
99 let x = homeX;
100 let y = homeY;
101 let color = fresh ? base : [126, 178, 224];
102 let alpha = fresh ? (inThroat ? 0.94 : 0.86) : (inThroat ? 0.72 : 0.56);
103
104 if (kind && morph > 0) {
105 const e = ease(morph * 1.25 - ((i % 97) / 97) * 0.25);
106 const target = formPoint(kind, bodyStyle, i, formTime, agents);
107 x += (target.x * FORM_SCALE - homeX) * e;
108 y += (target.y * FORM_SCALE - homeY) * e;
109 const accent = pigment(hue + (variant * 4 - 2), clamp(0.56 + target.lit * 0.38, 0.50, 0.93), 0.78);
110 color = base.map((v, k) => v + (accent[k]! - v) * e);
111 alpha += (clamp(0.42 + 0.56 * target.fade) - alpha) * e;
112 }
113
114 points.push([clamp(x, -0.94, 0.94), clamp(y, -0.94, 0.94)]);
115 materials.push([...color.map(Math.round), alpha]);
116 }
117 return { points, materials, morph, bodyStyle };
118 }
119
120 export function blendDotFields(from: DotField, to: DotField, progress: number): DotField {
121 const m = ease(progress);
122 if (m >= 1) return to;
123 return {
124 morph: from.morph + (to.morph - from.morph) * m,
125 bodyStyle: to.bodyStyle,
126 points: to.points.map((p, i) => p.map((v, k) => from.points[i]![k]! + (v - from.points[i]![k]!) * m)),
127 materials: to.materials.map((p, i) => p.map((v, k) => from.materials[i]![k]! + (v - from.materials[i]![k]!) * m)),
128 };
129 }
130
130 lines TYPESCRIPT