@linxin666/dsh-pet 0.4.6 → 0.4.8
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.i18n.yaml +2 -2
- package/README.md +3 -1
- package/README.zh.md +3 -1
- package/lib/client.js +223 -79
- package/lib/client.js.map +1 -1
- package/lib/index.js +454 -1
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/client/PetSprite.d.ts +6 -4
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +100 -50
- package/lib/types/client/index.js +1 -1
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- package/lib/types/client/sequences.d.ts +9 -2
- package/lib/types/client/sequences.d.ts.map +1 -1
- package/lib/types/client/sequences.js +25 -14
- package/lib/types/client/spritesheet.d.ts +22 -1
- package/lib/types/client/spritesheet.d.ts.map +1 -1
- package/lib/types/client/spritesheet.js +26 -2
- package/package.json +26 -30
- package/src/client/PetSprite.test.tsx +262 -26
- package/src/client/PetSprite.tsx +99 -50
- package/src/client/index.ts +1 -1
- package/src/client/pet-css.test.ts +12 -0
- package/src/client/pet.module.css +9 -3
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- package/src/client/sequences.test.ts +37 -1
- package/src/client/sequences.ts +42 -16
- package/src/client/spritesheet.test.ts +81 -0
- package/src/client/spritesheet.ts +27 -2
- package/src/registry.test.ts +4 -2
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
|
@@ -93,10 +93,12 @@ export interface PetSpriteProps {
|
|
|
93
93
|
t: TranslateNS<typeof NS>;
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
|
-
* The floating pet. The spritesheet frame advances on
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
96
|
+
* The floating pet. The spritesheet frame advances on a timer scheduled for the
|
|
97
|
+
* current frame's remaining time (per-frame durations come from the
|
|
98
|
+
* definition's tracks), so the loop wakes once per frame change instead of once
|
|
99
|
+
* per screen refresh; the atlas image is loaded once and the background
|
|
100
|
+
* position is written straight to the sprite element (no per-frame React
|
|
101
|
+
* state). Roaming writes the sprite's position the same way (issue #17).
|
|
100
102
|
*/
|
|
101
103
|
export declare function PetSprite(props: PetSpriteProps): ReactPortal;
|
|
102
104
|
//# sourceMappingURL=PetSprite.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PetSprite.d.ts","sourceRoot":"","sources":["../../../src/client/PetSprite.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAkE,SAAS,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAGnH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAA;AACnE,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAA;AACrE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAGpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAIjD,OAAO,EAAE,EAAE,EAAE,MAAM,cAAc,CAAA;AAGjC,wEAAwE;AACxE,MAAM,WAAW,cAAc;IAC7B,gDAAgD;IAChD,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAA;IAC7B,8EAA8E;IAC9E,UAAU,EAAE,aAAa,CAAA;IACzB,qDAAqD;IACrD,OAAO,EAAE,gBAAgB,CAAA;IACzB,sCAAsC;IACtC,QAAQ,EAAE,WAAW,GAAG,IAAI,CAAA;IAC5B,8BAA8B;IAC9B,KAAK,EAAE,MAAM,IAAI,CAAA;IACjB,sCAAsC;IACtC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,sCAAsC;IACtC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,+BAA+B;IAC/B,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IAClD,6EAA6E;IAC7E,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAA;IAC9C,uDAAuD;IACvD,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC;;;;;OAKG;IACH,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAA;IACnC,4DAA4D;IAC5D,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAA;IAC1C,2DAA2D;IAC3D,cAAc,EAAE,MAAM,IAAI,CAAA;IAC1B;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB;;;;OAIG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IACf;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,IAAI,CAAA;IAC9D;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;IAC3B,qEAAqE;IACrE,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;OAGG;IACH,GAAG,CAAC,EAAE,WAAW,CAAA;IACjB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,+CAA+C;IAC/C,CAAC,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAA;CAC1B;AAkJD
|
|
1
|
+
{"version":3,"file":"PetSprite.d.ts","sourceRoot":"","sources":["../../../src/client/PetSprite.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAkE,SAAS,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAGnH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAA;AACnE,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAA;AACrE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAGpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAIjD,OAAO,EAAE,EAAE,EAAE,MAAM,cAAc,CAAA;AAGjC,wEAAwE;AACxE,MAAM,WAAW,cAAc;IAC7B,gDAAgD;IAChD,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAA;IAC7B,8EAA8E;IAC9E,UAAU,EAAE,aAAa,CAAA;IACzB,qDAAqD;IACrD,OAAO,EAAE,gBAAgB,CAAA;IACzB,sCAAsC;IACtC,QAAQ,EAAE,WAAW,GAAG,IAAI,CAAA;IAC5B,8BAA8B;IAC9B,KAAK,EAAE,MAAM,IAAI,CAAA;IACjB,sCAAsC;IACtC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,sCAAsC;IACtC,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,+BAA+B;IAC/B,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IAClD,6EAA6E;IAC7E,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAA;IAC9C,uDAAuD;IACvD,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC;;;;;OAKG;IACH,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAA;IACnC,4DAA4D;IAC5D,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAA;IAC1C,2DAA2D;IAC3D,cAAc,EAAE,MAAM,IAAI,CAAA;IAC1B;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB;;;;OAIG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IACf;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,IAAI,CAAA;IAC9D;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;IAC3B,qEAAqE;IACrE,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;OAGG;IACH,GAAG,CAAC,EAAE,WAAW,CAAA;IACjB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,+CAA+C;IAC/C,CAAC,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAA;CAC1B;AAkJD;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,WAAW,CA6wB5D"}
|
|
@@ -14,7 +14,7 @@ import { createPortal } from 'react-dom';
|
|
|
14
14
|
import clsx from 'clsx';
|
|
15
15
|
import { bubbleScaleFor } from "../persist.js";
|
|
16
16
|
import { announcementFresh } from "../announce.js";
|
|
17
|
-
import { framePosition, rowOfTrack, trimTrack } from "./spritesheet.js";
|
|
17
|
+
import { framePosition, rowOfTrack, scaledCellSize, trimTrack } from "./spritesheet.js";
|
|
18
18
|
import { createSequenceTimeline } from "./sequences.js";
|
|
19
19
|
import { animationForPhase } from "../state.js";
|
|
20
20
|
import styles from './pet.module.css';
|
|
@@ -132,10 +132,12 @@ function UsageAnnouncementBubble(props) {
|
|
|
132
132
|
return (_jsxs("div", { className: clsx(styles.bubble, styles.bubbleUsage, tone), role: "status", "aria-live": "polite", "data-dsh-pet-announcement": announcement.source, children: [_jsxs("span", { className: styles.bubbleUsageHead, children: [_jsx("span", { className: styles.bubbleUsageTitle, children: announcement.title }), (announcement.kind === 'balance' || announcement.kind === 'cost') && announcement.amount !== undefined && (_jsx("span", { className: styles.bubbleUsageValue, children: announcement.amount })), announcement.kind === 'plan' && announcement.percent !== undefined && (_jsx("span", { className: styles.bubbleUsageValue, children: Math.round(announcement.percent) + '%' }))] }), announcement.kind === 'plan' && announcement.percent !== undefined && (_jsx("span", { className: styles.bubbleUsageMeter, children: _jsx("span", { className: styles.bubbleUsageMeterFill, style: { width: Math.min(100, Math.max(0, announcement.percent)) + '%' } }) })), announcement.note !== undefined && (_jsx("span", { className: styles.bubbleUsageNote, children: announcement.note }))] }));
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
|
-
* The floating pet. The spritesheet frame advances on
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
135
|
+
* The floating pet. The spritesheet frame advances on a timer scheduled for the
|
|
136
|
+
* current frame's remaining time (per-frame durations come from the
|
|
137
|
+
* definition's tracks), so the loop wakes once per frame change instead of once
|
|
138
|
+
* per screen refresh; the atlas image is loaded once and the background
|
|
139
|
+
* position is written straight to the sprite element (no per-frame React
|
|
140
|
+
* state). Roaming writes the sprite's position the same way (issue #17).
|
|
139
141
|
*/
|
|
140
142
|
export function PetSprite(props) {
|
|
141
143
|
const { snapshot, definition, display, feedback } = props;
|
|
@@ -252,6 +254,10 @@ export function PetSprite(props) {
|
|
|
252
254
|
const animation = snapshot?.animation ?? 'idle';
|
|
253
255
|
const scaleRef = useRef(spriteScale);
|
|
254
256
|
scaleRef.current = spriteScale;
|
|
257
|
+
// The frame the loop painted last. A settled non-looping track leaves the
|
|
258
|
+
// loop stopped, so a later scale change repaints it from here instead of
|
|
259
|
+
// waking the loop.
|
|
260
|
+
const paintedRef = useRef(null);
|
|
255
261
|
useEffect(() => {
|
|
256
262
|
if (props.visual !== undefined)
|
|
257
263
|
return;
|
|
@@ -273,36 +279,48 @@ export function PetSprite(props) {
|
|
|
273
279
|
const leadAnimation = sequence?.[0] ?? animation;
|
|
274
280
|
const row = rowOfTrack(leadAnimation);
|
|
275
281
|
const track = trimTrack(tracks[leadAnimation], rows[row] ?? tracks[leadAnimation].frames.length);
|
|
282
|
+
// Write one frame's offset to the sprite element, skipping the assignment
|
|
283
|
+
// when the coordinates did not change. lastPosStr is the cache the whole
|
|
284
|
+
// loop shares, so a tick that changes nothing costs no style write.
|
|
285
|
+
let lastPosStr = '';
|
|
286
|
+
const paint = (frameRow, col) => {
|
|
287
|
+
paintedRef.current = { row: frameRow, col };
|
|
288
|
+
const pos = framePosition(cell, frameRow, col, scaleRef.current);
|
|
289
|
+
const posStr = pos.x + 'px ' + pos.y + 'px';
|
|
290
|
+
if (posStr === lastPosStr)
|
|
291
|
+
return;
|
|
292
|
+
lastPosStr = posStr;
|
|
293
|
+
if (spriteRef.current !== null) {
|
|
294
|
+
spriteRef.current.style.backgroundPosition = posStr;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
276
297
|
// Paint one static sprite frame up front either way, so the pet is never
|
|
277
298
|
// blank while the loop heat-up runs.
|
|
278
|
-
|
|
279
|
-
const lead = framePosition(cell, row, leadCol, scaleRef.current);
|
|
280
|
-
let lastPosStr = lead.x + 'px ' + lead.y + 'px';
|
|
281
|
-
if (spriteRef.current !== null) {
|
|
282
|
-
spriteRef.current.style.backgroundPosition = lastPosStr;
|
|
283
|
-
}
|
|
299
|
+
paint(row, track.frames[0]);
|
|
284
300
|
if (reduceMotion)
|
|
285
301
|
return;
|
|
286
|
-
let
|
|
302
|
+
let timer = 0;
|
|
287
303
|
let last = performance.now();
|
|
288
304
|
let sequenceElapsed = 0;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
305
|
+
// Sleep to the next frame boundary instead of waking on every screen
|
|
306
|
+
// refresh: track durations are 300-700 ms, so a 60 Hz loop spent the vast
|
|
307
|
+
// majority of its ticks proving that nothing had changed -- the same rule
|
|
308
|
+
// StatusOrnament already follows. A late wake (throttled tab, jank) still
|
|
309
|
+
// catches up every due frame, so the track keeps its own clock.
|
|
310
|
+
const schedule = (delay) => {
|
|
311
|
+
timer = window.setTimeout(tick, Math.max(1, delay));
|
|
312
|
+
};
|
|
313
|
+
const tick = () => {
|
|
314
|
+
timer = 0;
|
|
315
|
+
const now = performance.now();
|
|
316
|
+
const delta = now - last;
|
|
317
|
+
last = now;
|
|
292
318
|
if (timeline !== undefined && sequenceItems !== undefined) {
|
|
293
319
|
sequenceElapsed += delta;
|
|
294
320
|
const current = timeline.frameAt(sequenceElapsed);
|
|
295
321
|
const item = sequenceItems.get(current.animation);
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const posStr = pos.x + 'px ' + pos.y + 'px';
|
|
299
|
-
if (posStr !== lastPosStr) {
|
|
300
|
-
lastPosStr = posStr;
|
|
301
|
-
if (spriteRef.current !== null) {
|
|
302
|
-
spriteRef.current.style.backgroundPosition = posStr;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
raf = requestAnimationFrame(tick);
|
|
322
|
+
paint(item.row, item.track.frames[current.frameIndex]);
|
|
323
|
+
schedule(timeline.remainingAt(sequenceElapsed));
|
|
306
324
|
return;
|
|
307
325
|
}
|
|
308
326
|
// row/track come from the effect scope: they were computed once above
|
|
@@ -321,28 +339,32 @@ export function PetSprite(props) {
|
|
|
321
339
|
st.index += 1;
|
|
322
340
|
}
|
|
323
341
|
if (st.elapsed >= (track.durations[st.index] ?? 0)) {
|
|
324
|
-
if (track.loop) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
const col = track.frames[st.index];
|
|
333
|
-
const pos = framePosition(cell, row, col, scaleRef.current);
|
|
334
|
-
const posStr = pos.x + 'px ' + pos.y + 'px';
|
|
335
|
-
if (posStr !== lastPosStr) {
|
|
336
|
-
lastPosStr = posStr;
|
|
337
|
-
if (spriteRef.current !== null) {
|
|
338
|
-
spriteRef.current.style.backgroundPosition = posStr;
|
|
342
|
+
if (!track.loop) {
|
|
343
|
+
// Hold the final frame; the host owns the switch to the fallback, so
|
|
344
|
+
// stop scheduling instead of re-painting the same frame forever.
|
|
345
|
+
st.index = maxIndex;
|
|
346
|
+
paint(row, track.frames[st.index]);
|
|
347
|
+
return;
|
|
339
348
|
}
|
|
349
|
+
st.elapsed = 0;
|
|
350
|
+
st.index = 0;
|
|
340
351
|
}
|
|
341
|
-
|
|
352
|
+
paint(row, track.frames[st.index]);
|
|
353
|
+
schedule((track.durations[st.index] ?? 0) - st.elapsed);
|
|
342
354
|
};
|
|
343
|
-
|
|
344
|
-
return () =>
|
|
355
|
+
schedule(0);
|
|
356
|
+
return () => window.clearTimeout(timer);
|
|
345
357
|
}, [animation, phase, cell, columns, rows, tracks, sequences, props.visual]);
|
|
358
|
+
// A stopped loop is woken by nothing, so a sprite scale change repaints the
|
|
359
|
+
// frame it settled on: without this a resized pet would keep the old scale's
|
|
360
|
+
// cell offsets and show a neighbouring cell until the track changed.
|
|
361
|
+
useLayoutEffect(() => {
|
|
362
|
+
const painted = paintedRef.current;
|
|
363
|
+
if (props.visual !== undefined || painted === null || spriteRef.current === null)
|
|
364
|
+
return;
|
|
365
|
+
const pos = framePosition(cell, painted.row, painted.col, spriteScale);
|
|
366
|
+
spriteRef.current.style.backgroundPosition = pos.x + 'px ' + pos.y + 'px';
|
|
367
|
+
}, [spriteScale, cell, props.visual]);
|
|
346
368
|
// Auto-clear the feedback bubble after its CSS animation. The callback
|
|
347
369
|
// rides a ref so re-renders never reset the timer: the 2s poll rebuilds
|
|
348
370
|
// `props` every tick, and depending on it would starve the timeout.
|
|
@@ -371,10 +393,13 @@ export function PetSprite(props) {
|
|
|
371
393
|
const onPointerDown = (e) => {
|
|
372
394
|
if (props.dragDisabled === true)
|
|
373
395
|
return;
|
|
396
|
+
// Read the live position before ending a walk: the walk keeps it in a ref
|
|
397
|
+
// rather than React state, so a drag grabbed mid-walk starts where the pet
|
|
398
|
+
// actually is instead of snapping back to where the walk began.
|
|
399
|
+
const current = walkTargetRef.current ?? dragPosRef.current ?? { right: display.right, bottom: display.bottom };
|
|
374
400
|
endWalk(false);
|
|
375
401
|
e.preventDefault();
|
|
376
402
|
e.target.setPointerCapture?.(e.pointerId);
|
|
377
|
-
const current = dragPos ?? { right: display.right, bottom: display.bottom };
|
|
378
403
|
dragRef.current = { startX: e.clientX, startY: e.clientY, ...current };
|
|
379
404
|
draggedRef.current = false;
|
|
380
405
|
setHovered(false);
|
|
@@ -403,9 +428,6 @@ export function PetSprite(props) {
|
|
|
403
428
|
if (dragPos !== null)
|
|
404
429
|
props.onDragEnd(dragPos.right, dragPos.bottom);
|
|
405
430
|
};
|
|
406
|
-
const pos = dragPos ?? { right: display.right, bottom: display.bottom };
|
|
407
|
-
const spriteWidth = Math.round(cell.width * spriteScale);
|
|
408
|
-
const spriteHeight = Math.round(cell.height * spriteScale);
|
|
409
431
|
// --- roaming ---------------------------------------------------------
|
|
410
432
|
// The gameplay HUD rolls when the pet may wander; this side owns the motion
|
|
411
433
|
// (clamped to the viewport), the walk art's facing and the persisted resting
|
|
@@ -414,9 +436,22 @@ export function PetSprite(props) {
|
|
|
414
436
|
// the side view); every other track stays unmirrored.
|
|
415
437
|
const [facingRight, setFacingRight] = useState(false);
|
|
416
438
|
const walkRafRef = useRef(0);
|
|
439
|
+
// Live position while a walk is in flight. The walk writes the element's
|
|
440
|
+
// right/bottom directly and mirrors them here instead of calling setState:
|
|
441
|
+
// a walk is 1-3 s of 60 fps motion, and each frame used to re-render the
|
|
442
|
+
// whole sprite tree (bubble stack, dock panel) for a position the DOM already
|
|
443
|
+
// knew. Keeping it in a ref means a render that lands mid-walk (a poll, a
|
|
444
|
+
// hover) still sees where the sprite actually is (issue #17).
|
|
417
445
|
const walkTargetRef = useRef(null);
|
|
418
446
|
const dragPosRef = useRef(dragPos);
|
|
419
447
|
dragPosRef.current = dragPos;
|
|
448
|
+
const pos = walkTargetRef.current ?? dragPos ?? { right: display.right, bottom: display.bottom };
|
|
449
|
+
// One integral cell size for the sprite box, the atlas grid and every frame
|
|
450
|
+
// offset: background-size is this size times the column/row count, so the
|
|
451
|
+
// scaled atlas shares a pixel grid with the box that shows one cell (#17).
|
|
452
|
+
const scaled = scaledCellSize(cell, spriteScale);
|
|
453
|
+
const spriteWidth = scaled.width;
|
|
454
|
+
const spriteHeight = scaled.height;
|
|
420
455
|
/** Stop an active walk; `persist` lands the pet on the spot it reached. */
|
|
421
456
|
const endWalk = (persist) => {
|
|
422
457
|
if (walkRafRef.current === 0)
|
|
@@ -426,7 +461,13 @@ export function PetSprite(props) {
|
|
|
426
461
|
setFacingRight(false);
|
|
427
462
|
const settled = walkTargetRef.current;
|
|
428
463
|
walkTargetRef.current = null;
|
|
429
|
-
if (
|
|
464
|
+
if (settled === null)
|
|
465
|
+
return;
|
|
466
|
+
// Hand the reached spot back to React state: the walk's own frames never
|
|
467
|
+
// updated it, and a render (or a drag that starts here) would otherwise
|
|
468
|
+
// put the sprite back where the walk began.
|
|
469
|
+
setDragPos(settled);
|
|
470
|
+
if (persist)
|
|
430
471
|
props.onDragEnd(settled.right, settled.bottom);
|
|
431
472
|
};
|
|
432
473
|
useEffect(() => {
|
|
@@ -474,7 +515,15 @@ export function PetSprite(props) {
|
|
|
474
515
|
bottom: current.bottom + (target.bottom - current.bottom) * t,
|
|
475
516
|
};
|
|
476
517
|
walkTargetRef.current = next;
|
|
477
|
-
|
|
518
|
+
// The position goes straight to the element: a walk is the one motion
|
|
519
|
+
// that moved every frame, and a setState here re-rendered the bubble
|
|
520
|
+
// stack and the dock panel for a value the DOM already knew (issue
|
|
521
|
+
// #17). React state takes over once, on the landing frame.
|
|
522
|
+
const el = floatRef.current;
|
|
523
|
+
if (el !== null) {
|
|
524
|
+
el.style.right = next.right + 'px';
|
|
525
|
+
el.style.bottom = next.bottom + 'px';
|
|
526
|
+
}
|
|
478
527
|
if (t < 1) {
|
|
479
528
|
walkRafRef.current = window.requestAnimationFrame(step);
|
|
480
529
|
return;
|
|
@@ -482,6 +531,7 @@ export function PetSprite(props) {
|
|
|
482
531
|
walkRafRef.current = 0;
|
|
483
532
|
walkTargetRef.current = null;
|
|
484
533
|
setFacingRight(false);
|
|
534
|
+
setDragPos(next);
|
|
485
535
|
props.onDragEnd(next.right, next.bottom);
|
|
486
536
|
};
|
|
487
537
|
walkRafRef.current = window.requestAnimationFrame(step);
|
|
@@ -606,7 +656,7 @@ export function PetSprite(props) {
|
|
|
606
656
|
...(props.visual === undefined
|
|
607
657
|
? {
|
|
608
658
|
backgroundImage: imageReady ? 'url(' + definition.atlasUrl + ')' : undefined,
|
|
609
|
-
backgroundSize: (
|
|
659
|
+
backgroundSize: (scaled.width * columns) + 'px ' + (scaled.height * (definition.atlasRows ?? rows.length)) + 'px',
|
|
610
660
|
backgroundRepeat: 'no-repeat',
|
|
611
661
|
backgroundPosition: '0 0',
|
|
612
662
|
}
|
|
@@ -74,7 +74,7 @@ export const BINDER_RETRY_MS = 250;
|
|
|
74
74
|
const BINDER_ATTEMPTS = 24;
|
|
75
75
|
/** The snapshot an unresolved (or resolvable-but-unbound) form publishes. */
|
|
76
76
|
function pendingSnapshot(status) {
|
|
77
|
-
return { status, value: undefined, base: undefined, user: undefined, revision: undefined, writable: false
|
|
77
|
+
return { status, value: undefined, base: undefined, user: undefined, revision: undefined, writable: false };
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* A ConfigForm that late-binds to the family settings binder.
|
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
* - Canvas bitmap buffer (default where createImageBitmap/fetch/2D context
|
|
15
15
|
* exist): frames are decoded once into an ImageBitmap on demand, with a
|
|
16
16
|
* bounded look-ahead window over the playing track, and drawn onto one
|
|
17
|
-
* <canvas
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* <canvas>. Steady-state playback issues zero DOM mutations and re-decodes
|
|
18
|
+
* only what the retention budget evicted: decoded frames are held under
|
|
19
|
+
* MAX_RETAINED_BYTES, so a long-lived pet that plays every track of every
|
|
20
|
+
* skin cannot pin every bitmap for the life of the page (measured hotspot:
|
|
21
|
+
* swapping <img>.src per frame drove image decode + invalidation every tick).
|
|
20
22
|
* - Classic <img> fallback (jsdom/tests or missing APIs): identical to the
|
|
21
23
|
* historical behavior - cache-warm Image elements plus guarded src swaps,
|
|
22
24
|
* so environments without modern decoding keep working unchanged.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frames2d.d.ts","sourceRoot":"","sources":["../../../../src/client/renderers/frames2d.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"frames2d.d.ts","sourceRoot":"","sources":["../../../../src/client/renderers/frames2d.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAA4B,KAAK,WAAW,EAA2B,KAAK,iBAAiB,EAAE,MAAM,6BAA6B,CAAA;AACzI,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAEnD,oEAAoE;AACpE,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB,IAAI,EAAE,OAAO,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,8DAA8D;AAC9D,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAA;IAC3C,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,GAAG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;IACjE,wEAAwE;IACxE,KAAK,CAAC,EAAE,kBAAkB,EAAE,CAAA;CAC7B;AAED,sEAAsE;AACtE,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;IACb,0EAA0E;IAC1E,SAAS,EAAE,MAAM,CAAA;IACjB,sFAAsF;IACtF,YAAY,CAAC,EAAE,6BAA6B,EAAE,CAAA;IAC9C,2EAA2E;IAC3E,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACxC;AAED,4DAA4D;AAC5D,MAAM,WAAW,6BAA6B;IAC5C,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;CACnB;AAED,MAAM,WAAW,sBAAuB,SAAQ,iBAAiB;IAC/D,gFAAgF;IAChF,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAA;IACzC;;;;OAIG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAA;IAC7C,2DAA2D;IAC3D,YAAY,IAAI,MAAM,CAAA;CACvB;AA+HD,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,iBAAiB,CA2Y3D,CAAA"}
|
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
* - Canvas bitmap buffer (default where createImageBitmap/fetch/2D context
|
|
15
15
|
* exist): frames are decoded once into an ImageBitmap on demand, with a
|
|
16
16
|
* bounded look-ahead window over the playing track, and drawn onto one
|
|
17
|
-
* <canvas
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* <canvas>. Steady-state playback issues zero DOM mutations and re-decodes
|
|
18
|
+
* only what the retention budget evicted: decoded frames are held under
|
|
19
|
+
* MAX_RETAINED_BYTES, so a long-lived pet that plays every track of every
|
|
20
|
+
* skin cannot pin every bitmap for the life of the page (measured hotspot:
|
|
21
|
+
* swapping <img>.src per frame drove image decode + invalidation every tick).
|
|
20
22
|
* - Classic <img> fallback (jsdom/tests or missing APIs): identical to the
|
|
21
23
|
* historical behavior - cache-warm Image elements plus guarded src swaps,
|
|
22
24
|
* so environments without modern decoding keep working unchanged.
|
|
@@ -26,6 +28,19 @@
|
|
|
26
28
|
import { PET_RENDERER_API_VERSION } from "../../contracts/renderer.js";
|
|
27
29
|
/** Stall watchdog period: a looping track stuck longer re-kicks its chain. */
|
|
28
30
|
const WATCHDOG_MS = 1200;
|
|
31
|
+
/**
|
|
32
|
+
* Retention budget for decoded frame bitmaps, in bytes.
|
|
33
|
+
*
|
|
34
|
+
* The budget is expressed in bytes rather than in frames because a frame's
|
|
35
|
+
* decoded footprint is `width * height * 4` and published pets differ by more
|
|
36
|
+
* than an order of magnitude (a 512x683 frame is 1.33 MB, a 128x128 frame is
|
|
37
|
+
* 64 KB): a frame count would be either useless for small atlases or too tight
|
|
38
|
+
* for large ones. 128 MiB holds this repository's longest shipped track (78
|
|
39
|
+
* frames, 104 MB) in full, so a running loop still re-decodes nothing, while
|
|
40
|
+
* capping a pet that played every track of every skin at roughly a tenth of
|
|
41
|
+
* the ~1.7 GB the unbounded memo reached.
|
|
42
|
+
*/
|
|
43
|
+
const MAX_RETAINED_BYTES = 128 * 1024 * 1024;
|
|
29
44
|
function isRecord(value) {
|
|
30
45
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
31
46
|
}
|
|
@@ -108,6 +123,22 @@ function validateFrames2dConfig(config) {
|
|
|
108
123
|
}
|
|
109
124
|
return { tracks, phases: phases, ...(skins === undefined ? {} : { skins }) };
|
|
110
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Release one decoded frame. `close()` is browser-only and idempotent, so the
|
|
128
|
+
* call is guarded for exotic hosts and for frames that were already released;
|
|
129
|
+
* the `<img>` fallback has no close, where dropping the reference is the
|
|
130
|
+
* release.
|
|
131
|
+
*/
|
|
132
|
+
function releaseFrame(frame) {
|
|
133
|
+
if (frame === undefined)
|
|
134
|
+
return;
|
|
135
|
+
try {
|
|
136
|
+
const maybeClose = frame.source.close;
|
|
137
|
+
if (typeof maybeClose === 'function')
|
|
138
|
+
maybeClose.call(frame.source);
|
|
139
|
+
}
|
|
140
|
+
catch { /* already released */ }
|
|
141
|
+
}
|
|
111
142
|
export const frames2dRenderer = {
|
|
112
143
|
id: 'frames2d',
|
|
113
144
|
apiVersion: PET_RENDERER_API_VERSION,
|
|
@@ -155,11 +186,18 @@ export const frames2dRenderer = {
|
|
|
155
186
|
img.style.pointerEvents = 'none';
|
|
156
187
|
ctx.container.appendChild(img);
|
|
157
188
|
}
|
|
158
|
-
// Decode cache: one concurrent decode per URL,
|
|
159
|
-
//
|
|
160
|
-
//
|
|
189
|
+
// Decode cache: one concurrent decode per URL, retained under a decoded
|
|
190
|
+
// byte budget. The historical map memoized every decoded frame forever, so
|
|
191
|
+
// a pet that had played all of its tracks (every skin) kept every bitmap
|
|
192
|
+
// alive for the life of the page: a shipped three-skin pet measures 1276
|
|
193
|
+
// frames at 512x683 RGBA, about 1.7 GB, growing monotonically. Entries now
|
|
194
|
+
// carry their decoded footprint, sit in Map insertion order (delete + set
|
|
195
|
+
// is an LRU touch) and the least recently used ones outside the playing
|
|
196
|
+
// window are closed once MAX_RETAINED_BYTES is crossed. A failed decode
|
|
197
|
+
// still resolves undefined and leaves the memo, so a transient fetch error
|
|
198
|
+
// retries instead of sticking.
|
|
161
199
|
const decoding = new Map();
|
|
162
|
-
|
|
200
|
+
let retainedBytes = 0;
|
|
163
201
|
// All frame fetches funnel through a small pool. Full-library warm passes
|
|
164
202
|
// on large pets fire 1100+ requests at once, which trips the browser's
|
|
165
203
|
// in-flight request limit (net::ERR_INSUFFICIENT_RESOURCES) and fails
|
|
@@ -210,27 +248,96 @@ export const frames2dRenderer = {
|
|
|
210
248
|
frameQueue.unshift(frameQueue.splice(index, 1)[0]);
|
|
211
249
|
}
|
|
212
250
|
const cached = decoding.get(url);
|
|
213
|
-
if (cached !== undefined)
|
|
214
|
-
|
|
251
|
+
if (cached !== undefined) {
|
|
252
|
+
// LRU touch: a hit means playback just asked for this frame, so it must
|
|
253
|
+
// not be the next victim. Re-inserting the same entry only reorders.
|
|
254
|
+
decoding.delete(url);
|
|
255
|
+
decoding.set(url, cached);
|
|
256
|
+
return cached.job;
|
|
257
|
+
}
|
|
215
258
|
let release;
|
|
216
259
|
const gate = new Promise((resolve) => { release = resolve; });
|
|
217
260
|
const job = gate.then(() => (disposed ? undefined : decodeFrame(url)));
|
|
218
|
-
|
|
219
|
-
|
|
261
|
+
const entry = { job };
|
|
262
|
+
job.then((frame) => {
|
|
263
|
+
if (frame === undefined) {
|
|
264
|
+
decoding.delete(url);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// A decode that lands after dispose is closed here: nothing can close
|
|
268
|
+
// it later, and the map was already drained.
|
|
269
|
+
if (disposed) {
|
|
270
|
+
releaseFrame(frame);
|
|
271
|
+
decoding.delete(url);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
entry.frame = frame;
|
|
275
|
+
entry.bytes = frame.width * frame.height * 4;
|
|
276
|
+
retainedBytes += entry.bytes;
|
|
277
|
+
// Only a resolved frame is evictable, and its decoded size is known
|
|
278
|
+
// only now, so the budget is enforced here.
|
|
279
|
+
enforceRetentionBudget();
|
|
280
|
+
}, () => decoding.delete(url));
|
|
220
281
|
void job.finally(() => {
|
|
221
282
|
activeFrames -= 1;
|
|
222
283
|
pumpFrames();
|
|
223
284
|
});
|
|
224
|
-
decoding.set(url,
|
|
225
|
-
|
|
226
|
-
const entry = { url, release };
|
|
285
|
+
decoding.set(url, entry);
|
|
286
|
+
const queued = { url, release };
|
|
227
287
|
if (jump)
|
|
228
|
-
frameQueue.unshift(
|
|
288
|
+
frameQueue.unshift(queued);
|
|
229
289
|
else
|
|
230
|
-
frameQueue.push(
|
|
290
|
+
frameQueue.push(queued);
|
|
231
291
|
pumpFrames();
|
|
232
292
|
return job;
|
|
233
293
|
};
|
|
294
|
+
/**
|
|
295
|
+
* Release decoded bitmaps until the retained footprint is back under
|
|
296
|
+
* MAX_RETAINED_BYTES. Two properties keep this from breaking playback:
|
|
297
|
+
*
|
|
298
|
+
* - The playing track's look-ahead window, plus the frame a draw is waiting
|
|
299
|
+
* on, are never released. Releasing one would hand a closed bitmap to
|
|
300
|
+
* `drawImage`, and re-decoding it would stall the next tick.
|
|
301
|
+
* - Frames of any other track go first. That is what keeps a running loop
|
|
302
|
+
* warm under a budget smaller than two whole tracks: eviction reclaims the
|
|
303
|
+
* track the pet left instead of eating the cycle it is playing.
|
|
304
|
+
*
|
|
305
|
+
* When the protected window alone exceeds the budget nothing is released —
|
|
306
|
+
* a pet whose frames are that large cannot be bounded further without
|
|
307
|
+
* breaking playback, and the overshoot is then exactly one window.
|
|
308
|
+
*/
|
|
309
|
+
function enforceRetentionBudget() {
|
|
310
|
+
if (retainedBytes <= MAX_RETAINED_BYTES)
|
|
311
|
+
return;
|
|
312
|
+
const def = config.tracks[track];
|
|
313
|
+
const playing = new Set(def === undefined ? [] : def.frames);
|
|
314
|
+
const protectedUrls = new Set();
|
|
315
|
+
if (pinned !== undefined)
|
|
316
|
+
protectedUrls.add(pinned);
|
|
317
|
+
if (def !== undefined) {
|
|
318
|
+
const end = Math.min(def.frames.length, frameIndex + 1 + PREFETCH_AHEAD);
|
|
319
|
+
for (let index = frameIndex; index < end; index += 1) {
|
|
320
|
+
const url = def.frames[index];
|
|
321
|
+
if (url !== undefined)
|
|
322
|
+
protectedUrls.add(url);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Pass 1 reclaims frames of tracks the pet is not playing; pass 2 is the
|
|
326
|
+
// backstop for a single track larger than the whole budget.
|
|
327
|
+
for (const foreignOnly of [true, false]) {
|
|
328
|
+
for (const [url, entry] of decoding) {
|
|
329
|
+
if (retainedBytes <= MAX_RETAINED_BYTES)
|
|
330
|
+
return;
|
|
331
|
+
if (entry.bytes === undefined || protectedUrls.has(url))
|
|
332
|
+
continue;
|
|
333
|
+
if (foreignOnly && playing.has(url))
|
|
334
|
+
continue;
|
|
335
|
+
releaseFrame(entry.frame);
|
|
336
|
+
retainedBytes -= entry.bytes;
|
|
337
|
+
decoding.delete(url);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
234
341
|
/**
|
|
235
342
|
* Bounded look-ahead window: decode only the frames playback is about to
|
|
236
343
|
* need. The historical warm pass decoded every frame of every track up
|
|
@@ -265,6 +372,9 @@ export const frames2dRenderer = {
|
|
|
265
372
|
let baseIdle = config.phases.idle;
|
|
266
373
|
let drawToken = 0;
|
|
267
374
|
let lastDrawnUrl;
|
|
375
|
+
// The frame a draw is currently awaiting. Eviction must not release it:
|
|
376
|
+
// the callback below holds the bitmap and would draw a closed one.
|
|
377
|
+
let pinned;
|
|
268
378
|
const trackForPhase = (phase) => {
|
|
269
379
|
const mapped = config.phases[phase];
|
|
270
380
|
if (mapped === undefined)
|
|
@@ -277,7 +387,10 @@ export const frames2dRenderer = {
|
|
|
277
387
|
if (context2d === null || canvas === null)
|
|
278
388
|
return;
|
|
279
389
|
const myToken = ++drawToken;
|
|
390
|
+
pinned = url;
|
|
280
391
|
void loadFrame(url, true).then((frame) => {
|
|
392
|
+
if (pinned === url)
|
|
393
|
+
pinned = undefined;
|
|
281
394
|
if (disposed || frame === undefined || myToken !== drawToken)
|
|
282
395
|
return;
|
|
283
396
|
if (lastDrawnUrl === url)
|
|
@@ -292,7 +405,8 @@ export const frames2dRenderer = {
|
|
|
292
405
|
context2d.clearRect(0, 0, canvas.width, canvas.height);
|
|
293
406
|
}
|
|
294
407
|
context2d.drawImage(frame.source, 0, 0);
|
|
295
|
-
}).catch(() => {
|
|
408
|
+
}).catch(() => { if (pinned === url)
|
|
409
|
+
pinned = undefined; /* keep last frame */ });
|
|
296
410
|
};
|
|
297
411
|
const show = (trackId, index) => {
|
|
298
412
|
const def = config.tracks[trackId];
|
|
@@ -393,24 +507,16 @@ export const frames2dRenderer = {
|
|
|
393
507
|
clearTimeout(timer);
|
|
394
508
|
if (watchdog !== undefined)
|
|
395
509
|
clearInterval(watchdog);
|
|
396
|
-
// Release
|
|
397
|
-
//
|
|
398
|
-
//
|
|
510
|
+
// Release every retained bitmap and drain the pool. Queued-but-unstarted
|
|
511
|
+
// frames resolve as no-ops (the gate skips decodeFrame once disposed), and
|
|
512
|
+
// a decode that lands after this loop is closed by the resolution guard
|
|
513
|
+
// above, so teardown leaves nothing pending and nothing retained.
|
|
399
514
|
for (const queued of frameQueue.splice(0))
|
|
400
515
|
queued.release();
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
const maybeClose = frame?.source?.close;
|
|
406
|
-
if (typeof maybeClose === 'function' && frame !== undefined)
|
|
407
|
-
maybeClose.call(frame.source);
|
|
408
|
-
}
|
|
409
|
-
catch { /* already released */ }
|
|
410
|
-
}).catch(() => { });
|
|
411
|
-
}
|
|
412
|
-
decoding.clear();
|
|
413
|
-
});
|
|
516
|
+
for (const entry of decoding.values())
|
|
517
|
+
releaseFrame(entry.frame);
|
|
518
|
+
decoding.clear();
|
|
519
|
+
retainedBytes = 0;
|
|
414
520
|
canvas?.remove();
|
|
415
521
|
img?.remove();
|
|
416
522
|
};
|
|
@@ -7,11 +7,18 @@ export interface SequenceFrame {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Precomputed timeline for one looping sequence: the per-item duration table
|
|
10
|
-
* is built once instead of per query. The sprite frame loop asks
|
|
11
|
-
*
|
|
10
|
+
* is built once instead of per query. The sprite frame loop asks once per
|
|
11
|
+
* frame change, where the per-call map/reduce is pure waste.
|
|
12
12
|
*/
|
|
13
13
|
export interface SequenceTimeline {
|
|
14
14
|
frameAt(elapsedMs: number): SequenceFrame;
|
|
15
|
+
/**
|
|
16
|
+
* Milliseconds from `elapsedMs` until the frame it lands on ends. The sprite
|
|
17
|
+
* loop sleeps for exactly this long instead of polling at the screen refresh
|
|
18
|
+
* rate (issue #17); the floor of 1 ms keeps a zero-length frame from turning
|
|
19
|
+
* into a busy timer.
|
|
20
|
+
*/
|
|
21
|
+
remainingAt(elapsedMs: number): number;
|
|
15
22
|
}
|
|
16
23
|
/** Build a {@link SequenceTimeline} over one manifest sequence. */
|
|
17
24
|
export declare function createSequenceTimeline(sequence: readonly PetAnimation[], tracks: Record<PetAnimation, PetTrackDef>): SequenceTimeline;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sequences.d.ts","sourceRoot":"","sources":["../../../src/client/sequences.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAE1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,YAAY,CAAA;IACvB,UAAU,EAAE,MAAM,CAAA;CACnB;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"sequences.d.ts","sourceRoot":"","sources":["../../../src/client/sequences.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAE1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,YAAY,CAAA;IACvB,UAAU,EAAE,MAAM,CAAA;CACnB;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAAA;IACzC;;;;;OAKG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAA;CACvC;AAUD,mEAAmE;AACnE,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,SAAS,YAAY,EAAE,EACjC,MAAM,EAAE,MAAM,CAAC,YAAY,EAAE,WAAW,CAAC,GACxC,gBAAgB,CAgClB;AAED,2FAA2F;AAC3F,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,SAAS,YAAY,EAAE,EACjC,MAAM,EAAE,MAAM,CAAC,YAAY,EAAE,WAAW,CAAC,EACzC,SAAS,EAAE,MAAM,GAChB,aAAa,CAEf"}
|
|
@@ -3,22 +3,33 @@
|
|
|
3
3
|
export function createSequenceTimeline(sequence, tracks) {
|
|
4
4
|
const itemDurations = sequence.map(animation => tracks[animation].durations.reduce((sum, value) => sum + value, 0));
|
|
5
5
|
const sequenceDuration = itemDurations.reduce((sum, value) => sum + value, 0);
|
|
6
|
+
// One traversal serves both queries: frameAt needs the index it lands on and
|
|
7
|
+
// remainingAt needs how far into that frame it is, so resolving them
|
|
8
|
+
// separately would walk the same tables twice for one loop tick.
|
|
9
|
+
const locate = (elapsedMs) => {
|
|
10
|
+
let offset = sequenceDuration > 0 ? Math.max(0, elapsedMs) % sequenceDuration : 0;
|
|
11
|
+
let itemIndex = 0;
|
|
12
|
+
while (itemIndex < sequence.length - 1 && offset >= itemDurations[itemIndex]) {
|
|
13
|
+
offset -= itemDurations[itemIndex];
|
|
14
|
+
itemIndex += 1;
|
|
15
|
+
}
|
|
16
|
+
const animation = sequence[itemIndex];
|
|
17
|
+
const track = tracks[animation];
|
|
18
|
+
let frameIndex = 0;
|
|
19
|
+
while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]) {
|
|
20
|
+
offset -= track.durations[frameIndex];
|
|
21
|
+
frameIndex += 1;
|
|
22
|
+
}
|
|
23
|
+
return { animation, track, frameIndex, offsetInFrame: offset };
|
|
24
|
+
};
|
|
6
25
|
return {
|
|
7
26
|
frameAt(elapsedMs) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const animation = sequence[itemIndex];
|
|
15
|
-
const track = tracks[animation];
|
|
16
|
-
let frameIndex = 0;
|
|
17
|
-
while (frameIndex < track.frames.length - 1 && offset >= track.durations[frameIndex]) {
|
|
18
|
-
offset -= track.durations[frameIndex];
|
|
19
|
-
frameIndex += 1;
|
|
20
|
-
}
|
|
21
|
-
return { animation, frameIndex };
|
|
27
|
+
const point = locate(elapsedMs);
|
|
28
|
+
return { animation: point.animation, frameIndex: point.frameIndex };
|
|
29
|
+
},
|
|
30
|
+
remainingAt(elapsedMs) {
|
|
31
|
+
const point = locate(elapsedMs);
|
|
32
|
+
return Math.max(1, (point.track.durations[point.frameIndex] ?? 0) - point.offsetInFrame);
|
|
22
33
|
},
|
|
23
34
|
};
|
|
24
35
|
}
|