@flare-engine/animation 0.4.10 → 0.4.12
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/dist/index.d.mts +16 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +16 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +72 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +73 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -55,11 +55,20 @@ interface Spring {
|
|
|
55
55
|
/** Whether the spring has settled (within rest threshold). */
|
|
56
56
|
atRest: boolean;
|
|
57
57
|
}
|
|
58
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* Create a spring with initial value.
|
|
60
|
+
*
|
|
61
|
+
* Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`
|
|
62
|
+
* is negative, or any of the three is not finite: none of those ever produced a usable spring.
|
|
63
|
+
* `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later
|
|
64
|
+
* edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.
|
|
65
|
+
*/
|
|
59
66
|
export declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
60
67
|
/**
|
|
61
68
|
* Update a spring by dt seconds. Returns the current value.
|
|
62
|
-
*
|
|
69
|
+
* Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame
|
|
70
|
+
* or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float
|
|
71
|
+
* rounding).
|
|
63
72
|
*/
|
|
64
73
|
export declare function updateSpring(s: Spring, dt: number): number;
|
|
65
74
|
/** Set a new target for the spring. Wakes it if at rest. */
|
|
@@ -147,10 +156,13 @@ interface Tween {
|
|
|
147
156
|
export declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
148
157
|
/**
|
|
149
158
|
* Update a tween by dt seconds. Returns the current value.
|
|
150
|
-
* When complete, triggers onComplete callback
|
|
159
|
+
* When complete, triggers onComplete callback.
|
|
151
160
|
*/
|
|
152
161
|
export declare function updateTween(tw: Tween, dt: number): number;
|
|
153
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Chain another tween to run after this one completes. Returns the chained tween.
|
|
164
|
+
* Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.
|
|
165
|
+
*/
|
|
154
166
|
export declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
155
167
|
/** Reset a tween to its initial state. */
|
|
156
168
|
export declare function resetTween(tw: Tween): void;
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"mappings":";;;UAGiB;;WAEN;;WAEA;;WAEA;;WAEA,QAAQ;;EAEjB;;EAEA;;;;;;;;;;wBAWc,cACd,cACA,YACA,gBACA,SAAQ,WACP;;;;;wBASa,oBAAoB,IAAI,eAAe;;;;wBAavC,mBAAmB,IAAI;;UCpDtB;;EAEf;;EAEA;;EAEA;;;UAIe;;EAEf;;EAEA;;EAEA;;EAEA,QAAQ;;EAER
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"mappings":";;;UAGiB;;WAEN;;WAEA;;WAEA;;WAEA,QAAQ;;EAEjB;;EAEA;;;;;;;;;;wBAWc,cACd,cACA,YACA,gBACA,SAAQ,WACP;;;;;wBASa,oBAAoB,IAAI,eAAe;;;;wBAavC,mBAAmB,IAAI;;UCpDtB;;EAEf;;EAEA;;EAEA;;;UAIe;;EAEf;;EAEA;;EAEA;;EAEA,QAAQ;;EAER;;;;;;;;;;wBAiBc,OAAO,eAAe,gBAAgB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led (within rest threshold). */
|
|
56
56
|
atRest: boolean;
|
|
57
57
|
}
|
|
58
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* Create a spring with initial value.
|
|
60
|
+
*
|
|
61
|
+
* Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`
|
|
62
|
+
* is negative, or any of the three is not finite: none of those ever produced a usable spring.
|
|
63
|
+
* `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later
|
|
64
|
+
* edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.
|
|
65
|
+
*/
|
|
59
66
|
export declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
60
67
|
/**
|
|
61
68
|
* Update a spring by dt seconds. Returns the current value.
|
|
62
|
-
*
|
|
69
|
+
* Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame
|
|
70
|
+
* or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float
|
|
71
|
+
* rounding).
|
|
63
72
|
*/
|
|
64
73
|
export declare function updateSpring(s: Spring, dt: number): number;
|
|
65
74
|
/** Set a new target for the spring. Wakes it if at rest. */
|
|
@@ -147,10 +156,13 @@ interface Tween {
|
|
|
147
156
|
export declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
148
157
|
/**
|
|
149
158
|
* Update a tween by dt seconds. Returns the current value.
|
|
150
|
-
* When complete, triggers onComplete callback
|
|
159
|
+
* When complete, triggers onComplete callback.
|
|
151
160
|
*/
|
|
152
161
|
export declare function updateTween(tw: Tween, dt: number): number;
|
|
153
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Chain another tween to run after this one completes. Returns the chained tween.
|
|
164
|
+
* Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.
|
|
165
|
+
*/
|
|
154
166
|
export declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
155
167
|
/** Reset a tween to its initial state. */
|
|
156
168
|
export declare function resetTween(tw: Tween): void;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"mappings":";;;UAGiB;;WAEN;;WAEA;;WAEA;;WAEA,QAAQ;;EAEjB;;EAEA;;;;;;;;;;wBAWc,cACd,cACA,YACA,gBACA,SAAQ,WACP;;;;;wBASa,oBAAoB,IAAI,eAAe;;;;wBAavC,mBAAmB,IAAI;;UCpDtB;;EAEf;;EAEA;;EAEA;;;UAIe;;EAEf;;EAEA;;EAEA;;EAEA,QAAQ;;EAER
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"mappings":";;;UAGiB;;WAEN;;WAEA;;WAEA;;WAEA,QAAQ;;EAEjB;;EAEA;;;;;;;;;;wBAWc,cACd,cACA,YACA,gBACA,SAAQ,WACP;;;;;wBASa,oBAAoB,IAAI,eAAe;;;;wBAavC,mBAAmB,IAAI;;UCpDtB;;EAEf;;EAEA;;EAEA;;;UAIe;;EAEf;;EAEA;;EAEA;;EAEA,QAAQ;;EAER;;;;;;;;;;wBAiBc,OAAO,eAAe,gBAAgB,SAAS,QAAQ,gBAAgB;;;;;;;wBA4BvE,aAAa,GAAG,QAAQ;;wBAmExB,gBAAgB,GAAG,QAAQ;;wBAM3B,YAAY,GAAG,QAAQ;;UCxItB;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;EAEA;;EAEA;;;wBAIc,qBAAqB,OAAO,cAAc;;wBAY1C,cAAc,OAAO,qBAAqB;;wBAW1C,cAAc,OAAO;;wBASrB,eAAe,OAAO;;wBAKtB,gBAAgB,OAAO;;;;;wBAQvB,eAAe,OAAO,qBAAqB,aAAa;;wBA+BxD,gBAAgB,OAAO,sBAAsB;UC/F5C;WACN;;UAGM;WACN,gBAAgB;EACzB;EACA;;wBAGc,mBAAmB,gBAAgB,qBAAqB;wBASxD,iBAAiB,KAAK,cAAc;wBASpC,kBAAkB,KAAK;UC5BtB,cAAc;WACpB,OAAO;WACP,UAAU,MAAM,GAAG,UAAU,MAAM;EAC5C;;wBAGc,oBAAoB,GAClC,OAAO,GACP,kBACA,UAAU,MAAM,GAAG,UAAU,MAAM,IAClC,cAAc;wBAKD,aAAa,GAAG,KAAK,cAAc,IAAI;wBAIvC,iBAAiB,GAAG,KAAK,cAAc;;;;;wBAQvC,cAAc,GAAG,MAAM,GAAG,KAAK,cAAc,KAAK;;UCxBjD;;EAEf;;EAEA;;EAEA;;EAEA,QAAQ;;EAER;;EAEA;;EAEA;;EAEA;;EAEA,MAAM;;;wBAIQ,MAAM,cAAc,YAAY,kBAAkB,SAAS,WAAW;;;;;wBAkBtE,YAAY,IAAI,OAAO;;;;;wBAsBvB,WAAW,IAAI,OAAO,MAAM,QAAQ;;wBAMpC,WAAW,IAAI;;;;;UChEd;;WAEN,QAAQ;;;wBAIH,sBAAsB;;wBAKtB,SAAS,KAAK,cAAc,IAAI;;;;;wBAQhC,aAAa,KAAK,cAAc;;wBAuBhC,YAAY,KAAK"}
|
package/dist/index.js
CHANGED
|
@@ -42,31 +42,88 @@ function resetPeriodicTween(tw) {
|
|
|
42
42
|
// src/spring.ts
|
|
43
43
|
const REST_THRESHOLD = .001;
|
|
44
44
|
const VELOCITY_THRESHOLD = .001;
|
|
45
|
-
|
|
45
|
+
const CRITICAL_BAND = 1e-6;
|
|
46
|
+
/**
|
|
47
|
+
* Create a spring with initial value.
|
|
48
|
+
*
|
|
49
|
+
* Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`
|
|
50
|
+
* is negative, or any of the three is not finite: none of those ever produced a usable spring.
|
|
51
|
+
* `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later
|
|
52
|
+
* edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.
|
|
53
|
+
*/
|
|
46
54
|
function spring(value, target, config) {
|
|
55
|
+
const stiffness = config?.stiffness ?? 100;
|
|
56
|
+
const damping = config?.damping ?? 10;
|
|
57
|
+
const mass = config?.mass ?? 1;
|
|
58
|
+
if (!Number.isFinite(mass) || mass <= 0) throw new RangeError(`spring: mass must be a finite number above 0, got ${mass}`);
|
|
59
|
+
if (!Number.isFinite(stiffness) || stiffness < 0) throw new RangeError(`spring: stiffness must be a finite number >= 0, got ${stiffness}`);
|
|
60
|
+
if (!Number.isFinite(damping) || damping < 0) throw new RangeError(`spring: damping must be a finite number >= 0, got ${damping}`);
|
|
47
61
|
return {
|
|
48
62
|
value,
|
|
49
63
|
velocity: 0,
|
|
50
64
|
target,
|
|
51
65
|
config: {
|
|
52
|
-
stiffness
|
|
53
|
-
damping
|
|
54
|
-
mass
|
|
66
|
+
stiffness,
|
|
67
|
+
damping,
|
|
68
|
+
mass
|
|
55
69
|
},
|
|
56
70
|
atRest: value === target
|
|
57
71
|
};
|
|
58
72
|
}
|
|
59
73
|
/**
|
|
60
74
|
* Update a spring by dt seconds. Returns the current value.
|
|
61
|
-
*
|
|
75
|
+
* Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame
|
|
76
|
+
* or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float
|
|
77
|
+
* rounding).
|
|
62
78
|
*/
|
|
63
79
|
function updateSpring(s, dt) {
|
|
64
80
|
if (s.atRest) return s.value;
|
|
65
81
|
const { stiffness, damping, mass } = s.config;
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
const x0 = s.value - s.target;
|
|
83
|
+
const v0 = s.velocity;
|
|
84
|
+
const gamma = damping / mass;
|
|
85
|
+
let x;
|
|
86
|
+
let v;
|
|
87
|
+
if (stiffness === 0) {
|
|
88
|
+
if (gamma > 0) {
|
|
89
|
+
const e = Math.exp(-gamma * dt);
|
|
90
|
+
x = x0 + v0 * (1 - e) / gamma;
|
|
91
|
+
v = v0 * e;
|
|
92
|
+
} else {
|
|
93
|
+
x = x0 + v0 * dt;
|
|
94
|
+
v = v0;
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
const w0 = Math.sqrt(stiffness / mass);
|
|
98
|
+
const zeta = gamma / (2 * w0);
|
|
99
|
+
if (Math.abs(zeta - 1) <= CRITICAL_BAND) {
|
|
100
|
+
const e = Math.exp(-w0 * dt);
|
|
101
|
+
const b = v0 + w0 * x0;
|
|
102
|
+
x = (x0 + b * dt) * e;
|
|
103
|
+
v = (v0 - w0 * b * dt) * e;
|
|
104
|
+
} else if (zeta < 1) {
|
|
105
|
+
const sigma = zeta * w0;
|
|
106
|
+
const wd = w0 * Math.sqrt(1 - zeta * zeta);
|
|
107
|
+
const e = Math.exp(-sigma * dt);
|
|
108
|
+
const cos = Math.cos(wd * dt);
|
|
109
|
+
const sin = Math.sin(wd * dt);
|
|
110
|
+
const b = (v0 + sigma * x0) / wd;
|
|
111
|
+
x = e * (x0 * cos + b * sin);
|
|
112
|
+
v = e * (v0 * cos - (sigma * b + wd * x0) * sin);
|
|
113
|
+
} else {
|
|
114
|
+
const root = Math.sqrt(zeta * zeta - 1);
|
|
115
|
+
const r1 = -w0 / (zeta + root);
|
|
116
|
+
const r2 = -w0 * (zeta + root);
|
|
117
|
+
const c1 = (v0 - r2 * x0) / (r1 - r2);
|
|
118
|
+
const c2 = x0 - c1;
|
|
119
|
+
const e1 = Math.exp(r1 * dt);
|
|
120
|
+
const e2 = Math.exp(r2 * dt);
|
|
121
|
+
x = c1 * e1 + c2 * e2;
|
|
122
|
+
v = c1 * r1 * e1 + c2 * r2 * e2;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
s.value = s.target + x;
|
|
126
|
+
s.velocity = v;
|
|
70
127
|
if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {
|
|
71
128
|
s.value = s.target;
|
|
72
129
|
s.velocity = 0;
|
|
@@ -202,17 +259,13 @@ function applyModifier(base, mod) {
|
|
|
202
259
|
return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
|
|
203
260
|
}
|
|
204
261
|
// src/tween.ts
|
|
205
|
-
/** Linear easing fallback. */
|
|
206
|
-
function linearEasing(t) {
|
|
207
|
-
return t;
|
|
208
|
-
}
|
|
209
262
|
/** Create a tween. */
|
|
210
263
|
function tween(from, to, duration, easing) {
|
|
211
264
|
return {
|
|
212
265
|
from,
|
|
213
266
|
to,
|
|
214
267
|
duration,
|
|
215
|
-
easing: easing ??
|
|
268
|
+
easing: easing ?? _flare_engine_math.linear,
|
|
216
269
|
elapsed: 0,
|
|
217
270
|
value: from,
|
|
218
271
|
done: false,
|
|
@@ -222,7 +275,7 @@ function tween(from, to, duration, easing) {
|
|
|
222
275
|
}
|
|
223
276
|
/**
|
|
224
277
|
* Update a tween by dt seconds. Returns the current value.
|
|
225
|
-
* When complete, triggers onComplete callback
|
|
278
|
+
* When complete, triggers onComplete callback.
|
|
226
279
|
*/
|
|
227
280
|
function updateTween(tw, dt) {
|
|
228
281
|
if (tw.done) return tw.value;
|
|
@@ -238,7 +291,10 @@ function updateTween(tw, dt) {
|
|
|
238
291
|
tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
|
|
239
292
|
return tw.value;
|
|
240
293
|
}
|
|
241
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* Chain another tween to run after this one completes. Returns the chained tween.
|
|
296
|
+
* Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.
|
|
297
|
+
*/
|
|
242
298
|
function chainTween(tw, next) {
|
|
243
299
|
tw.next = next;
|
|
244
300
|
return next;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["easeInOutSine"],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"sourcesContent":["import { type EasingFn, easeInOutSine } from '@flare-engine/math';\n\n/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */\nexport interface PeriodicTween {\n /** Start value. */\n readonly from: number;\n /** End value. */\n readonly to: number;\n /** Full cycle period in seconds (from → to → from). */\n readonly period: number;\n /** Easing function (default: easeInOutSine). */\n readonly easing: EasingFn;\n /** Elapsed time in seconds — mutable, advanced by updatePeriodicTween. */\n elapsed: number;\n /** Current interpolated value — written by updatePeriodicTween. */\n value: number;\n}\n\n/**\n * Create a periodic oscillating tween.\n *\n * @param from Start value.\n * @param to End value.\n * @param period Full cycle duration in seconds (must be > 0).\n * @param easing Easing function. Defaults to easeInOutSine.\n */\nexport function periodicTween(\n from: number,\n to: number,\n period: number,\n easing: EasingFn = easeInOutSine,\n): PeriodicTween {\n if (period <= 0) throw new Error('periodicTween: period must be > 0');\n return { from, to, period, easing, elapsed: 0, value: from };\n}\n\n/**\n * Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,\n * computes ping-pong t, applies easing, writes `value`, returns it.\n */\nexport function updatePeriodicTween(tw: PeriodicTween, dt: number): number {\n tw.elapsed = (tw.elapsed + dt) % tw.period;\n const halfPeriod = tw.period * 0.5;\n // t in [0,1] ping-pong: forward for first half, backward for second half.\n const halfPhase = tw.elapsed / halfPeriod;\n const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Reset a periodic tween: zero elapsed, restore value to `from`.\n */\nexport function resetPeriodicTween(tw: PeriodicTween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n}\n","/** Spring configuration. */\nexport interface SpringConfig {\n /** Spring stiffness (higher = faster oscillation). Default: 100. */\n stiffness: number;\n /** Damping coefficient (higher = less oscillation). Default: 10. */\n damping: number;\n /** Mass of the spring body. Default: 1. */\n mass: number;\n}\n\n/** Spring state. */\nexport interface Spring {\n /** Current value. */\n value: number;\n /** Current velocity. */\n velocity: number;\n /** Target value. */\n target: number;\n /** Configuration. */\n config: SpringConfig;\n /** Whether the spring has settled (within rest threshold). */\n atRest: boolean;\n}\n\nconst REST_THRESHOLD = 0.001;\nconst VELOCITY_THRESHOLD = 0.001;\n\n/** Create a spring with initial value. */\nexport function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring {\n return {\n value,\n velocity: 0,\n target,\n config: {\n stiffness: config?.stiffness ?? 100,\n damping: config?.damping ?? 10,\n mass: config?.mass ?? 1,\n },\n atRest: value === target,\n };\n}\n\n/**\n * Update a spring by dt seconds. Returns the current value.\n * Uses semi-implicit Euler integration for stability.\n */\nexport function updateSpring(s: Spring, dt: number): number {\n if (s.atRest) return s.value;\n\n const { stiffness, damping, mass } = s.config;\n const displacement = s.value - s.target;\n const springForce = -stiffness * displacement;\n const dampingForce = -damping * s.velocity;\n const acceleration = (springForce + dampingForce) / mass;\n\n // Semi-implicit Euler: update velocity first, then position\n s.velocity += acceleration * dt;\n s.value += s.velocity * dt;\n\n // Check if settled\n if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {\n s.value = s.target;\n s.velocity = 0;\n s.atRest = true;\n }\n\n return s.value;\n}\n\n/** Set a new target for the spring. Wakes it if at rest. */\nexport function setSpringTarget(s: Spring, target: number): void {\n s.target = target;\n s.atRest = false;\n}\n\n/** Reset both value and target, zeroing velocity. */\nexport function resetSpring(s: Spring, value: number): void {\n s.value = value;\n s.target = value;\n s.velocity = 0;\n s.atRest = true;\n}\n","import type { SpriteFrame, SpriteSheet } from '@flare-engine/assets';\n\n/** State of a sprite animator. */\nexport interface SpriteAnimatorState {\n /** The sprite sheet data source. */\n sheet: SpriteSheet;\n /** Current animation name, or null if none. */\n currentAnimation: string | null;\n /** Current frame index within the animation sequence. */\n frameIndex: number;\n /** Accumulated time within current frame (seconds). */\n elapsed: number;\n /** Whether animation is paused. */\n paused: boolean;\n /** Whether the current sequence has finished (non-looping). */\n finished: boolean;\n}\n\n/** Create a sprite animator state bound to a sprite sheet. */\nexport function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState {\n return {\n sheet,\n currentAnimation: null,\n frameIndex: 0,\n elapsed: 0,\n paused: false,\n finished: false,\n };\n}\n\n/** Play a named animation. Resets to frame 0. */\nexport function playAnimation(state: SpriteAnimatorState, name: string): void {\n const seq = state.sheet.animations.get(name);\n if (!seq) return;\n state.currentAnimation = name;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Stop the current animation. */\nexport function stopAnimation(state: SpriteAnimatorState): void {\n state.currentAnimation = null;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Pause the current animation. */\nexport function pauseAnimation(state: SpriteAnimatorState): void {\n state.paused = true;\n}\n\n/** Resume the current animation. */\nexport function resumeAnimation(state: SpriteAnimatorState): void {\n state.paused = false;\n}\n\n/**\n * Tick the animator forward by dt seconds.\n * Returns the current SpriteFrame, or null if no animation is playing.\n */\nexport function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null {\n if (!state.currentAnimation || state.paused || state.finished) {\n return getCurrentFrame(state);\n }\n\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n if (seq.frameDuration <= 0) throw new Error('updateAnimator: frameDuration must be > 0');\n\n state.elapsed += dt;\n\n while (state.elapsed >= seq.frameDuration) {\n state.elapsed -= seq.frameDuration;\n state.frameIndex++;\n\n if (state.frameIndex >= seq.frameIndices.length) {\n if (seq.loop) {\n state.frameIndex = 0;\n } else {\n state.frameIndex = seq.frameIndices.length - 1;\n state.finished = true;\n state.elapsed = 0;\n break;\n }\n }\n }\n\n return getCurrentFrame(state);\n}\n\n/** Get the current SpriteFrame without advancing time. */\nexport function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null {\n if (!state.currentAnimation) return null;\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n const globalIdx = seq.frameIndices[state.frameIndex];\n if (globalIdx === undefined) return null;\n return state.sheet.frames[globalIdx] ?? null;\n}\n","export interface StepSequenceStep {\n readonly duration: number;\n}\n\nexport interface StepSequence {\n readonly steps: readonly StepSequenceStep[];\n current: number;\n elapsed: number;\n}\n\nexport function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence {\n if (steps.length === 0) throw new Error('createStepSequence: steps must not be empty');\n for (let i = 0; i < steps.length; i++) {\n if ((steps[i]?.duration ?? 0) <= 0)\n throw new Error('createStepSequence: each step duration must be > 0');\n }\n return { steps, current: 0, elapsed: 0 };\n}\n\nexport function tickStepSequence(seq: StepSequence, dt: number): number {\n seq.elapsed += dt;\n while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {\n seq.elapsed -= seq.steps[seq.current]!.duration;\n seq.current = (seq.current + 1) % seq.steps.length;\n }\n return seq.current;\n}\n\nexport function resetStepSequence(seq: StepSequence): void {\n seq.current = 0;\n seq.elapsed = 0;\n}\n","export interface TimedModifier<T> {\n readonly value: T;\n readonly combine: (base: T, modifier: T) => T;\n remaining: number;\n}\n\nexport function createTimedModifier<T>(\n value: T,\n duration: number,\n combine: (base: T, modifier: T) => T,\n): TimedModifier<T> {\n if (duration <= 0) throw new Error('createTimedModifier: duration must be > 0');\n return { value, combine, remaining: duration };\n}\n\nexport function tickModifier<T>(mod: TimedModifier<T>, dt: number): void {\n mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;\n}\n\nexport function isModifierActive<T>(mod: TimedModifier<T>): boolean {\n return mod.remaining > 0;\n}\n\n/**\n * Returns combine(base, mod.value) when active, base otherwise.\n * Zero-alloc only when T is number and combine is pure arithmetic.\n */\nexport function applyModifier<T>(base: T, mod: TimedModifier<T>): T {\n return mod.remaining > 0 ? mod.combine(base, mod.value) : base;\n}\n","import type { EasingFn } from '@flare-engine/math';\n\n/** Tween state. */\nexport interface Tween {\n /** Start value. */\n from: number;\n /** End value. */\n to: number;\n /** Duration in seconds. */\n duration: number;\n /** Easing function. */\n easing: EasingFn;\n /** Elapsed time in seconds. */\n elapsed: number;\n /** Current interpolated value. */\n value: number;\n /** Whether the tween is complete. */\n done: boolean;\n /** Optional callback when tween completes. */\n onComplete: (() => void) | null;\n /** Chained tween to start after this one completes. */\n next: Tween | null;\n}\n\n/** Linear easing fallback. */\nfunction linearEasing(t: number): number {\n return t;\n}\n\n/** Create a tween. */\nexport function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween {\n return {\n from,\n to,\n duration,\n easing: easing ?? linearEasing,\n elapsed: 0,\n value: from,\n done: false,\n onComplete: null,\n next: null,\n };\n}\n\n/**\n * Update a tween by dt seconds. Returns the current value.\n * When complete, triggers onComplete callback and advances to chained tween.\n */\nexport function updateTween(tw: Tween, dt: number): number {\n if (tw.done) return tw.value;\n\n tw.elapsed += dt;\n\n if (tw.elapsed >= tw.duration) {\n tw.elapsed = tw.duration;\n tw.value = tw.to;\n tw.done = true;\n tw.onComplete?.();\n return tw.value;\n }\n\n const t = tw.elapsed / tw.duration;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/** Chain another tween to run after this one completes. Returns the chained tween. */\nexport function chainTween(tw: Tween, next: Tween): Tween {\n tw.next = next;\n return next;\n}\n\n/** Reset a tween to its initial state. */\nexport function resetTween(tw: Tween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n tw.done = false;\n}\n","import type { Tween } from './tween.js';\nimport { updateTween } from './tween.js';\n\n/**\n * Manages a collection of active tweens.\n * Automatically removes completed tweens (or advances to chained tweens).\n */\nexport interface TweenManager {\n /** Active tweens. */\n readonly tweens: Tween[];\n}\n\n/** Create a new TweenManager. */\nexport function createTweenManager(): TweenManager {\n return { tweens: [] };\n}\n\n/** Add a tween to the manager. */\nexport function addTween(mgr: TweenManager, tw: Tween): void {\n (mgr.tweens as Tween[]).push(tw);\n}\n\n/**\n * Update all managed tweens by dt seconds.\n * Completed tweens are removed; chained tweens replace completed ones.\n */\nexport function updateTweens(mgr: TweenManager, dt: number): void {\n const tweens = mgr.tweens as Tween[];\n let i = 0;\n while (i < tweens.length) {\n const tw: Tween = tweens[i]!;\n updateTween(tw, dt);\n if (tw.done) {\n if (tw.next) {\n // Replace with chained tween\n tweens[i] = tw.next;\n i++;\n } else {\n // Swap-remove\n tweens[i] = tweens[tweens.length - 1]!;\n tweens.pop();\n }\n } else {\n i++;\n }\n }\n}\n\n/** Remove all tweens. */\nexport function clearTweens(mgr: TweenManager): void {\n (mgr.tweens as Tween[]).length = 0;\n}\n"],"mappings":";;;;;;;;;;;AA0BA,SAAgB,cACd,MACA,IACA,QACA,SAAmBA,mBAAAA,eACJ;CACf,IAAI,UAAU,GAAG,MAAM,IAAI,MAAM,mCAAmC;CACpE,OAAO;EAAE;EAAM;EAAI;EAAQ;EAAQ,SAAS;EAAG,OAAO;CAAK;AAC7D;;;;;AAMA,SAAgB,oBAAoB,IAAmB,IAAoB;CACzE,GAAG,WAAW,GAAG,UAAU,MAAM,GAAG;CACpC,MAAM,aAAa,GAAG,SAAS;CAE/B,MAAM,YAAY,GAAG,UAAU;CAC/B,MAAM,IAAI,aAAa,IAAI,YAAY,IAAI;CAC3C,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;AAKA,SAAgB,mBAAmB,IAAyB;CAC1D,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;AAChB;;AChCA,MAAM,iBAAiB;AACvB,MAAM,qBAAqB;;AAG3B,SAAgB,OAAO,OAAe,QAAgB,QAAwC;CAC5F,OAAO;EACL;EACA,UAAU;EACV;EACA,QAAQ;GACN,WAAW,QAAQ,aAAa;GAChC,SAAS,QAAQ,WAAW;GAC5B,MAAM,QAAQ,QAAQ;EACxB;EACA,QAAQ,UAAU;CACpB;AACF;;;;;AAMA,SAAgB,aAAa,GAAW,IAAoB;CAC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AgB,WAAW,IAAiB;CAC1C,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;CACd,GAAG,OAAO;AACZ;;;AChEA,SAAgB,qBAAmC;CACjD,OAAO,EAAE,QAAQ,CAAC,EAAE;AACtB;;AAGA,SAAgB,SAAS,KAAmB,IAAiB;CAC3D,IAAK,OAAmB,KAAK,EAAE;AACjC;;;;;AAMA,SAAgB,aAAa,KAAmB,IAAkB;CAChE,MAAM,SAAS,IAAI;CACnB,IAAI,IAAI;CACR,OAAO,IAAI,OAAO,QAAQ;EACxB,MAAM,KAAY,OAAO;EACzB,YAAY,IAAI,EAAE;EAClB,IAAI,GAAG,MAAM;GACX,IAAI,GAAG,MAAM;IAEX,OAAO,KAAK,GAAG;IACf;GACF,OAAO;IAEL,OAAO,KAAK,OAAO,OAAO,SAAS;IACnC,OAAO,IAAI;GACb;EACF,OACE;CAEJ;AACF;;AAGA,SAAgB,YAAY,KAAyB;CACnD,IAAK,OAAmB,SAAS;AACnC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["easeInOutSine","linear"],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"sourcesContent":["import { type EasingFn, easeInOutSine } from '@flare-engine/math';\n\n/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */\nexport interface PeriodicTween {\n /** Start value. */\n readonly from: number;\n /** End value. */\n readonly to: number;\n /** Full cycle period in seconds (from → to → from). */\n readonly period: number;\n /** Easing function (default: easeInOutSine). */\n readonly easing: EasingFn;\n /** Elapsed time in seconds — mutable, advanced by updatePeriodicTween. */\n elapsed: number;\n /** Current interpolated value — written by updatePeriodicTween. */\n value: number;\n}\n\n/**\n * Create a periodic oscillating tween.\n *\n * @param from Start value.\n * @param to End value.\n * @param period Full cycle duration in seconds (must be > 0).\n * @param easing Easing function. Defaults to easeInOutSine.\n */\nexport function periodicTween(\n from: number,\n to: number,\n period: number,\n easing: EasingFn = easeInOutSine,\n): PeriodicTween {\n if (period <= 0) throw new Error('periodicTween: period must be > 0');\n return { from, to, period, easing, elapsed: 0, value: from };\n}\n\n/**\n * Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,\n * computes ping-pong t, applies easing, writes `value`, returns it.\n */\nexport function updatePeriodicTween(tw: PeriodicTween, dt: number): number {\n tw.elapsed = (tw.elapsed + dt) % tw.period;\n const halfPeriod = tw.period * 0.5;\n // t in [0,1] ping-pong: forward for first half, backward for second half.\n const halfPhase = tw.elapsed / halfPeriod;\n const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Reset a periodic tween: zero elapsed, restore value to `from`.\n */\nexport function resetPeriodicTween(tw: PeriodicTween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n}\n","/** Spring configuration. */\nexport interface SpringConfig {\n /** Spring stiffness (higher = faster oscillation). Default: 100. */\n stiffness: number;\n /** Damping coefficient (higher = less oscillation). Default: 10. */\n damping: number;\n /** Mass of the spring body. Default: 1. */\n mass: number;\n}\n\n/** Spring state. */\nexport interface Spring {\n /** Current value. */\n value: number;\n /** Current velocity. */\n velocity: number;\n /** Target value. */\n target: number;\n /** Configuration. */\n config: SpringConfig;\n /** Whether the spring has settled (within rest threshold). */\n atRest: boolean;\n}\n\nconst REST_THRESHOLD = 0.001;\nconst VELOCITY_THRESHOLD = 0.001;\n// |ζ − 1| inside this band is solved as critically damped: the under/overdamped forms divide by\n// a vanishing ωd / root gap there, and the critical solution differs by under 1e-6·ω0·dt relative.\nconst CRITICAL_BAND = 1e-6;\n\n/**\n * Create a spring with initial value.\n *\n * Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`\n * is negative, or any of the three is not finite: none of those ever produced a usable spring.\n * `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later\n * edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.\n */\nexport function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring {\n const stiffness = config?.stiffness ?? 100;\n const damping = config?.damping ?? 10;\n const mass = config?.mass ?? 1;\n if (!Number.isFinite(mass) || mass <= 0) {\n throw new RangeError(`spring: mass must be a finite number above 0, got ${mass}`);\n }\n if (!Number.isFinite(stiffness) || stiffness < 0) {\n throw new RangeError(`spring: stiffness must be a finite number >= 0, got ${stiffness}`);\n }\n if (!Number.isFinite(damping) || damping < 0) {\n throw new RangeError(`spring: damping must be a finite number >= 0, got ${damping}`);\n }\n return {\n value,\n velocity: 0,\n target,\n config: { stiffness, damping, mass },\n atRest: value === target,\n };\n}\n\n/**\n * Update a spring by dt seconds. Returns the current value.\n * Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame\n * or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float\n * rounding).\n */\nexport function updateSpring(s: Spring, dt: number): number {\n if (s.atRest) return s.value;\n\n const { stiffness, damping, mass } = s.config;\n const x0 = s.value - s.target;\n const v0 = s.velocity;\n const gamma = damping / mass;\n let x: number;\n let v: number;\n\n if (stiffness === 0) {\n if (gamma > 0) {\n const e = Math.exp(-gamma * dt);\n x = x0 + (v0 * (1 - e)) / gamma;\n v = v0 * e;\n } else {\n x = x0 + v0 * dt;\n v = v0;\n }\n } else {\n const w0 = Math.sqrt(stiffness / mass);\n const zeta = gamma / (2 * w0);\n if (Math.abs(zeta - 1) <= CRITICAL_BAND) {\n // Critical: x = (x0 + (v0 + ω0·x0)·t)·e^(−ω0·t)\n const e = Math.exp(-w0 * dt);\n const b = v0 + w0 * x0;\n x = (x0 + b * dt) * e;\n v = (v0 - w0 * b * dt) * e;\n } else if (zeta < 1) {\n // Underdamped: x = e^(−σt)·(x0·cos ωd·t + B·sin ωd·t), σ = ζω0, B = (v0 + σ·x0)/ωd\n const sigma = zeta * w0;\n const wd = w0 * Math.sqrt(1 - zeta * zeta);\n const e = Math.exp(-sigma * dt);\n const cos = Math.cos(wd * dt);\n const sin = Math.sin(wd * dt);\n const b = (v0 + sigma * x0) / wd;\n x = e * (x0 * cos + b * sin);\n v = e * (v0 * cos - (sigma * b + wd * x0) * sin);\n } else {\n // Overdamped: x = c1·e^(r1·t) + c2·e^(r2·t) with real roots r2 < r1 < 0;\n // r1 = −ω0/(ζ + √(ζ²−1)) equals −ω0·(ζ − √(ζ²−1)) without the cancellation.\n const root = Math.sqrt(zeta * zeta - 1);\n const r1 = -w0 / (zeta + root);\n const r2 = -w0 * (zeta + root);\n const c1 = (v0 - r2 * x0) / (r1 - r2);\n const c2 = x0 - c1;\n const e1 = Math.exp(r1 * dt);\n const e2 = Math.exp(r2 * dt);\n x = c1 * e1 + c2 * e2;\n v = c1 * r1 * e1 + c2 * r2 * e2;\n }\n }\n\n s.value = s.target + x;\n s.velocity = v;\n\n // Check if settled\n if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {\n s.value = s.target;\n s.velocity = 0;\n s.atRest = true;\n }\n\n return s.value;\n}\n\n/** Set a new target for the spring. Wakes it if at rest. */\nexport function setSpringTarget(s: Spring, target: number): void {\n s.target = target;\n s.atRest = false;\n}\n\n/** Reset both value and target, zeroing velocity. */\nexport function resetSpring(s: Spring, value: number): void {\n s.value = value;\n s.target = value;\n s.velocity = 0;\n s.atRest = true;\n}\n","import type { SpriteFrame, SpriteSheet } from '@flare-engine/assets';\n\n/** State of a sprite animator. */\nexport interface SpriteAnimatorState {\n /** The sprite sheet data source. */\n sheet: SpriteSheet;\n /** Current animation name, or null if none. */\n currentAnimation: string | null;\n /** Current frame index within the animation sequence. */\n frameIndex: number;\n /** Accumulated time within current frame (seconds). */\n elapsed: number;\n /** Whether animation is paused. */\n paused: boolean;\n /** Whether the current sequence has finished (non-looping). */\n finished: boolean;\n}\n\n/** Create a sprite animator state bound to a sprite sheet. */\nexport function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState {\n return {\n sheet,\n currentAnimation: null,\n frameIndex: 0,\n elapsed: 0,\n paused: false,\n finished: false,\n };\n}\n\n/** Play a named animation. Resets to frame 0. */\nexport function playAnimation(state: SpriteAnimatorState, name: string): void {\n const seq = state.sheet.animations.get(name);\n if (!seq) return;\n state.currentAnimation = name;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Stop the current animation. */\nexport function stopAnimation(state: SpriteAnimatorState): void {\n state.currentAnimation = null;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Pause the current animation. */\nexport function pauseAnimation(state: SpriteAnimatorState): void {\n state.paused = true;\n}\n\n/** Resume the current animation. */\nexport function resumeAnimation(state: SpriteAnimatorState): void {\n state.paused = false;\n}\n\n/**\n * Tick the animator forward by dt seconds.\n * Returns the current SpriteFrame, or null if no animation is playing.\n */\nexport function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null {\n if (!state.currentAnimation || state.paused || state.finished) {\n return getCurrentFrame(state);\n }\n\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n if (seq.frameDuration <= 0) throw new Error('updateAnimator: frameDuration must be > 0');\n\n state.elapsed += dt;\n\n while (state.elapsed >= seq.frameDuration) {\n state.elapsed -= seq.frameDuration;\n state.frameIndex++;\n\n if (state.frameIndex >= seq.frameIndices.length) {\n if (seq.loop) {\n state.frameIndex = 0;\n } else {\n state.frameIndex = seq.frameIndices.length - 1;\n state.finished = true;\n state.elapsed = 0;\n break;\n }\n }\n }\n\n return getCurrentFrame(state);\n}\n\n/** Get the current SpriteFrame without advancing time. */\nexport function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null {\n if (!state.currentAnimation) return null;\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n const globalIdx = seq.frameIndices[state.frameIndex];\n if (globalIdx === undefined) return null;\n return state.sheet.frames[globalIdx] ?? null;\n}\n","export interface StepSequenceStep {\n readonly duration: number;\n}\n\nexport interface StepSequence {\n readonly steps: readonly StepSequenceStep[];\n current: number;\n elapsed: number;\n}\n\nexport function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence {\n if (steps.length === 0) throw new Error('createStepSequence: steps must not be empty');\n for (let i = 0; i < steps.length; i++) {\n if ((steps[i]?.duration ?? 0) <= 0)\n throw new Error('createStepSequence: each step duration must be > 0');\n }\n return { steps, current: 0, elapsed: 0 };\n}\n\nexport function tickStepSequence(seq: StepSequence, dt: number): number {\n seq.elapsed += dt;\n while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {\n seq.elapsed -= seq.steps[seq.current]!.duration;\n seq.current = (seq.current + 1) % seq.steps.length;\n }\n return seq.current;\n}\n\nexport function resetStepSequence(seq: StepSequence): void {\n seq.current = 0;\n seq.elapsed = 0;\n}\n","export interface TimedModifier<T> {\n readonly value: T;\n readonly combine: (base: T, modifier: T) => T;\n remaining: number;\n}\n\nexport function createTimedModifier<T>(\n value: T,\n duration: number,\n combine: (base: T, modifier: T) => T,\n): TimedModifier<T> {\n if (duration <= 0) throw new Error('createTimedModifier: duration must be > 0');\n return { value, combine, remaining: duration };\n}\n\nexport function tickModifier<T>(mod: TimedModifier<T>, dt: number): void {\n mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;\n}\n\nexport function isModifierActive<T>(mod: TimedModifier<T>): boolean {\n return mod.remaining > 0;\n}\n\n/**\n * Returns combine(base, mod.value) when active, base otherwise.\n * Zero-alloc only when T is number and combine is pure arithmetic.\n */\nexport function applyModifier<T>(base: T, mod: TimedModifier<T>): T {\n return mod.remaining > 0 ? mod.combine(base, mod.value) : base;\n}\n","import { type EasingFn, linear } from '@flare-engine/math';\n\n/** Tween state. */\nexport interface Tween {\n /** Start value. */\n from: number;\n /** End value. */\n to: number;\n /** Duration in seconds. */\n duration: number;\n /** Easing function. */\n easing: EasingFn;\n /** Elapsed time in seconds. */\n elapsed: number;\n /** Current interpolated value. */\n value: number;\n /** Whether the tween is complete. */\n done: boolean;\n /** Optional callback when tween completes. */\n onComplete: (() => void) | null;\n /** Chained tween to start after this one completes. */\n next: Tween | null;\n}\n\n/** Create a tween. */\nexport function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween {\n return {\n from,\n to,\n duration,\n easing: easing ?? linear,\n elapsed: 0,\n value: from,\n done: false,\n onComplete: null,\n next: null,\n };\n}\n\n/**\n * Update a tween by dt seconds. Returns the current value.\n * When complete, triggers onComplete callback.\n */\nexport function updateTween(tw: Tween, dt: number): number {\n if (tw.done) return tw.value;\n\n tw.elapsed += dt;\n\n if (tw.elapsed >= tw.duration) {\n tw.elapsed = tw.duration;\n tw.value = tw.to;\n tw.done = true;\n tw.onComplete?.();\n return tw.value;\n }\n\n const t = tw.elapsed / tw.duration;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Chain another tween to run after this one completes. Returns the chained tween.\n * Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.\n */\nexport function chainTween(tw: Tween, next: Tween): Tween {\n tw.next = next;\n return next;\n}\n\n/** Reset a tween to its initial state. */\nexport function resetTween(tw: Tween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n tw.done = false;\n}\n","import type { Tween } from './tween.js';\nimport { updateTween } from './tween.js';\n\n/**\n * Manages a collection of active tweens.\n * Automatically removes completed tweens (or advances to chained tweens).\n */\nexport interface TweenManager {\n /** Active tweens. */\n readonly tweens: Tween[];\n}\n\n/** Create a new TweenManager. */\nexport function createTweenManager(): TweenManager {\n return { tweens: [] };\n}\n\n/** Add a tween to the manager. */\nexport function addTween(mgr: TweenManager, tw: Tween): void {\n (mgr.tweens as Tween[]).push(tw);\n}\n\n/**\n * Update all managed tweens by dt seconds.\n * Completed tweens are removed; chained tweens replace completed ones.\n */\nexport function updateTweens(mgr: TweenManager, dt: number): void {\n const tweens = mgr.tweens as Tween[];\n let i = 0;\n while (i < tweens.length) {\n const tw: Tween = tweens[i]!;\n updateTween(tw, dt);\n if (tw.done) {\n if (tw.next) {\n // Replace with chained tween\n tweens[i] = tw.next;\n i++;\n } else {\n // Swap-remove\n tweens[i] = tweens[tweens.length - 1]!;\n tweens.pop();\n }\n } else {\n i++;\n }\n }\n}\n\n/** Remove all tweens. */\nexport function clearTweens(mgr: TweenManager): void {\n (mgr.tweens as Tween[]).length = 0;\n}\n"],"mappings":";;;;;;;;;;;AA0BA,SAAgB,cACd,MACA,IACA,QACA,SAAmBA,mBAAAA,eACJ;CACf,IAAI,UAAU,GAAG,MAAM,IAAI,MAAM,mCAAmC;CACpE,OAAO;EAAE;EAAM;EAAI;EAAQ;EAAQ,SAAS;EAAG,OAAO;CAAK;AAC7D;;;;;AAMA,SAAgB,oBAAoB,IAAmB,IAAoB;CACzE,GAAG,WAAW,GAAG,UAAU,MAAM,GAAG;CACpC,MAAM,aAAa,GAAG,SAAS;CAE/B,MAAM,YAAY,GAAG,UAAU;CAC/B,MAAM,IAAI,aAAa,IAAI,YAAY,IAAI;CAC3C,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;AAKA,SAAgB,mBAAmB,IAAyB;CAC1D,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;AAChB;;AChCA,MAAM,iBAAiB;AACvB,MAAM,qBAAqB;AAG3B,MAAM,gBAAgB;;;;;;;;;AAUtB,SAAgB,OAAO,OAAe,QAAgB,QAAwC;CAC5F,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,QAAQ,QAAQ;CAC7B,IAAI,CAAC,OAAO,SAAS,IAAI,KAAK,QAAQ,GACpC,MAAM,IAAI,WAAW,qDAAqD,MAAM;CAElF,IAAI,CAAC,OAAO,SAAS,SAAS,KAAK,YAAY,GAC7C,MAAM,IAAI,WAAW,uDAAuD,WAAW;CAEzF,IAAI,CAAC,OAAO,SAA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AG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;;AAMA,SAAgB,WAAW,IAAW,MAAoB;CACxD,GAAG,OAAO;CACV,OAAO;AACT;;AAGA,SAAgB,WAAW,IAAiB;CAC1C,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;CACd,GAAG,OAAO;AACZ;;;AC9DA,SAAgB,qBAAmC;CACjD,OAAO,EAAE,QAAQ,CAAC,EAAE;AACtB;;AAGA,SAAgB,SAAS,KAAmB,IAAiB;CAC3D,IAAK,OAAmB,KAAK,EAAE;AACjC;;;;;AAMA,SAAgB,aAAa,KAAmB,IAAkB;CAChE,MAAM,SAAS,IAAI;CACnB,IAAI,IAAI;CACR,OAAO,IAAI,OAAO,QAAQ;EACxB,MAAM,KAAY,OAAO;EACzB,YAAY,IAAI,EAAE;EAClB,IAAI,GAAG,MAAM;GACX,IAAI,GAAG,MAAM;IAEX,OAAO,KAAK,GAAG;IACf;GACF,OAAO;IAEL,OAAO,KAAK,OAAO,OAAO,SAAS;IACnC,OAAO,IAAI;GACb;EACF,OACE;CAEJ;AACF;;AAGA,SAAgB,YAAY,KAAyB;CACnD,IAAK,OAAmB,SAAS;AACnC"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { easeInOutSine } from "@flare-engine/math";
|
|
1
|
+
import { easeInOutSine, linear } from "@flare-engine/math";
|
|
2
2
|
// src/periodic.ts
|
|
3
3
|
/**
|
|
4
4
|
* Create a periodic oscillating tween.
|
|
@@ -41,31 +41,88 @@ function resetPeriodicTween(tw) {
|
|
|
41
41
|
// src/spring.ts
|
|
42
42
|
const REST_THRESHOLD = .001;
|
|
43
43
|
const VELOCITY_THRESHOLD = .001;
|
|
44
|
-
|
|
44
|
+
const CRITICAL_BAND = 1e-6;
|
|
45
|
+
/**
|
|
46
|
+
* Create a spring with initial value.
|
|
47
|
+
*
|
|
48
|
+
* Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`
|
|
49
|
+
* is negative, or any of the three is not finite: none of those ever produced a usable spring.
|
|
50
|
+
* `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later
|
|
51
|
+
* edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.
|
|
52
|
+
*/
|
|
45
53
|
function spring(value, target, config) {
|
|
54
|
+
const stiffness = config?.stiffness ?? 100;
|
|
55
|
+
const damping = config?.damping ?? 10;
|
|
56
|
+
const mass = config?.mass ?? 1;
|
|
57
|
+
if (!Number.isFinite(mass) || mass <= 0) throw new RangeError(`spring: mass must be a finite number above 0, got ${mass}`);
|
|
58
|
+
if (!Number.isFinite(stiffness) || stiffness < 0) throw new RangeError(`spring: stiffness must be a finite number >= 0, got ${stiffness}`);
|
|
59
|
+
if (!Number.isFinite(damping) || damping < 0) throw new RangeError(`spring: damping must be a finite number >= 0, got ${damping}`);
|
|
46
60
|
return {
|
|
47
61
|
value,
|
|
48
62
|
velocity: 0,
|
|
49
63
|
target,
|
|
50
64
|
config: {
|
|
51
|
-
stiffness
|
|
52
|
-
damping
|
|
53
|
-
mass
|
|
65
|
+
stiffness,
|
|
66
|
+
damping,
|
|
67
|
+
mass
|
|
54
68
|
},
|
|
55
69
|
atRest: value === target
|
|
56
70
|
};
|
|
57
71
|
}
|
|
58
72
|
/**
|
|
59
73
|
* Update a spring by dt seconds. Returns the current value.
|
|
60
|
-
*
|
|
74
|
+
* Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame
|
|
75
|
+
* or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float
|
|
76
|
+
* rounding).
|
|
61
77
|
*/
|
|
62
78
|
function updateSpring(s, dt) {
|
|
63
79
|
if (s.atRest) return s.value;
|
|
64
80
|
const { stiffness, damping, mass } = s.config;
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
81
|
+
const x0 = s.value - s.target;
|
|
82
|
+
const v0 = s.velocity;
|
|
83
|
+
const gamma = damping / mass;
|
|
84
|
+
let x;
|
|
85
|
+
let v;
|
|
86
|
+
if (stiffness === 0) {
|
|
87
|
+
if (gamma > 0) {
|
|
88
|
+
const e = Math.exp(-gamma * dt);
|
|
89
|
+
x = x0 + v0 * (1 - e) / gamma;
|
|
90
|
+
v = v0 * e;
|
|
91
|
+
} else {
|
|
92
|
+
x = x0 + v0 * dt;
|
|
93
|
+
v = v0;
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
const w0 = Math.sqrt(stiffness / mass);
|
|
97
|
+
const zeta = gamma / (2 * w0);
|
|
98
|
+
if (Math.abs(zeta - 1) <= CRITICAL_BAND) {
|
|
99
|
+
const e = Math.exp(-w0 * dt);
|
|
100
|
+
const b = v0 + w0 * x0;
|
|
101
|
+
x = (x0 + b * dt) * e;
|
|
102
|
+
v = (v0 - w0 * b * dt) * e;
|
|
103
|
+
} else if (zeta < 1) {
|
|
104
|
+
const sigma = zeta * w0;
|
|
105
|
+
const wd = w0 * Math.sqrt(1 - zeta * zeta);
|
|
106
|
+
const e = Math.exp(-sigma * dt);
|
|
107
|
+
const cos = Math.cos(wd * dt);
|
|
108
|
+
const sin = Math.sin(wd * dt);
|
|
109
|
+
const b = (v0 + sigma * x0) / wd;
|
|
110
|
+
x = e * (x0 * cos + b * sin);
|
|
111
|
+
v = e * (v0 * cos - (sigma * b + wd * x0) * sin);
|
|
112
|
+
} else {
|
|
113
|
+
const root = Math.sqrt(zeta * zeta - 1);
|
|
114
|
+
const r1 = -w0 / (zeta + root);
|
|
115
|
+
const r2 = -w0 * (zeta + root);
|
|
116
|
+
const c1 = (v0 - r2 * x0) / (r1 - r2);
|
|
117
|
+
const c2 = x0 - c1;
|
|
118
|
+
const e1 = Math.exp(r1 * dt);
|
|
119
|
+
const e2 = Math.exp(r2 * dt);
|
|
120
|
+
x = c1 * e1 + c2 * e2;
|
|
121
|
+
v = c1 * r1 * e1 + c2 * r2 * e2;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
s.value = s.target + x;
|
|
125
|
+
s.velocity = v;
|
|
69
126
|
if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {
|
|
70
127
|
s.value = s.target;
|
|
71
128
|
s.velocity = 0;
|
|
@@ -201,17 +258,13 @@ function applyModifier(base, mod) {
|
|
|
201
258
|
return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
|
|
202
259
|
}
|
|
203
260
|
// src/tween.ts
|
|
204
|
-
/** Linear easing fallback. */
|
|
205
|
-
function linearEasing(t) {
|
|
206
|
-
return t;
|
|
207
|
-
}
|
|
208
261
|
/** Create a tween. */
|
|
209
262
|
function tween(from, to, duration, easing) {
|
|
210
263
|
return {
|
|
211
264
|
from,
|
|
212
265
|
to,
|
|
213
266
|
duration,
|
|
214
|
-
easing: easing ??
|
|
267
|
+
easing: easing ?? linear,
|
|
215
268
|
elapsed: 0,
|
|
216
269
|
value: from,
|
|
217
270
|
done: false,
|
|
@@ -221,7 +274,7 @@ function tween(from, to, duration, easing) {
|
|
|
221
274
|
}
|
|
222
275
|
/**
|
|
223
276
|
* Update a tween by dt seconds. Returns the current value.
|
|
224
|
-
* When complete, triggers onComplete callback
|
|
277
|
+
* When complete, triggers onComplete callback.
|
|
225
278
|
*/
|
|
226
279
|
function updateTween(tw, dt) {
|
|
227
280
|
if (tw.done) return tw.value;
|
|
@@ -237,7 +290,10 @@ function updateTween(tw, dt) {
|
|
|
237
290
|
tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
|
|
238
291
|
return tw.value;
|
|
239
292
|
}
|
|
240
|
-
/**
|
|
293
|
+
/**
|
|
294
|
+
* Chain another tween to run after this one completes. Returns the chained tween.
|
|
295
|
+
* Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.
|
|
296
|
+
*/
|
|
241
297
|
function chainTween(tw, next) {
|
|
242
298
|
tw.next = next;
|
|
243
299
|
return next;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"sourcesContent":["import { type EasingFn, easeInOutSine } from '@flare-engine/math';\n\n/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */\nexport interface PeriodicTween {\n /** Start value. */\n readonly from: number;\n /** End value. */\n readonly to: number;\n /** Full cycle period in seconds (from → to → from). */\n readonly period: number;\n /** Easing function (default: easeInOutSine). */\n readonly easing: EasingFn;\n /** Elapsed time in seconds — mutable, advanced by updatePeriodicTween. */\n elapsed: number;\n /** Current interpolated value — written by updatePeriodicTween. */\n value: number;\n}\n\n/**\n * Create a periodic oscillating tween.\n *\n * @param from Start value.\n * @param to End value.\n * @param period Full cycle duration in seconds (must be > 0).\n * @param easing Easing function. Defaults to easeInOutSine.\n */\nexport function periodicTween(\n from: number,\n to: number,\n period: number,\n easing: EasingFn = easeInOutSine,\n): PeriodicTween {\n if (period <= 0) throw new Error('periodicTween: period must be > 0');\n return { from, to, period, easing, elapsed: 0, value: from };\n}\n\n/**\n * Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,\n * computes ping-pong t, applies easing, writes `value`, returns it.\n */\nexport function updatePeriodicTween(tw: PeriodicTween, dt: number): number {\n tw.elapsed = (tw.elapsed + dt) % tw.period;\n const halfPeriod = tw.period * 0.5;\n // t in [0,1] ping-pong: forward for first half, backward for second half.\n const halfPhase = tw.elapsed / halfPeriod;\n const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Reset a periodic tween: zero elapsed, restore value to `from`.\n */\nexport function resetPeriodicTween(tw: PeriodicTween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n}\n","/** Spring configuration. */\nexport interface SpringConfig {\n /** Spring stiffness (higher = faster oscillation). Default: 100. */\n stiffness: number;\n /** Damping coefficient (higher = less oscillation). Default: 10. */\n damping: number;\n /** Mass of the spring body. Default: 1. */\n mass: number;\n}\n\n/** Spring state. */\nexport interface Spring {\n /** Current value. */\n value: number;\n /** Current velocity. */\n velocity: number;\n /** Target value. */\n target: number;\n /** Configuration. */\n config: SpringConfig;\n /** Whether the spring has settled (within rest threshold). */\n atRest: boolean;\n}\n\nconst REST_THRESHOLD = 0.001;\nconst VELOCITY_THRESHOLD = 0.001;\n\n/** Create a spring with initial value. */\nexport function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring {\n return {\n value,\n velocity: 0,\n target,\n config: {\n stiffness: config?.stiffness ?? 100,\n damping: config?.damping ?? 10,\n mass: config?.mass ?? 1,\n },\n atRest: value === target,\n };\n}\n\n/**\n * Update a spring by dt seconds. Returns the current value.\n * Uses semi-implicit Euler integration for stability.\n */\nexport function updateSpring(s: Spring, dt: number): number {\n if (s.atRest) return s.value;\n\n const { stiffness, damping, mass } = s.config;\n const displacement = s.value - s.target;\n const springForce = -stiffness * displacement;\n const dampingForce = -damping * s.velocity;\n const acceleration = (springForce + dampingForce) / mass;\n\n // Semi-implicit Euler: update velocity first, then position\n s.velocity += acceleration * dt;\n s.value += s.velocity * dt;\n\n // Check if settled\n if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {\n s.value = s.target;\n s.velocity = 0;\n s.atRest = true;\n }\n\n return s.value;\n}\n\n/** Set a new target for the spring. Wakes it if at rest. */\nexport function setSpringTarget(s: Spring, target: number): void {\n s.target = target;\n s.atRest = false;\n}\n\n/** Reset both value and target, zeroing velocity. */\nexport function resetSpring(s: Spring, value: number): void {\n s.value = value;\n s.target = value;\n s.velocity = 0;\n s.atRest = true;\n}\n","import type { SpriteFrame, SpriteSheet } from '@flare-engine/assets';\n\n/** State of a sprite animator. */\nexport interface SpriteAnimatorState {\n /** The sprite sheet data source. */\n sheet: SpriteSheet;\n /** Current animation name, or null if none. */\n currentAnimation: string | null;\n /** Current frame index within the animation sequence. */\n frameIndex: number;\n /** Accumulated time within current frame (seconds). */\n elapsed: number;\n /** Whether animation is paused. */\n paused: boolean;\n /** Whether the current sequence has finished (non-looping). */\n finished: boolean;\n}\n\n/** Create a sprite animator state bound to a sprite sheet. */\nexport function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState {\n return {\n sheet,\n currentAnimation: null,\n frameIndex: 0,\n elapsed: 0,\n paused: false,\n finished: false,\n };\n}\n\n/** Play a named animation. Resets to frame 0. */\nexport function playAnimation(state: SpriteAnimatorState, name: string): void {\n const seq = state.sheet.animations.get(name);\n if (!seq) return;\n state.currentAnimation = name;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Stop the current animation. */\nexport function stopAnimation(state: SpriteAnimatorState): void {\n state.currentAnimation = null;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Pause the current animation. */\nexport function pauseAnimation(state: SpriteAnimatorState): void {\n state.paused = true;\n}\n\n/** Resume the current animation. */\nexport function resumeAnimation(state: SpriteAnimatorState): void {\n state.paused = false;\n}\n\n/**\n * Tick the animator forward by dt seconds.\n * Returns the current SpriteFrame, or null if no animation is playing.\n */\nexport function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null {\n if (!state.currentAnimation || state.paused || state.finished) {\n return getCurrentFrame(state);\n }\n\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n if (seq.frameDuration <= 0) throw new Error('updateAnimator: frameDuration must be > 0');\n\n state.elapsed += dt;\n\n while (state.elapsed >= seq.frameDuration) {\n state.elapsed -= seq.frameDuration;\n state.frameIndex++;\n\n if (state.frameIndex >= seq.frameIndices.length) {\n if (seq.loop) {\n state.frameIndex = 0;\n } else {\n state.frameIndex = seq.frameIndices.length - 1;\n state.finished = true;\n state.elapsed = 0;\n break;\n }\n }\n }\n\n return getCurrentFrame(state);\n}\n\n/** Get the current SpriteFrame without advancing time. */\nexport function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null {\n if (!state.currentAnimation) return null;\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n const globalIdx = seq.frameIndices[state.frameIndex];\n if (globalIdx === undefined) return null;\n return state.sheet.frames[globalIdx] ?? null;\n}\n","export interface StepSequenceStep {\n readonly duration: number;\n}\n\nexport interface StepSequence {\n readonly steps: readonly StepSequenceStep[];\n current: number;\n elapsed: number;\n}\n\nexport function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence {\n if (steps.length === 0) throw new Error('createStepSequence: steps must not be empty');\n for (let i = 0; i < steps.length; i++) {\n if ((steps[i]?.duration ?? 0) <= 0)\n throw new Error('createStepSequence: each step duration must be > 0');\n }\n return { steps, current: 0, elapsed: 0 };\n}\n\nexport function tickStepSequence(seq: StepSequence, dt: number): number {\n seq.elapsed += dt;\n while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {\n seq.elapsed -= seq.steps[seq.current]!.duration;\n seq.current = (seq.current + 1) % seq.steps.length;\n }\n return seq.current;\n}\n\nexport function resetStepSequence(seq: StepSequence): void {\n seq.current = 0;\n seq.elapsed = 0;\n}\n","export interface TimedModifier<T> {\n readonly value: T;\n readonly combine: (base: T, modifier: T) => T;\n remaining: number;\n}\n\nexport function createTimedModifier<T>(\n value: T,\n duration: number,\n combine: (base: T, modifier: T) => T,\n): TimedModifier<T> {\n if (duration <= 0) throw new Error('createTimedModifier: duration must be > 0');\n return { value, combine, remaining: duration };\n}\n\nexport function tickModifier<T>(mod: TimedModifier<T>, dt: number): void {\n mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;\n}\n\nexport function isModifierActive<T>(mod: TimedModifier<T>): boolean {\n return mod.remaining > 0;\n}\n\n/**\n * Returns combine(base, mod.value) when active, base otherwise.\n * Zero-alloc only when T is number and combine is pure arithmetic.\n */\nexport function applyModifier<T>(base: T, mod: TimedModifier<T>): T {\n return mod.remaining > 0 ? mod.combine(base, mod.value) : base;\n}\n","import type { EasingFn } from '@flare-engine/math';\n\n/** Tween state. */\nexport interface Tween {\n /** Start value. */\n from: number;\n /** End value. */\n to: number;\n /** Duration in seconds. */\n duration: number;\n /** Easing function. */\n easing: EasingFn;\n /** Elapsed time in seconds. */\n elapsed: number;\n /** Current interpolated value. */\n value: number;\n /** Whether the tween is complete. */\n done: boolean;\n /** Optional callback when tween completes. */\n onComplete: (() => void) | null;\n /** Chained tween to start after this one completes. */\n next: Tween | null;\n}\n\n/** Linear easing fallback. */\nfunction linearEasing(t: number): number {\n return t;\n}\n\n/** Create a tween. */\nexport function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween {\n return {\n from,\n to,\n duration,\n easing: easing ?? linearEasing,\n elapsed: 0,\n value: from,\n done: false,\n onComplete: null,\n next: null,\n };\n}\n\n/**\n * Update a tween by dt seconds. Returns the current value.\n * When complete, triggers onComplete callback and advances to chained tween.\n */\nexport function updateTween(tw: Tween, dt: number): number {\n if (tw.done) return tw.value;\n\n tw.elapsed += dt;\n\n if (tw.elapsed >= tw.duration) {\n tw.elapsed = tw.duration;\n tw.value = tw.to;\n tw.done = true;\n tw.onComplete?.();\n return tw.value;\n }\n\n const t = tw.elapsed / tw.duration;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/** Chain another tween to run after this one completes. Returns the chained tween. */\nexport function chainTween(tw: Tween, next: Tween): Tween {\n tw.next = next;\n return next;\n}\n\n/** Reset a tween to its initial state. */\nexport function resetTween(tw: Tween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n tw.done = false;\n}\n","import type { Tween } from './tween.js';\nimport { updateTween } from './tween.js';\n\n/**\n * Manages a collection of active tweens.\n * Automatically removes completed tweens (or advances to chained tweens).\n */\nexport interface TweenManager {\n /** Active tweens. */\n readonly tweens: Tween[];\n}\n\n/** Create a new TweenManager. */\nexport function createTweenManager(): TweenManager {\n return { tweens: [] };\n}\n\n/** Add a tween to the manager. */\nexport function addTween(mgr: TweenManager, tw: Tween): void {\n (mgr.tweens as Tween[]).push(tw);\n}\n\n/**\n * Update all managed tweens by dt seconds.\n * Completed tweens are removed; chained tweens replace completed ones.\n */\nexport function updateTweens(mgr: TweenManager, dt: number): void {\n const tweens = mgr.tweens as Tween[];\n let i = 0;\n while (i < tweens.length) {\n const tw: Tween = tweens[i]!;\n updateTween(tw, dt);\n if (tw.done) {\n if (tw.next) {\n // Replace with chained tween\n tweens[i] = tw.next;\n i++;\n } else {\n // Swap-remove\n tweens[i] = tweens[tweens.length - 1]!;\n tweens.pop();\n }\n } else {\n i++;\n }\n }\n}\n\n/** Remove all tweens. */\nexport function clearTweens(mgr: TweenManager): void {\n (mgr.tweens as Tween[]).length = 0;\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAgB,cACd,MACA,IACA,QACA,SAAmB,eACJ;CACf,IAAI,UAAU,GAAG,MAAM,IAAI,MAAM,mCAAmC;CACpE,OAAO;EAAE;EAAM;EAAI;EAAQ;EAAQ,SAAS;EAAG,OAAO;CAAK;AAC7D;;;;;AAMA,SAAgB,oBAAoB,IAAmB,IAAoB;CACzE,GAAG,WAAW,GAAG,UAAU,MAAM,GAAG;CACpC,MAAM,aAAa,GAAG,SAAS;CAE/B,MAAM,YAAY,GAAG,UAAU;CAC/B,MAAM,IAAI,aAAa,IAAI,YAAY,IAAI;CAC3C,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;AAKA,SAAgB,mBAAmB,IAAyB;CAC1D,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;AAChB;;AChCA,MAAM,iBAAiB;AACvB,MAAM,qBAAqB;;AAG3B,SAAgB,OAAO,OAAe,QAAgB,QAAwC;CAC5F,OAAO;EACL;EACA,UAAU;EACV;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QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;AAGA,SAAgB,WAAW,IAAW,MAAoB;CACxD,GAAG,OAAO;CACV,OAAO;AACT;;AAGA,SAAgB,WAAW,IAAiB;CAC1C,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;CACd,GAAG,OAAO;AACZ;;;AChEA,SAAgB,qBAAmC;CACjD,OAAO,EAAE,QAAQ,CAAC,EAAE;AACtB;;AAGA,SAAgB,SAAS,KAAmB,IAAiB;CAC3D,IAAK,OAAmB,KAAK,EAAE;AACjC;;;;;AAMA,SAAgB,aAAa,KAAmB,IAAkB;CAChE,MAAM,SAAS,IAAI;CACnB,IAAI,IAAI;CACR,OAAO,IAAI,OAAO,QAAQ;EACxB,MAAM,KAAY,OAAO;EACzB,YAAY,IAAI,EAAE;EAClB,IAAI,GAAG,MAAM;GACX,IAAI,GAAG,MAAM;IAEX,OAAO,KAAK,GAAG;IACf;GACF,OAAO;IAEL,OAAO,KAAK,OAAO,OAAO,SAAS;IACnC,OAAO,IAAI;GACb;EACF,OACE;CAEJ;AACF;;AAGA,SAAgB,YAAY,KAAyB;CACnD,IAAK,OAAmB,SAAS;AACnC"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/periodic.ts","../src/spring.ts","../src/sprite-animator.ts","../src/step-sequence.ts","../src/timed-modifier.ts","../src/tween.ts","../src/tween-manager.ts"],"sourcesContent":["import { type EasingFn, easeInOutSine } from '@flare-engine/math';\n\n/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */\nexport interface PeriodicTween {\n /** Start value. */\n readonly from: number;\n /** End value. */\n readonly to: number;\n /** Full cycle period in seconds (from → to → from). */\n readonly period: number;\n /** Easing function (default: easeInOutSine). */\n readonly easing: EasingFn;\n /** Elapsed time in seconds — mutable, advanced by updatePeriodicTween. */\n elapsed: number;\n /** Current interpolated value — written by updatePeriodicTween. */\n value: number;\n}\n\n/**\n * Create a periodic oscillating tween.\n *\n * @param from Start value.\n * @param to End value.\n * @param period Full cycle duration in seconds (must be > 0).\n * @param easing Easing function. Defaults to easeInOutSine.\n */\nexport function periodicTween(\n from: number,\n to: number,\n period: number,\n easing: EasingFn = easeInOutSine,\n): PeriodicTween {\n if (period <= 0) throw new Error('periodicTween: period must be > 0');\n return { from, to, period, easing, elapsed: 0, value: from };\n}\n\n/**\n * Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,\n * computes ping-pong t, applies easing, writes `value`, returns it.\n */\nexport function updatePeriodicTween(tw: PeriodicTween, dt: number): number {\n tw.elapsed = (tw.elapsed + dt) % tw.period;\n const halfPeriod = tw.period * 0.5;\n // t in [0,1] ping-pong: forward for first half, backward for second half.\n const halfPhase = tw.elapsed / halfPeriod;\n const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Reset a periodic tween: zero elapsed, restore value to `from`.\n */\nexport function resetPeriodicTween(tw: PeriodicTween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n}\n","/** Spring configuration. */\nexport interface SpringConfig {\n /** Spring stiffness (higher = faster oscillation). Default: 100. */\n stiffness: number;\n /** Damping coefficient (higher = less oscillation). Default: 10. */\n damping: number;\n /** Mass of the spring body. Default: 1. */\n mass: number;\n}\n\n/** Spring state. */\nexport interface Spring {\n /** Current value. */\n value: number;\n /** Current velocity. */\n velocity: number;\n /** Target value. */\n target: number;\n /** Configuration. */\n config: SpringConfig;\n /** Whether the spring has settled (within rest threshold). */\n atRest: boolean;\n}\n\nconst REST_THRESHOLD = 0.001;\nconst VELOCITY_THRESHOLD = 0.001;\n// |ζ − 1| inside this band is solved as critically damped: the under/overdamped forms divide by\n// a vanishing ωd / root gap there, and the critical solution differs by under 1e-6·ω0·dt relative.\nconst CRITICAL_BAND = 1e-6;\n\n/**\n * Create a spring with initial value.\n *\n * Throws a `RangeError` naming the field when `mass` is not above 0, `stiffness` or `damping`\n * is negative, or any of the three is not finite: none of those ever produced a usable spring.\n * `stiffness: 0` and `damping: 0` are valid. Only the config passed here is checked; later\n * edits to `s.config` are the caller's responsibility, so `updateSpring` stays check-free.\n */\nexport function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring {\n const stiffness = config?.stiffness ?? 100;\n const damping = config?.damping ?? 10;\n const mass = config?.mass ?? 1;\n if (!Number.isFinite(mass) || mass <= 0) {\n throw new RangeError(`spring: mass must be a finite number above 0, got ${mass}`);\n }\n if (!Number.isFinite(stiffness) || stiffness < 0) {\n throw new RangeError(`spring: stiffness must be a finite number >= 0, got ${stiffness}`);\n }\n if (!Number.isFinite(damping) || damping < 0) {\n throw new RangeError(`spring: damping must be a finite number >= 0, got ${damping}`);\n }\n return {\n value,\n velocity: 0,\n target,\n config: { stiffness, damping, mass },\n atRest: value === target,\n };\n}\n\n/**\n * Update a spring by dt seconds. Returns the current value.\n * Advances with the exact damped-oscillator solution, so any dt — a clamped 0.25 s hitch frame\n * or several frames at once — is stable, and n steps of dt equal one step of n·dt (up to float\n * rounding).\n */\nexport function updateSpring(s: Spring, dt: number): number {\n if (s.atRest) return s.value;\n\n const { stiffness, damping, mass } = s.config;\n const x0 = s.value - s.target;\n const v0 = s.velocity;\n const gamma = damping / mass;\n let x: number;\n let v: number;\n\n if (stiffness === 0) {\n if (gamma > 0) {\n const e = Math.exp(-gamma * dt);\n x = x0 + (v0 * (1 - e)) / gamma;\n v = v0 * e;\n } else {\n x = x0 + v0 * dt;\n v = v0;\n }\n } else {\n const w0 = Math.sqrt(stiffness / mass);\n const zeta = gamma / (2 * w0);\n if (Math.abs(zeta - 1) <= CRITICAL_BAND) {\n // Critical: x = (x0 + (v0 + ω0·x0)·t)·e^(−ω0·t)\n const e = Math.exp(-w0 * dt);\n const b = v0 + w0 * x0;\n x = (x0 + b * dt) * e;\n v = (v0 - w0 * b * dt) * e;\n } else if (zeta < 1) {\n // Underdamped: x = e^(−σt)·(x0·cos ωd·t + B·sin ωd·t), σ = ζω0, B = (v0 + σ·x0)/ωd\n const sigma = zeta * w0;\n const wd = w0 * Math.sqrt(1 - zeta * zeta);\n const e = Math.exp(-sigma * dt);\n const cos = Math.cos(wd * dt);\n const sin = Math.sin(wd * dt);\n const b = (v0 + sigma * x0) / wd;\n x = e * (x0 * cos + b * sin);\n v = e * (v0 * cos - (sigma * b + wd * x0) * sin);\n } else {\n // Overdamped: x = c1·e^(r1·t) + c2·e^(r2·t) with real roots r2 < r1 < 0;\n // r1 = −ω0/(ζ + √(ζ²−1)) equals −ω0·(ζ − √(ζ²−1)) without the cancellation.\n const root = Math.sqrt(zeta * zeta - 1);\n const r1 = -w0 / (zeta + root);\n const r2 = -w0 * (zeta + root);\n const c1 = (v0 - r2 * x0) / (r1 - r2);\n const c2 = x0 - c1;\n const e1 = Math.exp(r1 * dt);\n const e2 = Math.exp(r2 * dt);\n x = c1 * e1 + c2 * e2;\n v = c1 * r1 * e1 + c2 * r2 * e2;\n }\n }\n\n s.value = s.target + x;\n s.velocity = v;\n\n // Check if settled\n if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {\n s.value = s.target;\n s.velocity = 0;\n s.atRest = true;\n }\n\n return s.value;\n}\n\n/** Set a new target for the spring. Wakes it if at rest. */\nexport function setSpringTarget(s: Spring, target: number): void {\n s.target = target;\n s.atRest = false;\n}\n\n/** Reset both value and target, zeroing velocity. */\nexport function resetSpring(s: Spring, value: number): void {\n s.value = value;\n s.target = value;\n s.velocity = 0;\n s.atRest = true;\n}\n","import type { SpriteFrame, SpriteSheet } from '@flare-engine/assets';\n\n/** State of a sprite animator. */\nexport interface SpriteAnimatorState {\n /** The sprite sheet data source. */\n sheet: SpriteSheet;\n /** Current animation name, or null if none. */\n currentAnimation: string | null;\n /** Current frame index within the animation sequence. */\n frameIndex: number;\n /** Accumulated time within current frame (seconds). */\n elapsed: number;\n /** Whether animation is paused. */\n paused: boolean;\n /** Whether the current sequence has finished (non-looping). */\n finished: boolean;\n}\n\n/** Create a sprite animator state bound to a sprite sheet. */\nexport function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState {\n return {\n sheet,\n currentAnimation: null,\n frameIndex: 0,\n elapsed: 0,\n paused: false,\n finished: false,\n };\n}\n\n/** Play a named animation. Resets to frame 0. */\nexport function playAnimation(state: SpriteAnimatorState, name: string): void {\n const seq = state.sheet.animations.get(name);\n if (!seq) return;\n state.currentAnimation = name;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Stop the current animation. */\nexport function stopAnimation(state: SpriteAnimatorState): void {\n state.currentAnimation = null;\n state.frameIndex = 0;\n state.elapsed = 0;\n state.paused = false;\n state.finished = false;\n}\n\n/** Pause the current animation. */\nexport function pauseAnimation(state: SpriteAnimatorState): void {\n state.paused = true;\n}\n\n/** Resume the current animation. */\nexport function resumeAnimation(state: SpriteAnimatorState): void {\n state.paused = false;\n}\n\n/**\n * Tick the animator forward by dt seconds.\n * Returns the current SpriteFrame, or null if no animation is playing.\n */\nexport function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null {\n if (!state.currentAnimation || state.paused || state.finished) {\n return getCurrentFrame(state);\n }\n\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n if (seq.frameDuration <= 0) throw new Error('updateAnimator: frameDuration must be > 0');\n\n state.elapsed += dt;\n\n while (state.elapsed >= seq.frameDuration) {\n state.elapsed -= seq.frameDuration;\n state.frameIndex++;\n\n if (state.frameIndex >= seq.frameIndices.length) {\n if (seq.loop) {\n state.frameIndex = 0;\n } else {\n state.frameIndex = seq.frameIndices.length - 1;\n state.finished = true;\n state.elapsed = 0;\n break;\n }\n }\n }\n\n return getCurrentFrame(state);\n}\n\n/** Get the current SpriteFrame without advancing time. */\nexport function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null {\n if (!state.currentAnimation) return null;\n const seq = state.sheet.animations.get(state.currentAnimation);\n if (!seq || seq.frameIndices.length === 0) return null;\n const globalIdx = seq.frameIndices[state.frameIndex];\n if (globalIdx === undefined) return null;\n return state.sheet.frames[globalIdx] ?? null;\n}\n","export interface StepSequenceStep {\n readonly duration: number;\n}\n\nexport interface StepSequence {\n readonly steps: readonly StepSequenceStep[];\n current: number;\n elapsed: number;\n}\n\nexport function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence {\n if (steps.length === 0) throw new Error('createStepSequence: steps must not be empty');\n for (let i = 0; i < steps.length; i++) {\n if ((steps[i]?.duration ?? 0) <= 0)\n throw new Error('createStepSequence: each step duration must be > 0');\n }\n return { steps, current: 0, elapsed: 0 };\n}\n\nexport function tickStepSequence(seq: StepSequence, dt: number): number {\n seq.elapsed += dt;\n while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {\n seq.elapsed -= seq.steps[seq.current]!.duration;\n seq.current = (seq.current + 1) % seq.steps.length;\n }\n return seq.current;\n}\n\nexport function resetStepSequence(seq: StepSequence): void {\n seq.current = 0;\n seq.elapsed = 0;\n}\n","export interface TimedModifier<T> {\n readonly value: T;\n readonly combine: (base: T, modifier: T) => T;\n remaining: number;\n}\n\nexport function createTimedModifier<T>(\n value: T,\n duration: number,\n combine: (base: T, modifier: T) => T,\n): TimedModifier<T> {\n if (duration <= 0) throw new Error('createTimedModifier: duration must be > 0');\n return { value, combine, remaining: duration };\n}\n\nexport function tickModifier<T>(mod: TimedModifier<T>, dt: number): void {\n mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;\n}\n\nexport function isModifierActive<T>(mod: TimedModifier<T>): boolean {\n return mod.remaining > 0;\n}\n\n/**\n * Returns combine(base, mod.value) when active, base otherwise.\n * Zero-alloc only when T is number and combine is pure arithmetic.\n */\nexport function applyModifier<T>(base: T, mod: TimedModifier<T>): T {\n return mod.remaining > 0 ? mod.combine(base, mod.value) : base;\n}\n","import { type EasingFn, linear } from '@flare-engine/math';\n\n/** Tween state. */\nexport interface Tween {\n /** Start value. */\n from: number;\n /** End value. */\n to: number;\n /** Duration in seconds. */\n duration: number;\n /** Easing function. */\n easing: EasingFn;\n /** Elapsed time in seconds. */\n elapsed: number;\n /** Current interpolated value. */\n value: number;\n /** Whether the tween is complete. */\n done: boolean;\n /** Optional callback when tween completes. */\n onComplete: (() => void) | null;\n /** Chained tween to start after this one completes. */\n next: Tween | null;\n}\n\n/** Create a tween. */\nexport function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween {\n return {\n from,\n to,\n duration,\n easing: easing ?? linear,\n elapsed: 0,\n value: from,\n done: false,\n onComplete: null,\n next: null,\n };\n}\n\n/**\n * Update a tween by dt seconds. Returns the current value.\n * When complete, triggers onComplete callback.\n */\nexport function updateTween(tw: Tween, dt: number): number {\n if (tw.done) return tw.value;\n\n tw.elapsed += dt;\n\n if (tw.elapsed >= tw.duration) {\n tw.elapsed = tw.duration;\n tw.value = tw.to;\n tw.done = true;\n tw.onComplete?.();\n return tw.value;\n }\n\n const t = tw.elapsed / tw.duration;\n tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);\n return tw.value;\n}\n\n/**\n * Chain another tween to run after this one completes. Returns the chained tween.\n * Only `updateTweens` (TweenManager) follows the chain; `updateTween` does not.\n */\nexport function chainTween(tw: Tween, next: Tween): Tween {\n tw.next = next;\n return next;\n}\n\n/** Reset a tween to its initial state. */\nexport function resetTween(tw: Tween): void {\n tw.elapsed = 0;\n tw.value = tw.from;\n tw.done = false;\n}\n","import type { Tween } from './tween.js';\nimport { updateTween } from './tween.js';\n\n/**\n * Manages a collection of active tweens.\n * Automatically removes completed tweens (or advances to chained tweens).\n */\nexport interface TweenManager {\n /** Active tweens. */\n readonly tweens: Tween[];\n}\n\n/** Create a new TweenManager. */\nexport function createTweenManager(): TweenManager {\n return { tweens: [] };\n}\n\n/** Add a tween to the manager. */\nexport function addTween(mgr: TweenManager, tw: Tween): void {\n (mgr.tweens as Tween[]).push(tw);\n}\n\n/**\n * Update all managed tweens by dt seconds.\n * Completed tweens are removed; chained tweens replace completed ones.\n */\nexport function updateTweens(mgr: TweenManager, dt: number): void {\n const tweens = mgr.tweens as Tween[];\n let i = 0;\n while (i < tweens.length) {\n const tw: Tween = tweens[i]!;\n updateTween(tw, dt);\n if (tw.done) {\n if (tw.next) {\n // Replace with chained tween\n tweens[i] = tw.next;\n i++;\n } else {\n // Swap-remove\n tweens[i] = tweens[tweens.length - 1]!;\n tweens.pop();\n }\n } else {\n i++;\n }\n }\n}\n\n/** Remove all tweens. */\nexport function clearTweens(mgr: TweenManager): void {\n (mgr.tweens as Tween[]).length = 0;\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAgB,cACd,MACA,IACA,QACA,SAAmB,eACJ;CACf,IAAI,UAAU,GAAG,MAAM,IAAI,MAAM,mCAAmC;CACpE,OAAO;EAAE;EAAM;EAAI;EAAQ;EAAQ,SAAS;EAAG,OAAO;CAAK;AAC7D;;;;;AAMA,SAAgB,oBAAoB,IAAmB,IAAoB;CACzE,GAAG,WAAW,GAAG,UAAU,MAAM,GAAG;CACpC,MAAM,aAAa,GAAG,SAAS;CAE/B,MAAM,YAAY,GAAG,UAAU;CAC/B,MAAM,IAAI,aAAa,IAAI,YAAY,IAAI;CAC3C,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;AAKA,SAAgB,mBAAmB,IAAyB;CAC1D,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;AAChB;;AChCA,MAAM,iBAAiB;AACvB,MAAM,qBAAqB;AAG3B,MAAM,gBAAgB;;;;;;;;;AAUtB,SAAgB,OAAO,OAAe,QAAgB,QAAwC;CAC5F,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,OAAO,QAAQ,QAAQ;CAC7B,IAAI,CAAC,OAAO,SAAS,IAAI,KAAK,QAAQ,GACpC,MAAM,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AAG;EAChB,GAAG,QAAQ,GAAG;EACd,GAAG,OAAO;EACV,GAAG,aAAa;EAChB,OAAO,GAAG;CACZ;CAEA,MAAM,IAAI,GAAG,UAAU,GAAG;CAC1B,GAAG,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACpD,OAAO,GAAG;AACZ;;;;;AAMA,SAAgB,WAAW,IAAW,MAAoB;CACxD,GAAG,OAAO;CACV,OAAO;AACT;;AAGA,SAAgB,WAAW,IAAiB;CAC1C,GAAG,UAAU;CACb,GAAG,QAAQ,GAAG;CACd,GAAG,OAAO;AACZ;;;AC9DA,SAAgB,qBAAmC;CACjD,OAAO,EAAE,QAAQ,CAAC,EAAE;AACtB;;AAGA,SAAgB,SAAS,KAAmB,IAAiB;CAC3D,IAAK,OAAmB,KAAK,EAAE;AACjC;;;;;AAMA,SAAgB,aAAa,KAAmB,IAAkB;CAChE,MAAM,SAAS,IAAI;CACnB,IAAI,IAAI;CACR,OAAO,IAAI,OAAO,QAAQ;EACxB,MAAM,KAAY,OAAO;EACzB,YAAY,IAAI,EAAE;EAClB,IAAI,GAAG,MAAM;GACX,IAAI,GAAG,MAAM;IAEX,OAAO,KAAK,GAAG;IACf;GACF,OAAO;IAEL,OAAO,KAAK,OAAO,OAAO,SAAS;IACnC,OAAO,IAAI;GACb;EACF,OACE;CAEJ;AACF;;AAGA,SAAgB,YAAY,KAAyB;CACnD,IAAK,OAAmB,SAAS;AACnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/animation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "Sprite animation, tweens, springs, step sequences",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"flare-engine",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"typecheck": "tsc --noEmit"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@flare-engine/assets": "^0.4.
|
|
51
|
-
"@flare-engine/math": "^0.4.
|
|
50
|
+
"@flare-engine/assets": "^0.4.12",
|
|
51
|
+
"@flare-engine/math": "^0.4.12"
|
|
52
52
|
},
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=20"
|