@flare-engine/animation 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,321 +1,320 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- addTween: () => addTween,
24
- applyModifier: () => applyModifier,
25
- chainTween: () => chainTween,
26
- clearTweens: () => clearTweens,
27
- createSpriteAnimator: () => createSpriteAnimator,
28
- createStepSequence: () => createStepSequence,
29
- createTimedModifier: () => createTimedModifier,
30
- createTweenManager: () => createTweenManager,
31
- getCurrentFrame: () => getCurrentFrame,
32
- isModifierActive: () => isModifierActive,
33
- pauseAnimation: () => pauseAnimation,
34
- periodicTween: () => periodicTween,
35
- playAnimation: () => playAnimation,
36
- resetPeriodicTween: () => resetPeriodicTween,
37
- resetSpring: () => resetSpring,
38
- resetStepSequence: () => resetStepSequence,
39
- resetTween: () => resetTween,
40
- resumeAnimation: () => resumeAnimation,
41
- setSpringTarget: () => setSpringTarget,
42
- spring: () => spring,
43
- stopAnimation: () => stopAnimation,
44
- tickModifier: () => tickModifier,
45
- tickStepSequence: () => tickStepSequence,
46
- tween: () => tween,
47
- updateAnimator: () => updateAnimator,
48
- updatePeriodicTween: () => updatePeriodicTween,
49
- updateSpring: () => updateSpring,
50
- updateTween: () => updateTween,
51
- updateTweens: () => updateTweens
52
- });
53
- module.exports = __toCommonJS(index_exports);
54
-
55
- // src/sprite-animator.ts
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _flare_engine_math = require("@flare-engine/math");
3
+ //#region src/periodic.ts
4
+ /**
5
+ * Create a periodic oscillating tween.
6
+ *
7
+ * @param from Start value.
8
+ * @param to End value.
9
+ * @param period Full cycle duration in seconds (must be > 0).
10
+ * @param easing Easing function. Defaults to easeInOutSine.
11
+ */
12
+ function periodicTween(from, to, period, easing = _flare_engine_math.easeInOutSine) {
13
+ if (period <= 0) throw new Error("periodicTween: period must be > 0");
14
+ return {
15
+ from,
16
+ to,
17
+ period,
18
+ easing,
19
+ elapsed: 0,
20
+ value: from
21
+ };
22
+ }
23
+ /**
24
+ * Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,
25
+ * computes ping-pong t, applies easing, writes `value`, returns it.
26
+ */
27
+ function updatePeriodicTween(tw, dt) {
28
+ tw.elapsed = (tw.elapsed + dt) % tw.period;
29
+ const halfPeriod = tw.period * .5;
30
+ const halfPhase = tw.elapsed / halfPeriod;
31
+ const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;
32
+ tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
33
+ return tw.value;
34
+ }
35
+ /**
36
+ * Reset a periodic tween: zero elapsed, restore value to `from`.
37
+ */
38
+ function resetPeriodicTween(tw) {
39
+ tw.elapsed = 0;
40
+ tw.value = tw.from;
41
+ }
42
+ //#endregion
43
+ //#region src/spring.ts
44
+ const REST_THRESHOLD = .001;
45
+ const VELOCITY_THRESHOLD = .001;
46
+ /** Create a spring with initial value. */
47
+ function spring(value, target, config) {
48
+ return {
49
+ value,
50
+ velocity: 0,
51
+ target,
52
+ config: {
53
+ stiffness: config?.stiffness ?? 100,
54
+ damping: config?.damping ?? 10,
55
+ mass: config?.mass ?? 1
56
+ },
57
+ atRest: value === target
58
+ };
59
+ }
60
+ /**
61
+ * Update a spring by dt seconds. Returns the current value.
62
+ * Uses semi-implicit Euler integration for stability.
63
+ */
64
+ function updateSpring(s, dt) {
65
+ if (s.atRest) return s.value;
66
+ const { stiffness, damping, mass } = s.config;
67
+ const displacement = s.value - s.target;
68
+ const acceleration = (-stiffness * displacement + -damping * s.velocity) / mass;
69
+ s.velocity += acceleration * dt;
70
+ s.value += s.velocity * dt;
71
+ if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {
72
+ s.value = s.target;
73
+ s.velocity = 0;
74
+ s.atRest = true;
75
+ }
76
+ return s.value;
77
+ }
78
+ /** Set a new target for the spring. Wakes it if at rest. */
79
+ function setSpringTarget(s, target) {
80
+ s.target = target;
81
+ s.atRest = false;
82
+ }
83
+ /** Reset both value and target, zeroing velocity. */
84
+ function resetSpring(s, value) {
85
+ s.value = value;
86
+ s.target = value;
87
+ s.velocity = 0;
88
+ s.atRest = true;
89
+ }
90
+ //#endregion
91
+ //#region src/sprite-animator.ts
92
+ /** Create a sprite animator state bound to a sprite sheet. */
56
93
  function createSpriteAnimator(sheet) {
57
- return {
58
- sheet,
59
- currentAnimation: null,
60
- frameIndex: 0,
61
- elapsed: 0,
62
- paused: false,
63
- finished: false
64
- };
94
+ return {
95
+ sheet,
96
+ currentAnimation: null,
97
+ frameIndex: 0,
98
+ elapsed: 0,
99
+ paused: false,
100
+ finished: false
101
+ };
65
102
  }
