dsh-quay-bot 1.0.2 → 1.0.4
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.
- package/README.md +28 -105
- package/botscreen/requirements.txt +1 -1
- package/client.js +3 -2
- package/cordis.patch.yml +1 -1
- package/host/api.js +4 -0
- package/host/remote.js +285 -8
- package/host/screen.js +22 -86
- package/package.json +35 -32
- package/remote/bin/dsh-remote.mjs +435 -0
- package/remote/package.json +27 -0
- package/remote/src/config.mjs +183 -0
- package/remote/src/crypto.mjs +220 -0
- package/remote/src/keys.mjs +226 -0
- package/remote/src/proxy.mjs +1028 -0
- package/remote/src/sessions.mjs +163 -0
- package/remote/src/shim.js +838 -0
- package/remote/src/supervisor.mjs +207 -0
- package/remote/src/tunnel.mjs +533 -0
- package/remote/src/upstream.mjs +403 -0
- package/remote/src/util.mjs +149 -0
- package/remote/src/ws.mjs +313 -0
- package/LICENSE +0 -21
- package/avatar/build.mjs +0 -81
- package/avatar/gallery/droid.json +0 -14
- package/avatar/gallery/ghost.json +0 -14
- package/avatar/gallery/grok-pod.json +0 -14
- package/avatar/gallery/guardian.json +0 -14
- package/avatar/gallery/mecha.json +0 -14
- package/avatar/gallery/mochi.json +0 -14
- package/avatar/gallery/neko.json +0 -14
- package/avatar/gallery/orbiter.json +0 -14
- package/avatar/gallery/relay.json +0 -14
- package/avatar/gallery/spark.json +0 -14
- package/avatar/src/bot/clay.ts +0 -135
- package/avatar/src/bot/effects.ts +0 -100
- package/avatar/src/bot/grok-bot.ts +0 -626
- package/avatar/src/bot/index.ts +0 -15
- package/avatar/src/bot/palettes.ts +0 -62
- package/avatar/src/bot/ring.ts +0 -219
- package/avatar/src/bot/scheduler.ts +0 -239
- package/avatar/src/bot/shapes.ts +0 -277
- package/avatar/src/bot/spring.ts +0 -196
- package/avatar/src/core/color.ts +0 -92
- package/avatar/src/core/eyes.ts +0 -297
- package/avatar/src/core/field.ts +0 -482
- package/avatar/src/core/frame.ts +0 -655
- package/avatar/src/core/fx.ts +0 -375
- package/avatar/src/core/index.ts +0 -49
- package/avatar/src/core/paint.ts +0 -331
- package/avatar/src/core/path.ts +0 -495
- package/avatar/src/core/pose.ts +0 -581
- package/avatar/src/core/svg.ts +0 -98
- package/avatar/src/core/types.ts +0 -153
- package/avatar/src/core/validate.ts +0 -296
- package/avatar/src/core/volume.ts +0 -681
- package/avatar/src/entry.ts +0 -260
- package/bin/doctor.mjs +0 -148
- package/botscreen/requirements-offline.txt +0 -11
- package/market/index.json +0 -41
package/avatar/src/bot/ring.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 48 点闭合眼环:表情变形的拓扑基础。
|
|
3
|
-
*
|
|
4
|
-
* 每只眼睛永远是 48 个顶点的闭合数组,索引 0 固定在 3 点钟方向,按屏幕顺时针
|
|
5
|
-
* (y 向下,角度递增)均匀排列。于是任意两种表情都能逐点插值,不会“拧麻花”。
|
|
6
|
-
*
|
|
7
|
-
* 坐标单位:viewBox 单位、以眼睛中心为原点、对应 `eyes.scale = 1`
|
|
8
|
-
* (中性眼约宽 4、高 11,与定义契约一致)。渲染器再乘以实际眼睛倍率。
|
|
9
|
-
*/
|
|
10
|
-
import type { AvatarMood } from '../core/types.ts';
|
|
11
|
-
|
|
12
|
-
export interface Pt {
|
|
13
|
-
x: number;
|
|
14
|
-
y: number;
|
|
15
|
-
}
|
|
16
|
-
export type EyeRing = Pt[];
|
|
17
|
-
export type EyePair = [EyeRing, EyeRing];
|
|
18
|
-
|
|
19
|
-
export const POINT_COUNT = 48;
|
|
20
|
-
const TAU = Math.PI * 2;
|
|
21
|
-
|
|
22
|
-
export interface RingParams {
|
|
23
|
-
/** 半宽、半高。 */
|
|
24
|
-
rx: number;
|
|
25
|
-
ry: number;
|
|
26
|
-
/** 超椭圆指数:2 = 椭圆,越大越方;默认 2.6(圆润的胶囊)。 */
|
|
27
|
-
n?: number;
|
|
28
|
-
/** 上半区压扁(0..1)。 */
|
|
29
|
-
squintTop?: number;
|
|
30
|
-
/** 下半区压扁(0..1)。 */
|
|
31
|
-
squintBottom?: number;
|
|
32
|
-
/** 抛物线弯曲:y += bend·(x/rx)²;正值两端下垂,读作“^”形笑眼。 */
|
|
33
|
-
bend?: number;
|
|
34
|
-
/** 上眼睑切线:y = lid.y + lid.slope·x,线以上的点压到线上。 */
|
|
35
|
-
lid?: { y: number; slope?: number };
|
|
36
|
-
/** 下眼睑切线:线以下的点压到线上。 */
|
|
37
|
-
floor?: { y: number; slope?: number };
|
|
38
|
-
/** 斜切:x += shear·y。 */
|
|
39
|
-
shear?: number;
|
|
40
|
-
/** 旋转(弧度,正 = 顺时针)。 */
|
|
41
|
-
tilt?: number;
|
|
42
|
-
/** 整体平移。 */
|
|
43
|
-
dx?: number;
|
|
44
|
-
dy?: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** 按角度 θᵢ = 2πi/48 采样超椭圆,再依次施加表情修饰。 */
|
|
48
|
-
export function createEyeRing(p: RingParams): EyeRing {
|
|
49
|
-
const n = p.n ?? 2.6;
|
|
50
|
-
const ring: EyeRing = [];
|
|
51
|
-
for (let i = 0; i < POINT_COUNT; i++) {
|
|
52
|
-
const theta = (i / POINT_COUNT) * TAU;
|
|
53
|
-
const c = Math.cos(theta);
|
|
54
|
-
const s = Math.sin(theta);
|
|
55
|
-
let x = Math.sign(c) * Math.abs(c) ** (2 / n) * p.rx;
|
|
56
|
-
let y = Math.sign(s) * Math.abs(s) ** (2 / n) * p.ry;
|
|
57
|
-
if (y < 0 && p.squintTop) y *= 1 - p.squintTop;
|
|
58
|
-
if (y > 0 && p.squintBottom) y *= 1 - p.squintBottom;
|
|
59
|
-
if (p.bend) y += (p.bend * x * x) / (p.rx * p.rx);
|
|
60
|
-
if (p.lid) {
|
|
61
|
-
const line = p.lid.y + (p.lid.slope ?? 0) * x;
|
|
62
|
-
if (y < line) y = line;
|
|
63
|
-
}
|
|
64
|
-
if (p.floor) {
|
|
65
|
-
const line = p.floor.y + (p.floor.slope ?? 0) * x;
|
|
66
|
-
if (y > line) y = line;
|
|
67
|
-
}
|
|
68
|
-
if (p.shear) x += p.shear * y;
|
|
69
|
-
ring.push({ x, y });
|
|
70
|
-
}
|
|
71
|
-
return transform(ring, p.tilt ?? 0, p.dx ?? 0, p.dy ?? 0);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function transform(ring: EyeRing, tilt: number, dx: number, dy: number): EyeRing {
|
|
75
|
-
const c = Math.cos(tilt);
|
|
76
|
-
const s = Math.sin(tilt);
|
|
77
|
-
return ring.map(({ x, y }) => ({ x: x * c - y * s + dx, y: x * s + y * c + dy }));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 任意“关于原点星形”的闭合折线 → 48 点眼环:沿 θᵢ 方向发射线,取最远交点。
|
|
82
|
-
* 保证与参数化眼环同样的索引约定(0 = 3 点钟、顺时针)。
|
|
83
|
-
*/
|
|
84
|
-
export function ringFromPolygon(poly: Pt[], origin: Pt = { x: 0, y: 0 }): EyeRing {
|
|
85
|
-
const ring: EyeRing = [];
|
|
86
|
-
for (let i = 0; i < POINT_COUNT; i++) {
|
|
87
|
-
const theta = (i / POINT_COUNT) * TAU;
|
|
88
|
-
const ux = Math.cos(theta);
|
|
89
|
-
const uy = Math.sin(theta);
|
|
90
|
-
let best = 0;
|
|
91
|
-
for (let k = 0, j = poly.length - 1; k < poly.length; j = k++) {
|
|
92
|
-
const ax = poly[j].x - origin.x;
|
|
93
|
-
const ay = poly[j].y - origin.y;
|
|
94
|
-
const ex = poly[k].x - poly[j].x;
|
|
95
|
-
const ey = poly[k].y - poly[j].y;
|
|
96
|
-
const den = ux * ey - uy * ex;
|
|
97
|
-
if (Math.abs(den) < 1e-12) continue;
|
|
98
|
-
const t = (ax * ey - ay * ex) / den;
|
|
99
|
-
const u = (ax * uy - ay * ux) / den;
|
|
100
|
-
if (t > best && u >= 0 && u <= 1) best = t;
|
|
101
|
-
}
|
|
102
|
-
ring.push({ x: origin.x + ux * best, y: origin.y + uy * best });
|
|
103
|
-
}
|
|
104
|
-
return ring;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** 心形眼(参数方程采样后按角度重排),宽约 2·halfWidth。 */
|
|
108
|
-
export function heartRing(halfWidth: number, dy = 0): EyeRing {
|
|
109
|
-
const poly: Pt[] = [];
|
|
110
|
-
for (let k = 0; k < 160; k++) {
|
|
111
|
-
const t = (k / 160) * TAU;
|
|
112
|
-
const x = 16 * Math.sin(t) ** 3;
|
|
113
|
-
const y = -(13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
|
|
114
|
-
poly.push({ x: (x / 16) * halfWidth, y: (y / 16) * halfWidth + 0.1 * halfWidth });
|
|
115
|
-
}
|
|
116
|
-
return ringFromPolygon(poly).map((p) => ({ x: p.x, y: p.y + dy }));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 左右镜像,同时保持索引约定:镜像后第 i 点来自原环第 (N/2 − i) 点,
|
|
121
|
-
* 于是 0 号点仍在 3 点钟、方向仍是顺时针。
|
|
122
|
-
*/
|
|
123
|
-
export function mirrorRing(ring: EyeRing): EyeRing {
|
|
124
|
-
const out: EyeRing = [];
|
|
125
|
-
for (let i = 0; i < POINT_COUNT; i++) {
|
|
126
|
-
const p = ring[(((POINT_COUNT / 2 - i) % POINT_COUNT) + POINT_COUNT) % POINT_COUNT];
|
|
127
|
-
out.push({ x: -p.x, y: p.y });
|
|
128
|
-
}
|
|
129
|
-
return out;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** 左眼参数 → 一对(右眼镜像,于是“内角”语义左右一致)。 */
|
|
133
|
-
const pair = (left: RingParams, right?: RingParams): EyePair => [
|
|
134
|
-
createEyeRing(left),
|
|
135
|
-
right ? mirrorRing(createEyeRing(right)) : mirrorRing(createEyeRing(left)),
|
|
136
|
-
];
|
|
137
|
-
|
|
138
|
-
/** 15 种表情。所有参数写的是“左眼”,x 正方向 = 朝鼻梁的内角。 */
|
|
139
|
-
const LIBRARY: Record<AvatarMood, EyePair> = {
|
|
140
|
-
neutral: pair({ rx: 2.1, ry: 5.4 }),
|
|
141
|
-
happy: pair({ rx: 2.9, ry: 1.5, bend: 2.6, dy: -0.6 }),
|
|
142
|
-
joy: pair({ rx: 3.2, ry: 1.9, bend: 3.4, n: 2.2, dy: -0.9 }),
|
|
143
|
-
love: [heartRing(3.2, -0.2), mirrorRing(heartRing(3.2, -0.2))],
|
|
144
|
-
surprised: pair({ rx: 2.9, ry: 5.8, n: 2.15 }),
|
|
145
|
-
curious: pair({ rx: 2.4, ry: 5.8, n: 2.3 }, { rx: 2.0, ry: 4.6, squintTop: 0.25, dy: 0.5 }),
|
|
146
|
-
skeptical: pair({ rx: 2.2, ry: 5.2 }, { rx: 2.4, ry: 5.2, lid: { y: -0.9, slope: 0.15 } }),
|
|
147
|
-
sleepy: pair({ rx: 2.6, ry: 5.2, lid: { y: 1.6 } }),
|
|
148
|
-
bored: pair({ rx: 2.5, ry: 5.2, lid: { y: -0.5 }, squintBottom: 0.15 }),
|
|
149
|
-
sad: pair({ rx: 2.3, ry: 5.0, lid: { y: -1.6, slope: -0.75 }, dy: 0.6 }),
|
|
150
|
-
angry: pair({ rx: 2.5, ry: 5.2, lid: { y: -1.2, slope: 0.95 } }),
|
|
151
|
-
focused: pair({ rx: 2.8, ry: 5.4, squintTop: 0.5, squintBottom: 0.38 }),
|
|
152
|
-
confused: pair({ rx: 2.2, ry: 5.6, tilt: 0.18 }, { rx: 1.8, ry: 4.0, tilt: 0.28, dy: -0.8 }),
|
|
153
|
-
shy: pair({ rx: 2.0, ry: 3.5, squintTop: 0.2, dy: 1.8 }),
|
|
154
|
-
smug: pair({ rx: 2.6, ry: 4.6, lid: { y: -0.6, slope: -0.25 }, bend: -0.7, squintBottom: 0.25 }),
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
export const EXPRESSIONS: readonly AvatarMood[] = Object.keys(LIBRARY) as AvatarMood[];
|
|
158
|
-
|
|
159
|
-
/** 表情的目标眼环(深拷贝,可随意修改)。未知表情回落到 neutral(兼容历史 grumpy -> angry)。 */
|
|
160
|
-
export function expressionRings(mood: AvatarMood | string | undefined): EyePair {
|
|
161
|
-
const key = mood === 'grumpy' ? 'angry' : mood;
|
|
162
|
-
const src = LIBRARY[(key as AvatarMood) ?? 'neutral'] ?? LIBRARY.neutral;
|
|
163
|
-
return [src[0].map((p) => ({ ...p })), src[1].map((p) => ({ ...p }))];
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function lerpPair(a: EyePair, b: EyePair, t: number, out?: EyePair): EyePair {
|
|
167
|
-
const res: EyePair = out ?? [a[0].map((p) => ({ ...p })), a[1].map((p) => ({ ...p }))];
|
|
168
|
-
for (let e = 0; e < 2; e++) {
|
|
169
|
-
for (let i = 0; i < POINT_COUNT; i++) {
|
|
170
|
-
res[e][i].x = a[e][i].x + (b[e][i].x - a[e][i].x) * t;
|
|
171
|
-
res[e][i].y = a[e][i].y + (b[e][i].y - a[e][i].y) * t;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return res;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function clonePair(p: EyePair): EyePair {
|
|
178
|
-
return [p[0].map((q) => ({ ...q })), p[1].map((q) => ({ ...q }))];
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 所有表情的外包络(超椭圆,指数 ENVELOPE_N):校验器用它判断“眼睛无论换成哪种表情都不出界”,
|
|
183
|
-
* 渲染器用它估算眼睛的深度半宽。
|
|
184
|
-
*/
|
|
185
|
-
export const ENVELOPE_N = 2.6;
|
|
186
|
-
export const RING_ENVELOPE = (() => {
|
|
187
|
-
let hw = 0;
|
|
188
|
-
let hh = 0;
|
|
189
|
-
for (const p of Object.values(LIBRARY)) {
|
|
190
|
-
for (const ring of p) {
|
|
191
|
-
for (const q of ring) {
|
|
192
|
-
hw = Math.max(hw, Math.abs(q.x));
|
|
193
|
-
hh = Math.max(hh, Math.abs(q.y));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
// 超椭圆包络要比轴对齐的最大值略大才能装下斜向的点。
|
|
198
|
-
return { hw: hw * 1.06, hh: hh * 1.04 };
|
|
199
|
-
})();
|
|
200
|
-
|
|
201
|
-
const fmt = (n: number): string => {
|
|
202
|
-
const r = Math.round(n * 100) / 100;
|
|
203
|
-
return (Object.is(r, -0) ? 0 : r).toString();
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 眼环 → 光滑闭合路径(每个顶点一条二次曲线,在边中点相接)。
|
|
208
|
-
* `scale` 是眼睛倍率,`blink` 是纵向缩放(1 = 睁开,0.08 ≈ 闭上),`dx/dy` 是视线位移。
|
|
209
|
-
*/
|
|
210
|
-
export function ringPath(ring: EyeRing, cx: number, cy: number, scale: number, blink = 1, dx = 0, dy = 0): string {
|
|
211
|
-
const n = ring.length;
|
|
212
|
-
const X = (i: number) => cx + dx + ring[i % n].x * scale;
|
|
213
|
-
const Y = (i: number) => cy + dy + ring[i % n].y * scale * blink;
|
|
214
|
-
const mx = (i: number) => (X(i) + X(i + 1)) / 2;
|
|
215
|
-
const my = (i: number) => (Y(i) + Y(i + 1)) / 2;
|
|
216
|
-
let d = `M${fmt(mx(n - 1))} ${fmt(my(n - 1))}`;
|
|
217
|
-
for (let i = 0; i < n; i++) d += `Q${fmt(X(i))} ${fmt(Y(i))} ${fmt(mx(i))} ${fmt(my(i))}`;
|
|
218
|
-
return `${d}Z`;
|
|
219
|
-
}
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 双节奏状态机调度层(纯 TS,无 DOM / 定时器副作用)。
|
|
3
|
-
*
|
|
4
|
-
* 将“业务逻辑状态”与“动画表现节奏”解耦:
|
|
5
|
-
* 1. 表情切换定时器:按当前状态的间隔区间(如 thinking 3.5s..7.0s)从专属表情池随机抽新表情;
|
|
6
|
-
* 2. 独立眨眼定时器:按 2.0s..3.6s(随表情微调)独立触发眨眼;
|
|
7
|
-
* 3. 瞬时反应(poke)与手动锁定表情(setMood)均能无缝接入,驱动 SpringEyeEngine。
|
|
8
|
-
*/
|
|
9
|
-
import type { AvatarMood, AvatarState } from '../core/types.ts';
|
|
10
|
-
import { SpringEyeEngine, type SpringOptions } from './spring.ts';
|
|
11
|
-
|
|
12
|
-
export interface StateCadence {
|
|
13
|
-
pool: readonly AvatarMood[];
|
|
14
|
-
/** 表情切换区间(秒)。 */
|
|
15
|
-
moodInterval: [number, number];
|
|
16
|
-
/** 眨眼间隔区间(秒)。 */
|
|
17
|
-
blinkInterval: [number, number];
|
|
18
|
-
/** 眨眼时长(秒)。 */
|
|
19
|
-
blinkDuration: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const STATE_CADENCES: Record<AvatarState, StateCadence> = {
|
|
23
|
-
idle: {
|
|
24
|
-
pool: ['neutral', 'happy', 'curious', 'shy', 'sleepy'],
|
|
25
|
-
moodInterval: [3.2, 6.8],
|
|
26
|
-
blinkInterval: [2.2, 3.8],
|
|
27
|
-
blinkDuration: 0.15,
|
|
28
|
-
},
|
|
29
|
-
thinking: {
|
|
30
|
-
pool: ['curious', 'skeptical', 'focused', 'confused'],
|
|
31
|
-
moodInterval: [3.5, 7.0],
|
|
32
|
-
blinkInterval: [2.4, 4.2],
|
|
33
|
-
blinkDuration: 0.16,
|
|
34
|
-
},
|
|
35
|
-
working: {
|
|
36
|
-
pool: ['focused', 'smug', 'joy', 'neutral'],
|
|
37
|
-
moodInterval: [2.8, 5.5],
|
|
38
|
-
blinkInterval: [1.9, 3.2],
|
|
39
|
-
blinkDuration: 0.13,
|
|
40
|
-
},
|
|
41
|
-
waiting: {
|
|
42
|
-
pool: ['bored', 'sleepy', 'shy', 'curious'],
|
|
43
|
-
moodInterval: [3.8, 7.2],
|
|
44
|
-
blinkInterval: [2.8, 4.8],
|
|
45
|
-
blinkDuration: 0.22,
|
|
46
|
-
},
|
|
47
|
-
blocked: {
|
|
48
|
-
pool: ['sad', 'confused', 'angry'],
|
|
49
|
-
moodInterval: [2.4, 4.8],
|
|
50
|
-
blinkInterval: [2.2, 3.6],
|
|
51
|
-
blinkDuration: 0.15,
|
|
52
|
-
},
|
|
53
|
-
done: {
|
|
54
|
-
pool: ['joy', 'love', 'happy'],
|
|
55
|
-
moodInterval: [2.0, 4.2],
|
|
56
|
-
blinkInterval: [2.0, 3.4],
|
|
57
|
-
blinkDuration: 0.14,
|
|
58
|
-
},
|
|
59
|
-
alert: {
|
|
60
|
-
pool: ['surprised', 'angry', 'focused'],
|
|
61
|
-
moodInterval: [1.8, 3.5],
|
|
62
|
-
blinkInterval: [1.6, 2.8],
|
|
63
|
-
blinkDuration: 0.12,
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const POKE_POOL: readonly AvatarMood[] = ['surprised', 'love', 'shy', 'curious', 'joy', 'angry'];
|
|
68
|
-
|
|
69
|
-
export interface SchedulerOptions {
|
|
70
|
-
state?: AvatarState;
|
|
71
|
-
/** 若指定固定表情则锁定;传 null/undefined 时由状态机自动轮换表情。 */
|
|
72
|
-
mood?: AvatarMood | null;
|
|
73
|
-
spring?: SpringOptions;
|
|
74
|
-
/** 确定性伪随机源(默认 Math.random,测试可注入)。 */
|
|
75
|
-
rng?: () => number;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export class BotScheduler {
|
|
79
|
-
readonly eyes: SpringEyeEngine;
|
|
80
|
-
private _state: AvatarState;
|
|
81
|
-
private _lockedMood: AvatarMood | null;
|
|
82
|
-
private _activeMood: AvatarMood;
|
|
83
|
-
private _stateAge = 0;
|
|
84
|
-
|
|
85
|
-
private moodTimer = 0;
|
|
86
|
-
private blinkTimer = 0;
|
|
87
|
-
private burst: { mood: AvatarMood; remaining: number } | null = null;
|
|
88
|
-
private rng: () => number;
|
|
89
|
-
|
|
90
|
-
constructor(options: SchedulerOptions = {}) {
|
|
91
|
-
this.rng = options.rng ?? Math.random;
|
|
92
|
-
this._state = options.state ?? 'idle';
|
|
93
|
-
this._lockedMood = options.mood ?? null;
|
|
94
|
-
const cadence = STATE_CADENCES[this._state];
|
|
95
|
-
this._activeMood = this._lockedMood ?? cadence.pool[0];
|
|
96
|
-
this.eyes = new SpringEyeEngine(this._activeMood, options.spring);
|
|
97
|
-
this.moodTimer = this.randRange(cadence.moodInterval);
|
|
98
|
-
this.blinkTimer = this.randRange(cadence.blinkInterval);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
get state(): AvatarState {
|
|
102
|
-
return this._state;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
get lockedMood(): AvatarMood | null {
|
|
106
|
-
return this._lockedMood;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** 当前正在呈现的表情(含自动轮换或 poke 瞬时表情)。 */
|
|
110
|
-
get activeMood(): AvatarMood {
|
|
111
|
-
return this._activeMood;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
get stateAge(): number {
|
|
115
|
-
return this._stateAge;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private randRange([lo, hi]: [number, number]): number {
|
|
119
|
-
return lo + (hi - lo) * this.rng();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private pickFromPool(pool: readonly AvatarMood[], exclude?: AvatarMood): AvatarMood {
|
|
123
|
-
const candidates = pool.length > 1 && exclude ? pool.filter((m) => m !== exclude) : pool;
|
|
124
|
-
return candidates[Math.floor(this.rng() * candidates.length) % candidates.length];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
setState(next: AvatarState, immediate = false): void {
|
|
128
|
-
if (next === this._state) return;
|
|
129
|
-
this._state = next;
|
|
130
|
-
this._stateAge = 0;
|
|
131
|
-
const cadence = STATE_CADENCES[next];
|
|
132
|
-
this.moodTimer = this.randRange(cadence.moodInterval);
|
|
133
|
-
this.blinkTimer = Math.min(this.blinkTimer, this.randRange(cadence.blinkInterval));
|
|
134
|
-
if (!this._lockedMood && !this.burst) {
|
|
135
|
-
const picked = cadence.pool[0];
|
|
136
|
-
this._activeMood = picked;
|
|
137
|
-
this.eyes.setExpression(picked, immediate);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** 锁定某一个表情;传 `null` 恢复状态机自动轮换。 */
|
|
142
|
-
setMood(mood: AvatarMood | null, immediate = false): void {
|
|
143
|
-
this._lockedMood = mood;
|
|
144
|
-
if (mood !== null) {
|
|
145
|
-
this.burst = null;
|
|
146
|
-
this._activeMood = mood;
|
|
147
|
-
this.eyes.setExpression(mood, immediate);
|
|
148
|
-
} else {
|
|
149
|
-
const cadence = STATE_CADENCES[this._state];
|
|
150
|
-
const next = this.pickFromPool(cadence.pool, this._activeMood);
|
|
151
|
-
this._activeMood = next;
|
|
152
|
-
this.moodTimer = this.randRange(cadence.moodInterval);
|
|
153
|
-
this.eyes.setExpression(next, immediate);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** 外部手动设置注视目标(-1..1)。 */
|
|
158
|
-
setGazeTarget(x: number, y: number, immediate = false): void {
|
|
159
|
-
this.eyes.setLookTarget(x, y, immediate);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** “逗它一下”:触发一个短暂的随机表情反应,结束后平滑弹回。 */
|
|
163
|
-
poke(mood?: AvatarMood, durationSec = 1.6): AvatarMood {
|
|
164
|
-
const picked = mood ?? this.pickFromPool(POKE_POOL, this._activeMood);
|
|
165
|
-
this.burst = { mood: picked, remaining: Math.max(0.3, durationSec) };
|
|
166
|
-
this._activeMood = picked;
|
|
167
|
-
this.eyes.setExpression(picked);
|
|
168
|
-
return picked;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** 主动眨一次眼。 */
|
|
172
|
-
blink(doubleBlink = false): void {
|
|
173
|
-
const cadence = STATE_CADENCES[this._state];
|
|
174
|
-
this.eyes.triggerBlink(doubleBlink, cadence.blinkDuration);
|
|
175
|
-
this.blinkTimer = this.randRange(cadence.blinkInterval);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** 推进调度器与弹簧物理(`dt` 秒)。 */
|
|
179
|
-
update(dt: number, reducedMotion = false): void {
|
|
180
|
-
const clamped = Math.min(0.05, Math.max(0, Number.isFinite(dt) ? dt : 0));
|
|
181
|
-
this._stateAge += clamped;
|
|
182
|
-
const cadence = STATE_CADENCES[this._state];
|
|
183
|
-
|
|
184
|
-
if (!reducedMotion) {
|
|
185
|
-
// 1. 瞬时反应(逗它一下)倒计时
|
|
186
|
-
if (this.burst) {
|
|
187
|
-
this.burst.remaining -= clamped;
|
|
188
|
-
if (this.burst.remaining <= 0) {
|
|
189
|
-
this.burst = null;
|
|
190
|
-
const fallback = this._lockedMood ?? this.pickFromPool(cadence.pool);
|
|
191
|
-
this._activeMood = fallback;
|
|
192
|
-
this.eyes.setExpression(fallback);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 2. 表情池定时轮换(未锁定且无 poke 时)
|
|
197
|
-
if (!this._lockedMood && !this.burst) {
|
|
198
|
-
this.moodTimer -= clamped;
|
|
199
|
-
if (this.moodTimer <= 0) {
|
|
200
|
-
this.moodTimer = this.randRange(cadence.moodInterval);
|
|
201
|
-
const next = this.pickFromPool(cadence.pool, this._activeMood);
|
|
202
|
-
this._activeMood = next;
|
|
203
|
-
this.eyes.setExpression(next);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 3. 独立眨眼调度
|
|
208
|
-
this.blinkTimer -= clamped;
|
|
209
|
-
if (this.blinkTimer <= 0) {
|
|
210
|
-
this.blinkTimer = this.randRange(cadence.blinkInterval);
|
|
211
|
-
const doubleBlink = this.rng() > 0.82;
|
|
212
|
-
this.eyes.triggerBlink(doubleBlink, cadence.blinkDuration);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
this.eyes.update(clamped, reducedMotion);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 计算距离下一次定时器事件(眨眼、表情轮换、burst 结束)还剩多少毫秒。
|
|
221
|
-
* 配合 `eyes.isSettled()` 用于 Idle 静止降帧休眠。
|
|
222
|
-
*/
|
|
223
|
-
nextWakeDelayMs(): number {
|
|
224
|
-
const timers: number[] = [this.blinkTimer];
|
|
225
|
-
if (!this._lockedMood && !this.burst) timers.push(this.moodTimer);
|
|
226
|
-
if (this.burst) timers.push(this.burst.remaining);
|
|
227
|
-
const minS = Math.max(0.04, Math.min(...timers));
|
|
228
|
-
return Math.round(minS * 1000);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/** 休眠唤醒:直接把最接近到期的定时器和内部时钟快进 `elapsedSec` 秒。 */
|
|
232
|
-
advanceSleep(elapsedSec: number): void {
|
|
233
|
-
if (elapsedSec <= 0) return;
|
|
234
|
-
this._stateAge += elapsedSec;
|
|
235
|
-
if (this.burst) this.burst.remaining = Math.max(0, this.burst.remaining - elapsedSec);
|
|
236
|
-
if (!this._lockedMood && !this.burst) this.moodTimer = Math.max(0, this.moodTimer - elapsedSec);
|
|
237
|
-
this.blinkTimer = Math.max(0, this.blinkTimer - elapsedSec);
|
|
238
|
-
}
|
|
239
|
-
}
|