@holtjs/stage 0.0.1 → 0.4.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.
- package/CHANGELOG.md +82 -0
- package/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +173 -0
- package/dist/audio.d.ts +159 -0
- package/dist/audio.js +781 -0
- package/dist/ecs.d.ts +172 -0
- package/dist/ecs.js +537 -0
- package/dist/flex.d.ts +113 -0
- package/dist/flex.js +426 -0
- package/dist/game-C6NrnZ7-.d.ts +1799 -0
- package/dist/game-CbmA4myj.js +5899 -0
- package/dist/headless.d.ts +154 -0
- package/dist/headless.js +356 -0
- package/dist/hit.d.ts +63 -0
- package/dist/hit.js +185 -0
- package/dist/index.d.ts +314 -0
- package/dist/index.js +49 -0
- package/dist/joystick.d.ts +233 -0
- package/dist/joystick.js +763 -0
- package/dist/kernel.d.ts +2 -0
- package/dist/kernel.js +639 -0
- package/dist/nav.d.ts +119 -0
- package/dist/nav.js +342 -0
- package/dist/particles.d.ts +219 -0
- package/dist/particles.js +478 -0
- package/dist/renderer-webgpu-Bvvi5jN8.js +2618 -0
- package/dist/renderer-webgpu.d.ts +2 -0
- package/dist/renderer-webgpu.js +1 -0
- package/dist/spatial.d.ts +70 -0
- package/dist/spatial.js +165 -0
- package/dist/surfaces.d.ts +209 -0
- package/dist/surfaces.js +567 -0
- package/dist/trace.d.ts +2 -0
- package/dist/trace.js +247 -0
- package/dist/tween-BiF1FUvc.d.ts +93 -0
- package/dist/tween.d.ts +2 -0
- package/dist/tween.js +316 -0
- package/dist/types-DRFB0Klc.js +168 -0
- package/llms.txt +40 -0
- package/package.json +118 -1
- package/skills/stage/SKILL.md +272 -0
- package/skills/stage/references/subpaths.md +82 -0
package/dist/trace.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import "./types-DRFB0Klc.js";
|
|
2
|
+
// src/trace.ts
|
|
3
|
+
const TRACK_TOKEN_RE = /^[\w:.\-/>]+$/;
|
|
4
|
+
const MAX_KEY_LEN = 64;
|
|
5
|
+
const MAX_ALT_LEN = 64;
|
|
6
|
+
const DEFAULT_MAX_NODES = 120;
|
|
7
|
+
const MAX_SURFACE_ENTRIES = 32;
|
|
8
|
+
/** Sanitize a privacy-safe semantic identifier (`trackId`, `role`, `surface`). Returns null if invalid or empty. */
|
|
9
|
+
function sanitizeTrackToken(raw, maxLen = MAX_KEY_LEN) {
|
|
10
|
+
if (typeof raw !== "string") return null;
|
|
11
|
+
const trimmed = raw.trim();
|
|
12
|
+
if (!trimmed) return null;
|
|
13
|
+
const cleaned = trimmed.replace(/[^\w:.\-/>]/g, "").slice(0, maxLen);
|
|
14
|
+
return cleaned && TRACK_TOKEN_RE.test(cleaned) ? cleaned : null;
|
|
15
|
+
}
|
|
16
|
+
function sanitizeAlt(raw) {
|
|
17
|
+
if (typeof raw !== "string") return null;
|
|
18
|
+
const cleaned = raw.replace(/\s+/g, " ").trim();
|
|
19
|
+
return cleaned ? cleaned.slice(0, MAX_ALT_LEN) : null;
|
|
20
|
+
}
|
|
21
|
+
function roundBounds(x, y, w, h) {
|
|
22
|
+
return [
|
|
23
|
+
Math.round(x),
|
|
24
|
+
Math.round(y),
|
|
25
|
+
Math.round(w),
|
|
26
|
+
Math.round(h)
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
function sanitizeSurfaceEntry(raw) {
|
|
30
|
+
if (!raw || typeof raw !== "object") return null;
|
|
31
|
+
const id = sanitizeTrackToken(raw.id);
|
|
32
|
+
if (!id) return null;
|
|
33
|
+
return {
|
|
34
|
+
id,
|
|
35
|
+
priority: typeof raw.priority === "number" && Number.isFinite(raw.priority) ? raw.priority : 0,
|
|
36
|
+
seq: typeof raw.seq === "number" && Number.isFinite(raw.seq) ? raw.seq : 0,
|
|
37
|
+
scope: sanitizeTrackToken(raw.scope, 16) ?? "global",
|
|
38
|
+
barrier: sanitizeTrackToken(raw.barrier, 16) ?? "none",
|
|
39
|
+
parent: sanitizeTrackToken(raw.parent)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function sanitizeSurfaces(src) {
|
|
43
|
+
if (!src) return null;
|
|
44
|
+
const activeRaw = typeof src.active === "function" ? src.active() : [];
|
|
45
|
+
const active = [];
|
|
46
|
+
if (Array.isArray(activeRaw)) {
|
|
47
|
+
const cap = Math.min(activeRaw.length, MAX_SURFACE_ENTRIES);
|
|
48
|
+
for (let i = 0; i < cap; i++) {
|
|
49
|
+
const entry = sanitizeSurfaceEntry(activeRaw[i]);
|
|
50
|
+
if (entry) active.push(entry);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const topBarrier = typeof src.topBarrier === "function" ? sanitizeSurfaceEntry(src.topBarrier()) : null;
|
|
54
|
+
const worldBlocked = typeof src.isWorldBlocked === "function" ? Boolean(src.isWorldBlocked()) : false;
|
|
55
|
+
let domZ = null;
|
|
56
|
+
if (typeof src.domZ === "function") {
|
|
57
|
+
const rawZ = src.domZ();
|
|
58
|
+
if (rawZ && typeof rawZ === "object") {
|
|
59
|
+
const out = {};
|
|
60
|
+
let count = 0;
|
|
61
|
+
for (const [k, v] of Object.entries(rawZ)) {
|
|
62
|
+
if (count >= MAX_SURFACE_ENTRIES) break;
|
|
63
|
+
const sk = sanitizeTrackToken(k);
|
|
64
|
+
if (sk && typeof v === "number" && Number.isFinite(v)) {
|
|
65
|
+
out[sk] = Math.round(v);
|
|
66
|
+
count++;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
domZ = out;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
let activeFocus = null;
|
|
73
|
+
if (typeof src.activeFocus === "function") {
|
|
74
|
+
const rawFocus = src.activeFocus();
|
|
75
|
+
if (rawFocus && typeof rawFocus === "object") {
|
|
76
|
+
const tag = sanitizeTrackToken(rawFocus.tag, 24);
|
|
77
|
+
if (tag) activeFocus = {
|
|
78
|
+
tag,
|
|
79
|
+
ui: sanitizeTrackToken(rawFocus.ui),
|
|
80
|
+
role: sanitizeTrackToken(rawFocus.role, 32)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
active,
|
|
86
|
+
topBarrier,
|
|
87
|
+
worldBlocked,
|
|
88
|
+
domZ,
|
|
89
|
+
activeFocus
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function nonNegInt(v) {
|
|
93
|
+
return typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.round(v) : 0;
|
|
94
|
+
}
|
|
95
|
+
function sanitizeSandbox(src) {
|
|
96
|
+
const raw = typeof src === "function" ? src() : src;
|
|
97
|
+
if (!raw || typeof raw !== "object") return null;
|
|
98
|
+
let engine = null;
|
|
99
|
+
if (raw.engine && typeof raw.engine === "object") {
|
|
100
|
+
const modId = sanitizeTrackToken(raw.engine.modId);
|
|
101
|
+
if (modId) engine = {
|
|
102
|
+
modId,
|
|
103
|
+
ready: Boolean(raw.engine.ready),
|
|
104
|
+
uptimeMs: nonNegInt(raw.engine.uptimeMs),
|
|
105
|
+
renderer: sanitizeTrackToken(raw.engine.renderer, 16),
|
|
106
|
+
apps: nonNegInt(raw.engine.apps),
|
|
107
|
+
frames: nonNegInt(raw.engine.frames),
|
|
108
|
+
longFrames: nonNegInt(raw.engine.longFrames),
|
|
109
|
+
drawCalls: nonNegInt(raw.engine.drawCalls),
|
|
110
|
+
texBinds: nonNegInt(raw.engine.texBinds),
|
|
111
|
+
errors: nonNegInt(raw.engine.errors),
|
|
112
|
+
violations: nonNegInt(raw.engine.violations),
|
|
113
|
+
lastHeartbeatMs: typeof raw.engine.lastHeartbeatMs === "number" && Number.isFinite(raw.engine.lastHeartbeatMs) && raw.engine.lastHeartbeatMs >= 0 ? Math.round(raw.engine.lastHeartbeatMs) : null
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
engine,
|
|
118
|
+
mountedWorkers: nonNegInt(raw.mountedWorkers)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Build a privacy-safe structural snapshot of a Stage2D scene and optional UiRoot.
|
|
123
|
+
* Never reads arbitrary `TextObject.text` or user-typed strings; only emits sanitized `trackId`/`role`/`surface`
|
|
124
|
+
* tokens and explicitly declared semantic `alt` labels.
|
|
125
|
+
*/
|
|
126
|
+
function buildSceneDigest(scene, opts = {}) {
|
|
127
|
+
const maxNodes = Math.max(1, opts.maxNodes ?? DEFAULT_MAX_NODES);
|
|
128
|
+
const nodes = [];
|
|
129
|
+
const seenTrackIds = /* @__PURE__ */ new Set();
|
|
130
|
+
let truncated = false;
|
|
131
|
+
let uiCount = 0;
|
|
132
|
+
const pushNode = (n) => {
|
|
133
|
+
if (nodes.length >= maxNodes) {
|
|
134
|
+
truncated = true;
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
nodes.push(n);
|
|
138
|
+
return true;
|
|
139
|
+
};
|
|
140
|
+
const uiList = opts.ui?.registry?.api?.list?.();
|
|
141
|
+
if (Array.isArray(uiList)) {
|
|
142
|
+
uiCount = uiList.length;
|
|
143
|
+
for (let i = 0; i < uiList.length; i++) {
|
|
144
|
+
const info = uiList[i];
|
|
145
|
+
if (!info) continue;
|
|
146
|
+
const trackId = sanitizeTrackToken(info.trackId ?? info.id);
|
|
147
|
+
const role = sanitizeTrackToken(info.role ?? info.kind, 32);
|
|
148
|
+
const alt = sanitizeAlt(info.alt);
|
|
149
|
+
const surface = sanitizeTrackToken(info.surface, 32);
|
|
150
|
+
if (trackId) seenTrackIds.add(trackId);
|
|
151
|
+
const b = info.bounds ?? {
|
|
152
|
+
x: 0,
|
|
153
|
+
y: 0,
|
|
154
|
+
w: 0,
|
|
155
|
+
h: 0
|
|
156
|
+
};
|
|
157
|
+
if (!pushNode({
|
|
158
|
+
trackId,
|
|
159
|
+
role,
|
|
160
|
+
alt,
|
|
161
|
+
surface,
|
|
162
|
+
type: "UiElement",
|
|
163
|
+
visible: Boolean(info.visible),
|
|
164
|
+
enabled: info.enabled !== false,
|
|
165
|
+
zIndex: typeof info.zIndex === "number" ? info.zIndex : 0,
|
|
166
|
+
depth: typeof info.depth === "number" ? info.depth : 0,
|
|
167
|
+
alpha: typeof info.alpha === "number" ? +info.alpha.toFixed(2) : 1,
|
|
168
|
+
bounds: roundBounds(b.x || 0, b.y || 0, b.w || 0, b.h || 0)
|
|
169
|
+
})) break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const walk = (list, parentVisible, parentAlpha, px, py) => {
|
|
173
|
+
for (let i = 0; i < list.length; i++) {
|
|
174
|
+
if (nodes.length >= maxNodes) {
|
|
175
|
+
truncated = true;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const o = list[i];
|
|
179
|
+
if (!o || !o.active) continue;
|
|
180
|
+
const vis = parentVisible && o.visible;
|
|
181
|
+
const alpha = parentAlpha * o.alpha;
|
|
182
|
+
const wx = px + o.x;
|
|
183
|
+
const wy = py + o.y;
|
|
184
|
+
if (o.trackId || o.role || o.alt) {
|
|
185
|
+
const trackId = sanitizeTrackToken(o.trackId);
|
|
186
|
+
if (!trackId || !seenTrackIds.has(trackId)) {
|
|
187
|
+
if (trackId) seenTrackIds.add(trackId);
|
|
188
|
+
pushNode({
|
|
189
|
+
trackId,
|
|
190
|
+
role: sanitizeTrackToken(o.role, 32),
|
|
191
|
+
alt: sanitizeAlt(o.alt),
|
|
192
|
+
surface: sanitizeTrackToken(o.surface, 32),
|
|
193
|
+
type: o.type,
|
|
194
|
+
visible: vis && alpha > 0,
|
|
195
|
+
enabled: o.uiEl ? Boolean(o.uiEl.enabled) : true,
|
|
196
|
+
zIndex: o.zIndex || 0,
|
|
197
|
+
depth: o.depth || 0,
|
|
198
|
+
alpha: +alpha.toFixed(2),
|
|
199
|
+
bounds: roundBounds(wx - o.displayOriginX * Math.abs(o.scaleX), wy - o.displayOriginY * Math.abs(o.scaleY), o.displayWidth, o.displayHeight)
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (o.type === "Container") walk(o.list, vis, alpha, wx, wy);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
walk(scene.children.list, true, 1, 0, 0);
|
|
207
|
+
const r = scene.renderer ?? scene.game?.renderer;
|
|
208
|
+
const rendererType = r?.type === 3 ? "webgpu" : r?.type === 2 ? r.isWebGL2 ? "webgl2" : "webgl" : "canvas2d";
|
|
209
|
+
const rStats = typeof r?.stats === "function" ? r.stats() : null;
|
|
210
|
+
const dpr = opts.dpr ?? opts.ui?.dpr ?? scene.dpr ?? 1;
|
|
211
|
+
const vw = opts.ui?.view?.w ?? Math.round((scene.scale?.width ?? 0) / (dpr || 1));
|
|
212
|
+
const vh = opts.ui?.view?.h ?? Math.round((scene.scale?.height ?? 0) / (dpr || 1));
|
|
213
|
+
return {
|
|
214
|
+
clock: scene.clock ? scene.clock.snapshot() : scene.game?.clock?.snapshot() ?? null,
|
|
215
|
+
traceId: opts.traceId ?? null,
|
|
216
|
+
viewport: {
|
|
217
|
+
w: Math.round(vw),
|
|
218
|
+
h: Math.round(vh),
|
|
219
|
+
dpr
|
|
220
|
+
},
|
|
221
|
+
renderer: rStats ? {
|
|
222
|
+
type: rendererType,
|
|
223
|
+
drawCalls: rStats.drawCalls,
|
|
224
|
+
flushes: rStats.flushes,
|
|
225
|
+
tris: rStats.tris,
|
|
226
|
+
culled: rStats.culled,
|
|
227
|
+
texBinds: rStats.texBinds,
|
|
228
|
+
sdfPanels: rStats.sdfPanels,
|
|
229
|
+
sdfDraws: rStats.sdfDraws,
|
|
230
|
+
msdfIcons: rStats.msdfIcons,
|
|
231
|
+
msdfDraws: rStats.msdfDraws,
|
|
232
|
+
pixelAaIcons: rStats.pixelAaIcons,
|
|
233
|
+
pixelAaDraws: rStats.pixelAaDraws
|
|
234
|
+
} : { type: rendererType },
|
|
235
|
+
lifecycle: typeof scene.lifecycleStats === "function" ? scene.lifecycleStats() : null,
|
|
236
|
+
tweens: typeof scene.tweenStats === "function" ? scene.tweenStats() : null,
|
|
237
|
+
overlays: typeof opts.ui?.__overlays?.stats === "function" ? opts.ui.__overlays.stats() : null,
|
|
238
|
+
surfaces: sanitizeSurfaces(opts.surfaces),
|
|
239
|
+
sandbox: sanitizeSandbox(opts.sandbox),
|
|
240
|
+
plugins: typeof scene.game?.kernel?.collectPluginDigests === "function" ? scene.game.kernel.collectPluginDigests() : null,
|
|
241
|
+
displayListCount: scene.children.list.length,
|
|
242
|
+
uiCount,
|
|
243
|
+
truncated,
|
|
244
|
+
nodes
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
export { buildSceneDigest, sanitizeTrackToken };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
type EasingFn = (t: number) => number;
|
|
2
|
+
declare const Easings: {
|
|
3
|
+
readonly linear: (t: number) => number;
|
|
4
|
+
readonly inQuad: (t: number) => number;
|
|
5
|
+
readonly outQuad: (t: number) => number;
|
|
6
|
+
readonly inOutQuad: (t: number) => number;
|
|
7
|
+
readonly inCubic: (t: number) => number;
|
|
8
|
+
readonly outCubic: (t: number) => number;
|
|
9
|
+
readonly inOutCubic: (t: number) => number;
|
|
10
|
+
readonly outQuart: (t: number) => number;
|
|
11
|
+
readonly inOutSine: (t: number) => number;
|
|
12
|
+
readonly outBack: (t: number) => number;
|
|
13
|
+
};
|
|
14
|
+
type EasingName = keyof typeof Easings;
|
|
15
|
+
/** Any Stage2D `DisplayObject`, `UiElement`, or custom lifecycle scope supporting `onDispose()`. */
|
|
16
|
+
interface DisposableOwner {
|
|
17
|
+
readonly active?: boolean;
|
|
18
|
+
readonly destroyed?: boolean;
|
|
19
|
+
onDispose?(fn: () => void): () => void;
|
|
20
|
+
}
|
|
21
|
+
interface TweenOptions {
|
|
22
|
+
from: number;
|
|
23
|
+
to: number;
|
|
24
|
+
durationMs: number;
|
|
25
|
+
delayMs?: number;
|
|
26
|
+
ease?: EasingName | EasingFn;
|
|
27
|
+
owner?: DisposableOwner | null;
|
|
28
|
+
onUpdate: (value: number, progress: number) => void;
|
|
29
|
+
onComplete?: () => void;
|
|
30
|
+
}
|
|
31
|
+
interface SpringOptions {
|
|
32
|
+
from: number;
|
|
33
|
+
to: number;
|
|
34
|
+
velocity?: number;
|
|
35
|
+
/** Spring stiffness `k` (default `280`). */
|
|
36
|
+
stiffness?: number;
|
|
37
|
+
/** Viscous damping `c` (default `26`; critical damping at k=280, m=1 is ~33.5). */
|
|
38
|
+
damping?: number;
|
|
39
|
+
/** Oscillator mass `m` (default `1`). */
|
|
40
|
+
mass?: number;
|
|
41
|
+
/** Positional settle threshold (default `0.01`). */
|
|
42
|
+
precision?: number;
|
|
43
|
+
/** Velocity settle threshold in units/s (default `0.05`). */
|
|
44
|
+
velocityPrecision?: number;
|
|
45
|
+
owner?: DisposableOwner | null;
|
|
46
|
+
onUpdate: (value: number, velocity: number) => void;
|
|
47
|
+
onComplete?: () => void;
|
|
48
|
+
}
|
|
49
|
+
interface TweenHandle {
|
|
50
|
+
readonly active: boolean;
|
|
51
|
+
readonly value: number;
|
|
52
|
+
cancel(): void;
|
|
53
|
+
finish(): void;
|
|
54
|
+
}
|
|
55
|
+
interface SpringHandle extends TweenHandle {
|
|
56
|
+
readonly velocity: number;
|
|
57
|
+
readonly target: number;
|
|
58
|
+
/** Smoothly retarget the spring mid-flight (or wake a previously settled spring if not cancelled/disposed). */
|
|
59
|
+
setTarget(nextTo: number): void;
|
|
60
|
+
}
|
|
61
|
+
interface TweenStats {
|
|
62
|
+
readonly active: number;
|
|
63
|
+
readonly started: number;
|
|
64
|
+
readonly completed: number;
|
|
65
|
+
readonly cancelled: number;
|
|
66
|
+
readonly errors: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Scene-owned deterministic animation scheduler stepped strictly from `EngineClock.lastDeltaMs`.
|
|
70
|
+
* Supports duration-based eased tweens and damped harmonic springs with automatic `owner.onDispose()` cleanup.
|
|
71
|
+
*/
|
|
72
|
+
declare class TweenManager {
|
|
73
|
+
private readonly entries;
|
|
74
|
+
private _started;
|
|
75
|
+
private _completed;
|
|
76
|
+
private _cancelled;
|
|
77
|
+
private _errors;
|
|
78
|
+
get hasActive(): boolean;
|
|
79
|
+
get activeCount(): number;
|
|
80
|
+
stats(): TweenStats;
|
|
81
|
+
private safeCall;
|
|
82
|
+
/** Start a deterministic duration-based eased tween. */
|
|
83
|
+
to(opts: TweenOptions): TweenHandle;
|
|
84
|
+
/** Alias for `to(opts)` so Phaser/KAPLAY-style `tweens.add(...)` and `tweens.to(...)` both work seamlessly. */
|
|
85
|
+
add(opts: TweenOptions): TweenHandle;
|
|
86
|
+
/** Start a sub-stepped damped harmonic spring with smooth mid-flight retargeting. */
|
|
87
|
+
spring(opts: SpringOptions): SpringHandle;
|
|
88
|
+
/** Advance all active tweens & springs by a clamped EngineClock delta in ms. */
|
|
89
|
+
step(deltaMs: number): void;
|
|
90
|
+
/** Cancel and clear all active tweens & springs (called during `Scene._destroy()`). */
|
|
91
|
+
clear(): void;
|
|
92
|
+
}
|
|
93
|
+
export { SpringHandle as a, TweenManager as c, Easings as i, TweenOptions as l, EasingFn as n, SpringOptions as o, EasingName as r, TweenHandle as s, DisposableOwner as t, TweenStats as u };
|
package/dist/tween.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as SpringHandle, c as TweenManager, i as Easings, l as TweenOptions, n as EasingFn, o as SpringOptions, r as EasingName, s as TweenHandle, t as DisposableOwner, u as TweenStats } from "./tween-BiF1FUvc.js";
|
|
2
|
+
export { DisposableOwner, EasingFn, EasingName, Easings, SpringHandle, SpringOptions, TweenHandle, TweenManager, TweenOptions, TweenStats };
|
package/dist/tween.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// src/tween.ts
|
|
2
|
+
const Easings = {
|
|
3
|
+
linear: (t) => t,
|
|
4
|
+
inQuad: (t) => t * t,
|
|
5
|
+
outQuad: (t) => 1 - (1 - t) * (1 - t),
|
|
6
|
+
inOutQuad: (t) => t < .5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2,
|
|
7
|
+
inCubic: (t) => t * t * t,
|
|
8
|
+
outCubic: (t) => 1 - Math.pow(1 - t, 3),
|
|
9
|
+
inOutCubic: (t) => t < .5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2,
|
|
10
|
+
outQuart: (t) => 1 - Math.pow(1 - t, 4),
|
|
11
|
+
inOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
|
|
12
|
+
outBack: (t) => {
|
|
13
|
+
return 1 + 2.70158 * Math.pow(t - 1, 3) + 1.70158 * Math.pow(t - 1, 2);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const MAX_FRAME_DELTA_MS = 100;
|
|
17
|
+
const MAX_SPRING_SUBSTEP_MS = 8;
|
|
18
|
+
function isOwnerDead(owner) {
|
|
19
|
+
if (!owner) return false;
|
|
20
|
+
return owner.destroyed === true || owner.active === false;
|
|
21
|
+
}
|
|
22
|
+
function resolveEasing(ease) {
|
|
23
|
+
if (typeof ease === "function") return ease;
|
|
24
|
+
if (ease && ease in Easings) return Easings[ease];
|
|
25
|
+
return Easings.outCubic;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Scene-owned deterministic animation scheduler stepped strictly from `EngineClock.lastDeltaMs`.
|
|
29
|
+
* Supports duration-based eased tweens and damped harmonic springs with automatic `owner.onDispose()` cleanup.
|
|
30
|
+
*/
|
|
31
|
+
var TweenManager = class {
|
|
32
|
+
entries = /* @__PURE__ */ new Set();
|
|
33
|
+
_started = 0;
|
|
34
|
+
_completed = 0;
|
|
35
|
+
_cancelled = 0;
|
|
36
|
+
_errors = 0;
|
|
37
|
+
get hasActive() {
|
|
38
|
+
return this.entries.size > 0;
|
|
39
|
+
}
|
|
40
|
+
get activeCount() {
|
|
41
|
+
return this.entries.size;
|
|
42
|
+
}
|
|
43
|
+
stats() {
|
|
44
|
+
return {
|
|
45
|
+
active: this.entries.size,
|
|
46
|
+
started: this._started,
|
|
47
|
+
completed: this._completed,
|
|
48
|
+
cancelled: this._cancelled,
|
|
49
|
+
errors: this._errors
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
safeCall(fn) {
|
|
53
|
+
try {
|
|
54
|
+
fn();
|
|
55
|
+
return true;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
this._errors++;
|
|
58
|
+
console.error("Stage2D tween callback error", err);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Start a deterministic duration-based eased tween. */
|
|
63
|
+
to(opts) {
|
|
64
|
+
const from = Number.isFinite(opts.from) ? opts.from : 0;
|
|
65
|
+
const to = Number.isFinite(opts.to) ? opts.to : from;
|
|
66
|
+
const durationMs = Math.max(0, Number.isFinite(opts.durationMs) ? opts.durationMs : 0);
|
|
67
|
+
let delayLeft = Math.max(0, Number.isFinite(opts.delayMs) ? opts.delayMs ?? 0 : 0);
|
|
68
|
+
const ease = resolveEasing(opts.ease);
|
|
69
|
+
const owner = opts.owner ?? null;
|
|
70
|
+
if (isOwnerDead(owner)) return {
|
|
71
|
+
get active() {
|
|
72
|
+
return false;
|
|
73
|
+
},
|
|
74
|
+
get value() {
|
|
75
|
+
return from;
|
|
76
|
+
},
|
|
77
|
+
cancel() {},
|
|
78
|
+
finish() {}
|
|
79
|
+
};
|
|
80
|
+
let value = from;
|
|
81
|
+
let elapsed = 0;
|
|
82
|
+
let detachOwner = null;
|
|
83
|
+
const detach = () => {
|
|
84
|
+
if (detachOwner) {
|
|
85
|
+
const fn = detachOwner;
|
|
86
|
+
detachOwner = null;
|
|
87
|
+
fn();
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const completeEntry = (entry) => {
|
|
91
|
+
if (!entry.active) return;
|
|
92
|
+
entry.active = false;
|
|
93
|
+
this.entries.delete(entry);
|
|
94
|
+
detach();
|
|
95
|
+
this._completed++;
|
|
96
|
+
value = to;
|
|
97
|
+
if (this.safeCall(() => opts.onUpdate(to, 1)) && opts.onComplete) this.safeCall(() => opts.onComplete?.());
|
|
98
|
+
};
|
|
99
|
+
const entry = {
|
|
100
|
+
kind: "tween",
|
|
101
|
+
active: true,
|
|
102
|
+
step: (dtMs) => {
|
|
103
|
+
if (!entry.active) return false;
|
|
104
|
+
if (isOwnerDead(owner)) {
|
|
105
|
+
entry.cancel();
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
let rem = dtMs;
|
|
109
|
+
if (delayLeft > 0) {
|
|
110
|
+
if (rem < delayLeft) {
|
|
111
|
+
delayLeft -= rem;
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
rem -= delayLeft;
|
|
115
|
+
delayLeft = 0;
|
|
116
|
+
if (rem === 0 && durationMs > 0) return true;
|
|
117
|
+
}
|
|
118
|
+
if (durationMs <= 0) {
|
|
119
|
+
completeEntry(entry);
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
elapsed += rem;
|
|
123
|
+
const rawT = Math.min(1, elapsed / durationMs);
|
|
124
|
+
if (rawT >= 1) {
|
|
125
|
+
completeEntry(entry);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const eased = ease(rawT);
|
|
129
|
+
value = from + (to - from) * eased;
|
|
130
|
+
if (!this.safeCall(() => opts.onUpdate(value, rawT))) {
|
|
131
|
+
entry.cancel();
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
return entry.active;
|
|
135
|
+
},
|
|
136
|
+
cancel: () => {
|
|
137
|
+
if (!entry.active) return;
|
|
138
|
+
entry.active = false;
|
|
139
|
+
this.entries.delete(entry);
|
|
140
|
+
detach();
|
|
141
|
+
this._cancelled++;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
this._started++;
|
|
145
|
+
if (durationMs <= 0 && delayLeft <= 0) completeEntry(entry);
|
|
146
|
+
else {
|
|
147
|
+
this.entries.add(entry);
|
|
148
|
+
if (owner?.onDispose) detachOwner = owner.onDispose(() => entry.cancel());
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
get active() {
|
|
152
|
+
return entry.active;
|
|
153
|
+
},
|
|
154
|
+
get value() {
|
|
155
|
+
return value;
|
|
156
|
+
},
|
|
157
|
+
cancel() {
|
|
158
|
+
entry.cancel();
|
|
159
|
+
},
|
|
160
|
+
finish() {
|
|
161
|
+
completeEntry(entry);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/** Alias for `to(opts)` so Phaser/KAPLAY-style `tweens.add(...)` and `tweens.to(...)` both work seamlessly. */
|
|
166
|
+
add(opts) {
|
|
167
|
+
return this.to(opts);
|
|
168
|
+
}
|
|
169
|
+
/** Start a sub-stepped damped harmonic spring with smooth mid-flight retargeting. */
|
|
170
|
+
spring(opts) {
|
|
171
|
+
const stiffness = Math.max(.001, Number.isFinite(opts.stiffness) ? opts.stiffness ?? 280 : 280);
|
|
172
|
+
const damping = Math.max(0, Number.isFinite(opts.damping) ? opts.damping ?? 26 : 26);
|
|
173
|
+
const mass = Math.max(.001, Number.isFinite(opts.mass) ? opts.mass ?? 1 : 1);
|
|
174
|
+
const precision = Math.max(1e-6, Number.isFinite(opts.precision) ? opts.precision ?? .01 : .01);
|
|
175
|
+
const velocityPrecision = Math.max(1e-6, Number.isFinite(opts.velocityPrecision) ? opts.velocityPrecision ?? .05 : .05);
|
|
176
|
+
const owner = opts.owner ?? null;
|
|
177
|
+
let value = Number.isFinite(opts.from) ? opts.from : 0;
|
|
178
|
+
let target = Number.isFinite(opts.to) ? opts.to : value;
|
|
179
|
+
let velocity = Number.isFinite(opts.velocity) ? opts.velocity ?? 0 : 0;
|
|
180
|
+
if (isOwnerDead(owner)) return {
|
|
181
|
+
get active() {
|
|
182
|
+
return false;
|
|
183
|
+
},
|
|
184
|
+
get value() {
|
|
185
|
+
return value;
|
|
186
|
+
},
|
|
187
|
+
get velocity() {
|
|
188
|
+
return 0;
|
|
189
|
+
},
|
|
190
|
+
get target() {
|
|
191
|
+
return target;
|
|
192
|
+
},
|
|
193
|
+
setTarget() {},
|
|
194
|
+
cancel() {},
|
|
195
|
+
finish() {}
|
|
196
|
+
};
|
|
197
|
+
let disposed = false;
|
|
198
|
+
let detachOwner = null;
|
|
199
|
+
const detach = () => {
|
|
200
|
+
if (detachOwner) {
|
|
201
|
+
const fn = detachOwner;
|
|
202
|
+
detachOwner = null;
|
|
203
|
+
fn();
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
const settleEntry = (entry) => {
|
|
207
|
+
if (!entry.active) return;
|
|
208
|
+
entry.active = false;
|
|
209
|
+
this.entries.delete(entry);
|
|
210
|
+
detach();
|
|
211
|
+
this._completed++;
|
|
212
|
+
value = target;
|
|
213
|
+
velocity = 0;
|
|
214
|
+
if (this.safeCall(() => opts.onUpdate(value, 0)) && opts.onComplete) this.safeCall(() => opts.onComplete?.());
|
|
215
|
+
};
|
|
216
|
+
const entry = {
|
|
217
|
+
kind: "spring",
|
|
218
|
+
active: false,
|
|
219
|
+
step: (dtMs) => {
|
|
220
|
+
if (!entry.active) return false;
|
|
221
|
+
if (isOwnerDead(owner)) {
|
|
222
|
+
entry.cancel();
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
let rem = dtMs;
|
|
226
|
+
while (rem > 0) {
|
|
227
|
+
const hMs = rem > MAX_SPRING_SUBSTEP_MS ? MAX_SPRING_SUBSTEP_MS : rem;
|
|
228
|
+
const hSec = hMs * .001;
|
|
229
|
+
const accel = (-stiffness * (value - target) - damping * velocity) / mass;
|
|
230
|
+
velocity += accel * hSec;
|
|
231
|
+
value += velocity * hSec;
|
|
232
|
+
rem -= hMs;
|
|
233
|
+
}
|
|
234
|
+
if (Math.abs(value - target) <= precision && Math.abs(velocity) <= velocityPrecision) {
|
|
235
|
+
settleEntry(entry);
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
if (!this.safeCall(() => opts.onUpdate(value, velocity))) {
|
|
239
|
+
entry.cancel();
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
return entry.active;
|
|
243
|
+
},
|
|
244
|
+
cancel: () => {
|
|
245
|
+
disposed = true;
|
|
246
|
+
if (!entry.active) return;
|
|
247
|
+
entry.active = false;
|
|
248
|
+
this.entries.delete(entry);
|
|
249
|
+
detach();
|
|
250
|
+
this._cancelled++;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const arm = () => {
|
|
254
|
+
if (disposed || isOwnerDead(owner)) return;
|
|
255
|
+
if (Math.abs(value - target) <= precision && Math.abs(velocity) <= velocityPrecision) {
|
|
256
|
+
this._started++;
|
|
257
|
+
entry.active = true;
|
|
258
|
+
settleEntry(entry);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (!entry.active) {
|
|
262
|
+
entry.active = true;
|
|
263
|
+
this._started++;
|
|
264
|
+
this.entries.add(entry);
|
|
265
|
+
if (owner?.onDispose && !detachOwner) detachOwner = owner.onDispose(() => entry.cancel());
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
arm();
|
|
269
|
+
return {
|
|
270
|
+
get active() {
|
|
271
|
+
return entry.active;
|
|
272
|
+
},
|
|
273
|
+
get value() {
|
|
274
|
+
return value;
|
|
275
|
+
},
|
|
276
|
+
get velocity() {
|
|
277
|
+
return velocity;
|
|
278
|
+
},
|
|
279
|
+
get target() {
|
|
280
|
+
return target;
|
|
281
|
+
},
|
|
282
|
+
setTarget(nextTo) {
|
|
283
|
+
if (disposed || isOwnerDead(owner) || !Number.isFinite(nextTo)) return;
|
|
284
|
+
target = nextTo;
|
|
285
|
+
if (!entry.active) {
|
|
286
|
+
if (Math.abs(value - target) > precision || Math.abs(velocity) > velocityPrecision) arm();
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
cancel() {
|
|
290
|
+
entry.cancel();
|
|
291
|
+
},
|
|
292
|
+
finish() {
|
|
293
|
+
settleEntry(entry);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** Advance all active tweens & springs by a clamped EngineClock delta in ms. */
|
|
298
|
+
step(deltaMs) {
|
|
299
|
+
if (this.entries.size === 0) return;
|
|
300
|
+
const dt = Number.isFinite(deltaMs) && deltaMs > 0 ? Math.min(MAX_FRAME_DELTA_MS, deltaMs) : 0;
|
|
301
|
+
if (dt <= 0) return;
|
|
302
|
+
const list = Array.from(this.entries);
|
|
303
|
+
for (let i = 0; i < list.length; i++) {
|
|
304
|
+
const entry = list[i];
|
|
305
|
+
if (entry.active) entry.step(dt);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/** Cancel and clear all active tweens & springs (called during `Scene._destroy()`). */
|
|
309
|
+
clear() {
|
|
310
|
+
if (this.entries.size === 0) return;
|
|
311
|
+
const list = Array.from(this.entries);
|
|
312
|
+
for (let i = 0; i < list.length; i++) list[i].cancel();
|
|
313
|
+
this.entries.clear();
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
export { Easings, TweenManager };
|