103
+ /** Play a named animation. Resets to frame 0. */
66
104
  function playAnimation(state, name) {
67
- const seq = state.sheet.animations.get(name);
68
- if (!seq) return;
69
- state.currentAnimation = name;
70
- state.frameIndex = 0;
71
- state.elapsed = 0;
72
- state.paused = false;
73
- state.finished = false;
105
+ if (!state.sheet.animations.get(name)) return;
106
+ state.currentAnimation = name;
107
+ state.frameIndex = 0;
108
+ state.elapsed = 0;
109
+ state.paused = false;
110
+ state.finished = false;
74
111
  }
112
+ /** Stop the current animation. */
75
113
  function stopAnimation(state) {
76
- state.currentAnimation = null;
77
- state.frameIndex = 0;
78
- state.elapsed = 0;
79
- state.paused = false;
80
- state.finished = false;
114
+ state.currentAnimation = null;
115
+ state.frameIndex = 0;
116
+ state.elapsed = 0;
117
+ state.paused = false;
118
+ state.finished = false;
81
119
  }
120
+ /** Pause the current animation. */
82
121
  function pauseAnimation(state) {
83
- state.paused = true;
122
+ state.paused = true;
84
123
  }
124
+ /** Resume the current animation. */
85
125
  function resumeAnimation(state) {
86
- state.paused = false;
126
+ state.paused = false;
87
127
  }
128
+ /**
129
+ * Tick the animator forward by dt seconds.
130
+ * Returns the current SpriteFrame, or null if no animation is playing.
131
+ */
88
132
  function updateAnimator(state, dt) {
89
- if (!state.currentAnimation || state.paused || state.finished) {
90
- return getCurrentFrame(state);
91
- }
92
- const seq = state.sheet.animations.get(state.currentAnimation);
93
- if (!seq || seq.frameIndices.length === 0) return null;
94
- state.elapsed += dt;
95
- while (state.elapsed >= seq.frameDuration) {
96
- state.elapsed -= seq.frameDuration;
97
- state.frameIndex++;
98
- if (state.frameIndex >= seq.frameIndices.length) {
99
- if (seq.loop) {
100
- state.frameIndex = 0;
101
- } else {
102
- state.frameIndex = seq.frameIndices.length - 1;
103
- state.finished = true;
104
- state.elapsed = 0;
105
- break;
106
- }
107
- }
108
- }
109
- return getCurrentFrame(state);
133
+ if (!state.currentAnimation || state.paused || state.finished) return getCurrentFrame(state);
134
+ const seq = state.sheet.animations.get(state.currentAnimation);
135
+ if (!seq || seq.frameIndices.length === 0) return null;
136
+ if (seq.frameDuration <= 0) throw new Error("updateAnimator: frameDuration must be > 0");
137
+ state.elapsed += dt;
138
+ while (state.elapsed >= seq.frameDuration) {
139
+ state.elapsed -= seq.frameDuration;
140
+ state.frameIndex++;
141
+ if (state.frameIndex >= seq.frameIndices.length) if (seq.loop) state.frameIndex = 0;
142
+ else {
143
+ state.frameIndex = seq.frameIndices.length - 1;
144
+ state.finished = true;
145
+ state.elapsed = 0;
146
+ break;
147
+ }
148
+ }
149
+ return getCurrentFrame(state);
110
150
  }
