@linxin666/dsh-pet 0.4.7 → 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 +1 -0
- package/README.zh.md +1 -0
- package/lib/client.js +121 -56
- package/lib/client.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/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 +1 -1
- package/src/client/PetSprite.test.tsx +262 -26
- package/src/client/PetSprite.tsx +99 -50
- package/src/client/pet-css.test.ts +12 -0
- package/src/client/pet.module.css +9 -3
- 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 +2 -1
|
@@ -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=Pet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rE,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
|
}
|
|
@@ -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
|
}
|
|
@@ -12,12 +12,33 @@ import type { PetCell, PetTrackDef } from '../registry.ts';
|
|
|
12
12
|
export type TrackDef = PetTrackDef;
|
|
13
13
|
/** Row index of one animation track (the fixed 9-row contract). */
|
|
14
14
|
export declare function rowOfTrack(animation: PetAnimation): number;
|
|
15
|
+
/**
|
|
16
|
+
* Integer size of one cell after `scale`, in device-independent px.
|
|
17
|
+
*
|
|
18
|
+
* Rounding happens here, once, so the rendered atlas grid is integral end to
|
|
19
|
+
* end (issue #17): `background-size` is this size times the column/row count
|
|
20
|
+
* and every frame offset is an exact multiple of it. A fractional cell size
|
|
21
|
+
* leaves each frame on a sub-pixel boundary — 192px cells at `display.size`
|
|
22
|
+
* 160 scale to 147.69px — and the compositor then has to resample the whole
|
|
23
|
+
* scaled atlas on every frame change instead of translating an already-scaled
|
|
24
|
+
* bitmap by whole pixels. The pet advances a frame every 300-700 ms, so that
|
|
25
|
+
* resample was running continuously for a background surface the size of the
|
|
26
|
+
* sprite. A floor of 1px keeps a legally tiny display.size from collapsing the
|
|
27
|
+
* cell to zero and rendering nothing.
|
|
28
|
+
*/
|
|
29
|
+
export declare function scaledCellSize(cell: PetCell, scale: number): {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
};
|
|
15
33
|
/**
|
|
16
34
|
* Background-position (px) of one frame cell within the scaled atlas.
|
|
17
35
|
* The background image is scaled by `scale` (element size ÷ cell size), and
|
|
18
36
|
* background-position offsets are applied in SCALED coordinates — using raw
|
|
19
37
|
* atlas coordinates here would drift each frame by the scale factor and
|
|
20
|
-
* render torn/overlapping frames.
|
|
38
|
+
* render torn/overlapping frames. The scaled cell is quantized by
|
|
39
|
+
* {@link scaledCellSize}, so the offset of frame N is exactly N whole cells:
|
|
40
|
+
* the atlas is snapped to the pixel grid and a frame change is a plain
|
|
41
|
+
* translate of the scaled bitmap.
|
|
21
42
|
*/
|
|
22
43
|
export declare function framePosition(cell: PetCell, row: number, col: number, scale?: number): {
|
|
23
44
|
x: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spritesheet.d.ts","sourceRoot":"","sources":["../../../src/client/spritesheet.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE1D,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,WAAW,CAAA;AAElC,mEAAmE;AACnE,wBAAgB,UAAU,CAAC,SAAS,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED
|
|
1
|
+
{"version":3,"file":"spritesheet.d.ts","sourceRoot":"","sources":["../../../src/client/spritesheet.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE1D,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,WAAW,CAAA;AAElC,mEAAmE;AACnE,wBAAgB,UAAU,CAAC,SAAS,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAK9F;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,SAAI,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAG1G;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,QAAQ,CAQvE"}
|
|
@@ -12,15 +12,39 @@ export function rowOfTrack(animation) {
|
|
|
12
12
|
// The table itself lives in state.ts (rowOf) — the single source of truth.
|
|
13
13
|
return rowOf(animation);
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Integer size of one cell after `scale`, in device-independent px.
|
|
17
|
+
*
|
|
18
|
+
* Rounding happens here, once, so the rendered atlas grid is integral end to
|
|
19
|
+
* end (issue #17): `background-size` is this size times the column/row count
|
|
20
|
+
* and every frame offset is an exact multiple of it. A fractional cell size
|
|
21
|
+
* leaves each frame on a sub-pixel boundary — 192px cells at `display.size`
|
|
22
|
+
* 160 scale to 147.69px — and the compositor then has to resample the whole
|
|
23
|
+
* scaled atlas on every frame change instead of translating an already-scaled
|
|
24
|
+
* bitmap by whole pixels. The pet advances a frame every 300-700 ms, so that
|
|
25
|
+
* resample was running continuously for a background surface the size of the
|
|
26
|
+
* sprite. A floor of 1px keeps a legally tiny display.size from collapsing the
|
|
27
|
+
* cell to zero and rendering nothing.
|
|
28
|
+
*/
|
|
29
|
+
export function scaledCellSize(cell, scale) {
|
|
30
|
+
return {
|
|
31
|
+
width: Math.max(1, Math.round(cell.width * scale)),
|
|
32
|
+
height: Math.max(1, Math.round(cell.height * scale)),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
15
35
|
/**
|
|
16
36
|
* Background-position (px) of one frame cell within the scaled atlas.
|
|
17
37
|
* The background image is scaled by `scale` (element size ÷ cell size), and
|
|
18
38
|
* background-position offsets are applied in SCALED coordinates — using raw
|
|
19
39
|
* atlas coordinates here would drift each frame by the scale factor and
|
|
20
|
-
* render torn/overlapping frames.
|
|
40
|
+
* render torn/overlapping frames. The scaled cell is quantized by
|
|
41
|
+
* {@link scaledCellSize}, so the offset of frame N is exactly N whole cells:
|
|
42
|
+
* the atlas is snapped to the pixel grid and a frame change is a plain
|
|
43
|
+
* translate of the scaled bitmap.
|
|
21
44
|
*/
|
|
22
45
|
export function framePosition(cell, row, col, scale = 1) {
|
|
23
|
-
|
|
46
|
+
const scaled = scaledCellSize(cell, scale);
|
|
47
|
+
return { x: -col * scaled.width, y: -row * scaled.height };
|
|
24
48
|
}
|
|
25
49
|
/**
|
|
26
50
|
* Trim a track to the actual frame count of its row (the manifest's per-row
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-pet",
|
|
3
3
|
"description": "Multi-pet companion plugin for the dsh web GUI: a registry-driven floating pet that reacts to model activity, with per-pet naming, petting/feeding interactions and an affinity score",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.8",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|