151
+ /** Get the current SpriteFrame without advancing time. */
111
152
  function getCurrentFrame(state) {
112
- if (!state.currentAnimation) return null;
113
- const seq = state.sheet.animations.get(state.currentAnimation);
114
- if (!seq || seq.frameIndices.length === 0) return null;
115
- const globalIdx = seq.frameIndices[state.frameIndex];
116
- if (globalIdx === void 0) return null;
117
- return state.sheet.frames[globalIdx] ?? null;
153
+ if (!state.currentAnimation) return null;
154
+ const seq = state.sheet.animations.get(state.currentAnimation);
155
+ if (!seq || seq.frameIndices.length === 0) return null;
156
+ const globalIdx = seq.frameIndices[state.frameIndex];
157
+ if (globalIdx === void 0) return null;
158
+ return state.sheet.frames[globalIdx] ?? null;
118
159
  }
119
-
120
- // src/tween.ts
160
+ //#endregion
161
+ //#region src/step-sequence.ts
162
+ function createStepSequence(steps) {
163
+ if (steps.length === 0) throw new Error("createStepSequence: steps must not be empty");
164
+ for (let i = 0; i < steps.length; i++) if ((steps[i]?.duration ?? 0) <= 0) throw new Error("createStepSequence: each step duration must be > 0");
165
+ return {
166
+ steps,
167
+ current: 0,
168
+ elapsed: 0
169
+ };
170
+ }
171
+ function tickStepSequence(seq, dt) {
172
+ seq.elapsed += dt;
173
+ while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {
174
+ seq.elapsed -= seq.steps[seq.current].duration;
175
+ seq.current = (seq.current + 1) % seq.steps.length;
176
+ }
177
+ return seq.current;
178
+ }
179
+ function resetStepSequence(seq) {
180
+ seq.current = 0;
181
+ seq.elapsed = 0;
182
+ }
183
+ //#endregion
184
+ //#region src/timed-modifier.ts
185
+ function createTimedModifier(value, duration, combine) {
186
+ if (duration <= 0) throw new Error("createTimedModifier: duration must be > 0");
187
+ return {
188
+ value,
189
+ combine,
190
+ remaining: duration
191
+ };
192
+ }
193
+ function tickModifier(mod, dt) {
194
+ mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;
195
+ }
196
+ function isModifierActive(mod) {
197
+ return mod.remaining > 0;
198
+ }
199
+ /**
200
+ * Returns combine(base, mod.value) when active, base otherwise.
201
+ * Zero-alloc only when T is number and combine is pure arithmetic.
202
+ */
203
+ function applyModifier(base, mod) {
204
+ return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
205
+ }
206
+ //#endregion
207
+ //#region src/tween.ts
208
+ /** Linear easing fallback. */
121
209
  function linearEasing(t) {
122
- return t;
210
+ return t;
123
211
  }
212
+ /** Create a tween. */
124
213
  function tween(from, to, duration, easing) {
125
- return {
126
- from,
127
- to,
128
- duration,
129
- easing: easing ?? linearEasing,
130
- elapsed: 0,
131
- value: from,
132
- done: false,
133
- onComplete: null,
134
- next: null
135
- };
214
+ return {
215
+ from,
216
+ to,
217
+ duration,
218
+ easing: easing ?? linearEasing,
219
+ elapsed: 0,
220
+ value: from,
221
+ done: false,
222
+ onComplete: null,
223
+ next: null
224
+ };
136
225
  }
226
+ /**
227
+ * Update a tween by dt seconds. Returns the current value.
228
+ * When complete, triggers onComplete callback and advances to chained tween.
229
+ */
137
230
  function updateTween(tw, dt) {
138
- if (tw.done) return tw.value;
139
- tw.elapsed += dt;
140
- if (tw.elapsed >= tw.duration) {
141
- tw.elapsed = tw.duration;
142
- tw.value = tw.to;
143
- tw.done = true;
144
- tw.onComplete?.();
145
- return tw.value;
146
- }
147
- const t = tw.elapsed / tw.duration;
148
- tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
149
- return tw.value;
231
+ if (tw.done) return tw.value;
232
+ tw.elapsed += dt;
233
+ if (tw.elapsed >= tw.duration) {
234
+ tw.elapsed = tw.duration;
235
+ tw.value = tw.to;
236
+ tw.done = true;
237
+ tw.onComplete?.();
238
+ return tw.value;
239
+ }
240
+ const t = tw.elapsed / tw.duration;
241
+ tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
242
+ return tw.value;
150
243
  }
244
+ /** Chain another tween to run after this one completes. Returns the chained tween. */
151
245
  function chainTween(tw, next) {
152
- tw.next = next;
153
- return next;
246
+ tw.next = next;
247
+ return next;
154
248
  }
249
+ /** Reset a tween to its initial state. */
155
250
  function resetTween(tw) {
156
- tw.elapsed = 0;
157
- tw.value = tw.from;
158
- tw.done = false;
251
+ tw.elapsed = 0;
252
+ tw.value = tw.from;
253
+ tw.done = false;
159
254
  }
160
-
161
- // src/spring.ts
162
- var REST_THRESHOLD = 1e-3;
163
- var VELOCITY_THRESHOLD = 1e-3;
164
- function spring(value, target, config) {
165
- return {
166
- value,
167
- velocity: 0,
168
- target,
169
- config: {
170
- stiffness: config?.stiffness ?? 100,
171
- damping: config?.damping ?? 10,
172
- mass: config?.mass ?? 1
173
- },
174
- atRest: value === target
175
- };
176
- }
177
- function updateSpring(s, dt) {
178
- if (s.atRest) return s.value;
179
- const { stiffness, damping, mass } = s.config;
180
- const displacement = s.value - s.target;
181
- const springForce = -stiffness * displacement;
182
- const dampingForce = -damping * s.velocity;
183
- const acceleration = (springForce + dampingForce) / mass;
184
- s.velocity += acceleration * dt;
185
- s.value += s.velocity * dt;
186
- if (Math.abs(s.value - s.target) < REST_THRESHOLD && Math.abs(s.velocity) < VELOCITY_THRESHOLD) {
187
- s.value = s.target;
188
- s.velocity = 0;
189
- s.atRest = true;
190
- }
191
- return s.value;
192
- }
193
- function setSpringTarget(s, target) {
194
- s.target = target;
195
- s.atRest = false;
196
- }
197
- function resetSpring(s, value) {
198
- s.value = value;
199
- s.target = value;
200
- s.velocity = 0;
201
- s.atRest = true;
202
- }
203
-
204
- // src/tween-manager.ts
255
+ //#endregion
256
+ //#region src/tween-manager.ts
257
+ /** Create a new TweenManager. */
205
258
  function createTweenManager() {
206
- return { tweens: [] };
259
+ return { tweens: [] };
207
260
  }
261
+ /** Add a tween to the manager. */
208
262
  function addTween(mgr, tw) {
209
- mgr.tweens.push(tw);
263
+ mgr.tweens.push(tw);
210
264
  }
265
+ /**
266
+ * Update all managed tweens by dt seconds.
267
+ * Completed tweens are removed; chained tweens replace completed ones.
268
+ */
211
269
  function updateTweens(mgr, dt) {
212
- const tweens = mgr.tweens;
213
- let i = 0;
214
- while (i < tweens.length) {
215
- const tw = tweens[i];
216
- updateTween(tw, dt);
217
- if (tw.done) {
218
- if (tw.next) {
219
- tweens[i] = tw.next;
220
- i++;
221
- } else {
222
- tweens[i] = tweens[tweens.length - 1];
223
- tweens.pop();
224
- }
225
- } else {
226
- i++;
227
- }
228
- }
270
+ const tweens = mgr.tweens;
271
+ let i = 0;
272
+ while (i < tweens.length) {
273
+ const tw = tweens[i];
274
+ updateTween(tw, dt);
275
+ if (tw.done) if (tw.next) {
276
+ tweens[i] = tw.next;
277
+ i++;
278
+ } else {
279
+ tweens[i] = tweens[tweens.length - 1];
280
+ tweens.pop();
281
+ }
282
+ else i++;
283
+ }
229
284
  }
285
+ /** Remove all tweens. */
230
286
  function clearTweens(mgr) {
231
- mgr.tweens.length = 0;
232
- }
233
-
234
- // src/periodic.ts
235
- var import_math = require("@flare-engine/math");
236
- function periodicTween(from, to, period, easing = import_math.easeInOutSine) {
237
- if (period <= 0) throw new Error("periodicTween: period must be > 0");
238
- return { from, to, period, easing, elapsed: 0, value: from };
239
- }
240
- function updatePeriodicTween(tw, dt) {
241
- tw.elapsed = (tw.elapsed + dt) % tw.period;
242
- const halfPeriod = tw.period * 0.5;
243
- const halfPhase = tw.elapsed / halfPeriod;
244
- const t = halfPhase <= 1 ? halfPhase : 2 - halfPhase;
245
- tw.value = tw.from + (tw.to - tw.from) * tw.easing(t);
246
- return tw.value;
247
- }
248
- function resetPeriodicTween(tw) {
249
- tw.elapsed = 0;
250
- tw.value = tw.from;
287
+ mgr.tweens.length = 0;
251
288
  }
289
+ //#endregion
290
+ exports.addTween = addTween;
291
+ exports.applyModifier = applyModifier;
292
+ exports.chainTween = chainTween;
293
+ exports.clearTweens = clearTweens;
294
+ exports.createSpriteAnimator = createSpriteAnimator;
295
+ exports.createStepSequence = createStepSequence;
296
+ exports.createTimedModifier = createTimedModifier;
297
+ exports.createTweenManager = createTweenManager;
298
+ exports.getCurrentFrame = getCurrentFrame;
299
+ exports.isModifierActive = isModifierActive;
300
+ exports.pauseAnimation = pauseAnimation;
301
+ exports.periodicTween = periodicTween;
302
+ exports.playAnimation = playAnimation;
303
+ exports.resetPeriodicTween = resetPeriodicTween;
304
+ exports.resetSpring = resetSpring;
305
+ exports.resetStepSequence = resetStepSequence;
306
+ exports.resetTween = resetTween;
307
+ exports.resumeAnimation = resumeAnimation;
308
+ exports.setSpringTarget = setSpringTarget;
309
+ exports.spring = spring;
310
+ exports.stopAnimation = stopAnimation;
311
+ exports.tickModifier = tickModifier;
312
+ exports.tickStepSequence = tickStepSequence;
313
+ exports.tween = tween;
314
+ exports.updateAnimator = updateAnimator;
315
+ exports.updatePeriodicTween = updatePeriodicTween;
316
+ exports.updateSpring = updateSpring;
317
+ exports.updateTween = updateTween;
318
+ exports.updateTweens = updateTweens;
252
319
 
253
- // src/step-sequence.ts
254
- function createStepSequence(steps) {
255
- if (steps.length === 0) throw new Error("createStepSequence: steps must not be empty");
256
- for (let i = 0; i < steps.length; i++) {
257
- if ((steps[i]?.duration ?? 0) <= 0)
258
- throw new Error("createStepSequence: each step duration must be > 0");
259
- }
260
- return { steps, current: 0, elapsed: 0 };
261
- }
262
- function tickStepSequence(seq, dt) {
263
- seq.elapsed += dt;
264
- while (seq.elapsed >= (seq.steps[seq.current]?.duration ?? 0)) {
265
- seq.elapsed -= seq.steps[seq.current].duration;
266
- seq.current = (seq.current + 1) % seq.steps.length;
267
- }
268
- return seq.current;
269
- }
270
- function resetStepSequence(seq) {
271
- seq.current = 0;
272
- seq.elapsed = 0;
273
- }
274
-
275
- // src/timed-modifier.ts
276
- function createTimedModifier(value, duration, combine) {
277
- if (duration <= 0) throw new Error("createTimedModifier: duration must be > 0");
278
- return { value, combine, remaining: duration };
279
- }
280
- function tickModifier(mod, dt) {
281
- mod.remaining = mod.remaining - dt < 0 ? 0 : mod.remaining - dt;
282
- }
283
- function isModifierActive(mod) {
284
- return mod.remaining > 0;
285
- }
286
- function applyModifier(base, mod) {
287
- return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
288
- }
289
- // Annotate the CommonJS export names for ESM import in node:
290
- 0 && (module.exports = {
291
- addTween,
292
- applyModifier,
293
- chainTween,
294
- clearTweens,
295
- createSpriteAnimator,
296
- createStepSequence,
297
- createTimedModifier,
298
- createTweenManager,
299
- getCurrentFrame,
300
- isModifierActive,
301
- pauseAnimation,
302
- periodicTween,
303
- playAnimation,
304
- resetPeriodicTween,
305
- resetSpring,
306
- resetStepSequence,
307
- resetTween,
308
- resumeAnimation,
309
- setSpringTarget,
310
- spring,
311
- stopAnimation,
312
- tickModifier,
313
- tickStepSequence,
314
- tween,
315
- updateAnimator,
316
- updatePeriodicTween,
317
- updateSpring,
318
- updateTween,
319
- updateTweens
320
- });
321
320
  //# sourceMappingURL=index.js.map