@flare-engine/animation 0.4.5 → 0.4.7
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 +30 -44
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +30 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { EasingFn } from "@flare-engine/math";
|
|
2
2
|
import { SpriteFrame, SpriteSheet } from "@flare-engine/assets";
|
|
3
|
-
//#region src/periodic.d.ts
|
|
4
3
|
/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */
|
|
5
4
|
interface PeriodicTween {
|
|
6
5
|
/** Start value. */
|
|
@@ -24,18 +23,16 @@ interface PeriodicTween {
|
|
|
24
23
|
* @param period Full cycle duration in seconds (must be > 0).
|
|
25
24
|
* @param easing Easing function. Defaults to easeInOutSine.
|
|
26
25
|
*/
|
|
27
|
-
declare function periodicTween(from: number, to: number, period: number, easing?: EasingFn): PeriodicTween;
|
|
26
|
+
export declare function periodicTween(from: number, to: number, period: number, easing?: EasingFn): PeriodicTween;
|
|
28
27
|
/**
|
|
29
28
|
* Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,
|
|
30
29
|
* computes ping-pong t, applies easing, writes `value`, returns it.
|
|
31
30
|
*/
|
|
32
|
-
declare function updatePeriodicTween(tw: PeriodicTween, dt: number): number;
|
|
31
|
+
export declare function updatePeriodicTween(tw: PeriodicTween, dt: number): number;
|
|
33
32
|
/**
|
|
34
33
|
* Reset a periodic tween: zero elapsed, restore value to `from`.
|
|
35
34
|
*/
|
|
36
|
-
declare function resetPeriodicTween(tw: PeriodicTween): void;
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/spring.d.ts
|
|
35
|
+
export declare function resetPeriodicTween(tw: PeriodicTween): void;
|
|
39
36
|
/** Spring configuration. */
|
|
40
37
|
interface SpringConfig {
|
|
41
38
|
/** Spring stiffness (higher = faster oscillation). Default: 100. */
|
|
@@ -59,18 +56,16 @@ interface Spring {
|
|
|
59
56
|
atRest: boolean;
|
|
60
57
|
}
|
|
61
58
|
/** Create a spring with initial value. */
|
|
62
|
-
declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
59
|
+
export declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
63
60
|
/**
|
|
64
61
|
* Update a spring by dt seconds. Returns the current value.
|
|
65
62
|
* Uses semi-implicit Euler integration for stability.
|
|
66
63
|
*/
|
|
67
|
-
declare function updateSpring(s: Spring, dt: number): number;
|
|
64
|
+
export declare function updateSpring(s: Spring, dt: number): number;
|
|
68
65
|
/** Set a new target for the spring. Wakes it if at rest. */
|
|
69
|
-
declare function setSpringTarget(s: Spring, target: number): void;
|
|
66
|
+
export declare function setSpringTarget(s: Spring, target: number): void;
|
|
70
67
|
/** Reset both value and target, zeroing velocity. */
|
|
71
|
-
declare function resetSpring(s: Spring, value: number): void;
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/sprite-animator.d.ts
|
|
68
|
+
export declare function resetSpring(s: Spring, value: number): void;
|
|
74
69
|
/** State of a sprite animator. */
|
|
75
70
|
interface SpriteAnimatorState {
|
|
76
71
|
/** The sprite sheet data source. */
|
|
@@ -87,24 +82,22 @@ interface SpriteAnimatorState {
|
|
|
87
82
|
finished: boolean;
|
|
88
83
|
}
|
|
89
84
|
/** Create a sprite animator state bound to a sprite sheet. */
|
|
90
|
-
declare function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState;
|
|
85
|
+
export declare function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState;
|
|
91
86
|
/** Play a named animation. Resets to frame 0. */
|
|
92
|
-
declare function playAnimation(state: SpriteAnimatorState, name: string): void;
|
|
87
|
+
export declare function playAnimation(state: SpriteAnimatorState, name: string): void;
|
|
93
88
|
/** Stop the current animation. */
|
|
94
|
-
declare function stopAnimation(state: SpriteAnimatorState): void;
|
|
89
|
+
export declare function stopAnimation(state: SpriteAnimatorState): void;
|
|
95
90
|
/** Pause the current animation. */
|
|
96
|
-
declare function pauseAnimation(state: SpriteAnimatorState): void;
|
|
91
|
+
export declare function pauseAnimation(state: SpriteAnimatorState): void;
|
|
97
92
|
/** Resume the current animation. */
|
|
98
|
-
declare function resumeAnimation(state: SpriteAnimatorState): void;
|
|
93
|
+
export declare function resumeAnimation(state: SpriteAnimatorState): void;
|
|
99
94
|
/**
|
|
100
95
|
* Tick the animator forward by dt seconds.
|
|
101
96
|
* Returns the current SpriteFrame, or null if no animation is playing.
|
|
102
97
|
*/
|
|
103
|
-
declare function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null;
|
|
98
|
+
export declare function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null;
|
|
104
99
|
/** Get the current SpriteFrame without advancing time. */
|
|
105
|
-
declare function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null;
|
|
106
|
-
//#endregion
|
|
107
|
-
//#region src/step-sequence.d.ts
|
|
100
|
+
export declare function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null;
|
|
108
101
|
interface StepSequenceStep {
|
|
109
102
|
readonly duration: number;
|
|
110
103
|
}
|
|
@@ -113,26 +106,22 @@ interface StepSequence {
|
|
|
113
106
|
current: number;
|
|
114
107
|
elapsed: number;
|
|
115
108
|
}
|
|
116
|
-
declare function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence;
|
|
117
|
-
declare function tickStepSequence(seq: StepSequence, dt: number): number;
|
|
118
|
-
declare function resetStepSequence(seq: StepSequence): void;
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/timed-modifier.d.ts
|
|
109
|
+
export declare function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence;
|
|
110
|
+
export declare function tickStepSequence(seq: StepSequence, dt: number): number;
|
|
111
|
+
export declare function resetStepSequence(seq: StepSequence): void;
|
|
121
112
|
interface TimedModifier<T> {
|
|
122
113
|
readonly value: T;
|
|
123
114
|
readonly combine: (base: T, modifier: T) => T;
|
|
124
115
|
remaining: number;
|
|
125
116
|
}
|
|
126
|
-
declare function createTimedModifier<T>(value: T, duration: number, combine: (base: T, modifier: T) => T): TimedModifier<T>;
|
|
127
|
-
declare function tickModifier<T>(mod: TimedModifier<T>, dt: number): void;
|
|
128
|
-
declare function isModifierActive<T>(mod: TimedModifier<T>): boolean;
|
|
117
|
+
export declare function createTimedModifier<T>(value: T, duration: number, combine: (base: T, modifier: T) => T): TimedModifier<T>;
|
|
118
|
+
export declare function tickModifier<T>(mod: TimedModifier<T>, dt: number): void;
|
|
119
|
+
export declare function isModifierActive<T>(mod: TimedModifier<T>): boolean;
|
|
129
120
|
/**
|
|
130
121
|
* Returns combine(base, mod.value) when active, base otherwise.
|
|
131
122
|
* Zero-alloc only when T is number and combine is pure arithmetic.
|
|
132
123
|
*/
|
|
133
|
-
declare function applyModifier<T>(base: T, mod: TimedModifier<T>): T;
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/tween.d.ts
|
|
124
|
+
export declare function applyModifier<T>(base: T, mod: TimedModifier<T>): T;
|
|
136
125
|
/** Tween state. */
|
|
137
126
|
interface Tween {
|
|
138
127
|
/** Start value. */
|
|
@@ -155,18 +144,16 @@ interface Tween {
|
|
|
155
144
|
next: Tween | null;
|
|
156
145
|
}
|
|
157
146
|
/** Create a tween. */
|
|
158
|
-
declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
147
|
+
export declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
159
148
|
/**
|
|
160
149
|
* Update a tween by dt seconds. Returns the current value.
|
|
161
150
|
* When complete, triggers onComplete callback and advances to chained tween.
|
|
162
151
|
*/
|
|
163
|
-
declare function updateTween(tw: Tween, dt: number): number;
|
|
152
|
+
export declare function updateTween(tw: Tween, dt: number): number;
|
|
164
153
|
/** Chain another tween to run after this one completes. Returns the chained tween. */
|
|
165
|
-
declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
154
|
+
export declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
166
155
|
/** Reset a tween to its initial state. */
|
|
167
|
-
declare function resetTween(tw: Tween): void;
|
|
168
|
-
//#endregion
|
|
169
|
-
//#region src/tween-manager.d.ts
|
|
156
|
+
export declare function resetTween(tw: Tween): void;
|
|
170
157
|
/**
|
|
171
158
|
* Manages a collection of active tweens.
|
|
172
159
|
* Automatically removes completed tweens (or advances to chained tweens).
|
|
@@ -176,16 +163,15 @@ interface TweenManager {
|
|
|
176
163
|
readonly tweens: Tween[];
|
|
177
164
|
}
|
|
178
165
|
/** Create a new TweenManager. */
|
|
179
|
-
declare function createTweenManager(): TweenManager;
|
|
166
|
+
export declare function createTweenManager(): TweenManager;
|
|
180
167
|
/** Add a tween to the manager. */
|
|
181
|
-
declare function addTween(mgr: TweenManager, tw: Tween): void;
|
|
168
|
+
export declare function addTween(mgr: TweenManager, tw: Tween): void;
|
|
182
169
|
/**
|
|
183
170
|
* Update all managed tweens by dt seconds.
|
|
184
171
|
* Completed tweens are removed; chained tweens replace completed ones.
|
|
185
172
|
*/
|
|
186
|
-
declare function updateTweens(mgr: TweenManager, dt: number): void;
|
|
173
|
+
export declare function updateTweens(mgr: TweenManager, dt: number): void;
|
|
187
174
|
/** Remove all tweens. */
|
|
188
|
-
declare function clearTweens(mgr: TweenManager): void;
|
|
189
|
-
|
|
190
|
-
export { type PeriodicTween, type Spring, type SpringConfig, type SpriteAnimatorState, type StepSequence, type StepSequenceStep, type TimedModifier, type Tween, type TweenManager, addTween, applyModifier, chainTween, clearTweens, createSpriteAnimator, createStepSequence, createTimedModifier, createTweenManager, getCurrentFrame, isModifierActive, pauseAnimation, periodicTween, playAnimation, resetPeriodicTween, resetSpring, resetStepSequence, resetTween, resumeAnimation, setSpringTarget, spring, stopAnimation, tickModifier, tickStepSequence, tween, updateAnimator, updatePeriodicTween, updateSpring, updateTween, updateTweens };
|
|
175
|
+
export declare function clearTweens(mgr: TweenManager): void;
|
|
176
|
+
export type { PeriodicTween, Spring, SpringConfig, SpriteAnimatorState, StepSequence, StepSequenceStep, TimedModifier, Tween, TweenManager };
|
|
191
177
|
//# sourceMappingURL=index.d.mts.map
|
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":"
|
|
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;;;wBAOc,OAAO,eAAe,gBAAgB,SAAS,QAAQ,gBAAgB;;;;;wBAkBvE,aAAa,GAAG,QAAQ;;wBAwBxB,gBAAgB,GAAG,QAAQ;;wBAM3B,YAAY,GAAG,QAAQ;;UCzEtB;;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;;;wBASQ,MAAM,cAAc,YAAY,kBAAkB,SAAS,WAAW;;;;;wBAkBtE,YAAY,IAAI,OAAO;;wBAmBvB,WAAW,IAAI,OAAO,MAAM,QAAQ;;wBAMpC,WAAW,IAAI;;;;;UClEd;;WAEN,QAAQ;;;wBAIH,sBAAsB;;wBAKtB,SAAS,KAAK,cAAc,IAAI;;;;;wBAQhC,aAAa,KAAK,cAAc;;wBAuBhC,YAAY,KAAK"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { EasingFn } from "@flare-engine/math";
|
|
2
2
|
import { SpriteFrame, SpriteSheet } from "@flare-engine/assets";
|
|
3
|
-
//#region src/periodic.d.ts
|
|
4
3
|
/** Oscillating tween that ping-pongs between `from` and `to` indefinitely. */
|
|
5
4
|
interface PeriodicTween {
|
|
6
5
|
/** Start value. */
|
|
@@ -24,18 +23,16 @@ interface PeriodicTween {
|
|
|
24
23
|
* @param period Full cycle duration in seconds (must be > 0).
|
|
25
24
|
* @param easing Easing function. Defaults to easeInOutSine.
|
|
26
25
|
*/
|
|
27
|
-
declare function periodicTween(from: number, to: number, period: number, easing?: EasingFn): PeriodicTween;
|
|
26
|
+
export declare function periodicTween(from: number, to: number, period: number, easing?: EasingFn): PeriodicTween;
|
|
28
27
|
/**
|
|
29
28
|
* Advance a periodic tween by `dt` seconds. Wraps elapsed modulo period,
|
|
30
29
|
* computes ping-pong t, applies easing, writes `value`, returns it.
|
|
31
30
|
*/
|
|
32
|
-
declare function updatePeriodicTween(tw: PeriodicTween, dt: number): number;
|
|
31
|
+
export declare function updatePeriodicTween(tw: PeriodicTween, dt: number): number;
|
|
33
32
|
/**
|
|
34
33
|
* Reset a periodic tween: zero elapsed, restore value to `from`.
|
|
35
34
|
*/
|
|
36
|
-
declare function resetPeriodicTween(tw: PeriodicTween): void;
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/spring.d.ts
|
|
35
|
+
export declare function resetPeriodicTween(tw: PeriodicTween): void;
|
|
39
36
|
/** Spring configuration. */
|
|
40
37
|
interface SpringConfig {
|
|
41
38
|
/** Spring stiffness (higher = faster oscillation). Default: 100. */
|
|
@@ -59,18 +56,16 @@ interface Spring {
|
|
|
59
56
|
atRest: boolean;
|
|
60
57
|
}
|
|
61
58
|
/** Create a spring with initial value. */
|
|
62
|
-
declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
59
|
+
export declare function spring(value: number, target: number, config?: Partial<SpringConfig>): Spring;
|
|
63
60
|
/**
|
|
64
61
|
* Update a spring by dt seconds. Returns the current value.
|
|
65
62
|
* Uses semi-implicit Euler integration for stability.
|
|
66
63
|
*/
|
|
67
|
-
declare function updateSpring(s: Spring, dt: number): number;
|
|
64
|
+
export declare function updateSpring(s: Spring, dt: number): number;
|
|
68
65
|
/** Set a new target for the spring. Wakes it if at rest. */
|
|
69
|
-
declare function setSpringTarget(s: Spring, target: number): void;
|
|
66
|
+
export declare function setSpringTarget(s: Spring, target: number): void;
|
|
70
67
|
/** Reset both value and target, zeroing velocity. */
|
|
71
|
-
declare function resetSpring(s: Spring, value: number): void;
|
|
72
|
-
//#endregion
|
|
73
|
-
//#region src/sprite-animator.d.ts
|
|
68
|
+
export declare function resetSpring(s: Spring, value: number): void;
|
|
74
69
|
/** State of a sprite animator. */
|
|
75
70
|
interface SpriteAnimatorState {
|
|
76
71
|
/** The sprite sheet data source. */
|
|
@@ -87,24 +82,22 @@ interface SpriteAnimatorState {
|
|
|
87
82
|
finished: boolean;
|
|
88
83
|
}
|
|
89
84
|
/** Create a sprite animator state bound to a sprite sheet. */
|
|
90
|
-
declare function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState;
|
|
85
|
+
export declare function createSpriteAnimator(sheet: SpriteSheet): SpriteAnimatorState;
|
|
91
86
|
/** Play a named animation. Resets to frame 0. */
|
|
92
|
-
declare function playAnimation(state: SpriteAnimatorState, name: string): void;
|
|
87
|
+
export declare function playAnimation(state: SpriteAnimatorState, name: string): void;
|
|
93
88
|
/** Stop the current animation. */
|
|
94
|
-
declare function stopAnimation(state: SpriteAnimatorState): void;
|
|
89
|
+
export declare function stopAnimation(state: SpriteAnimatorState): void;
|
|
95
90
|
/** Pause the current animation. */
|
|
96
|
-
declare function pauseAnimation(state: SpriteAnimatorState): void;
|
|
91
|
+
export declare function pauseAnimation(state: SpriteAnimatorState): void;
|
|
97
92
|
/** Resume the current animation. */
|
|
98
|
-
declare function resumeAnimation(state: SpriteAnimatorState): void;
|
|
93
|
+
export declare function resumeAnimation(state: SpriteAnimatorState): void;
|
|
99
94
|
/**
|
|
100
95
|
* Tick the animator forward by dt seconds.
|
|
101
96
|
* Returns the current SpriteFrame, or null if no animation is playing.
|
|
102
97
|
*/
|
|
103
|
-
declare function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null;
|
|
98
|
+
export declare function updateAnimator(state: SpriteAnimatorState, dt: number): SpriteFrame | null;
|
|
104
99
|
/** Get the current SpriteFrame without advancing time. */
|
|
105
|
-
declare function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null;
|
|
106
|
-
//#endregion
|
|
107
|
-
//#region src/step-sequence.d.ts
|
|
100
|
+
export declare function getCurrentFrame(state: SpriteAnimatorState): SpriteFrame | null;
|
|
108
101
|
interface StepSequenceStep {
|
|
109
102
|
readonly duration: number;
|
|
110
103
|
}
|
|
@@ -113,26 +106,22 @@ interface StepSequence {
|
|
|
113
106
|
current: number;
|
|
114
107
|
elapsed: number;
|
|
115
108
|
}
|
|
116
|
-
declare function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence;
|
|
117
|
-
declare function tickStepSequence(seq: StepSequence, dt: number): number;
|
|
118
|
-
declare function resetStepSequence(seq: StepSequence): void;
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/timed-modifier.d.ts
|
|
109
|
+
export declare function createStepSequence(steps: readonly StepSequenceStep[]): StepSequence;
|
|
110
|
+
export declare function tickStepSequence(seq: StepSequence, dt: number): number;
|
|
111
|
+
export declare function resetStepSequence(seq: StepSequence): void;
|
|
121
112
|
interface TimedModifier<T> {
|
|
122
113
|
readonly value: T;
|
|
123
114
|
readonly combine: (base: T, modifier: T) => T;
|
|
124
115
|
remaining: number;
|
|
125
116
|
}
|
|
126
|
-
declare function createTimedModifier<T>(value: T, duration: number, combine: (base: T, modifier: T) => T): TimedModifier<T>;
|
|
127
|
-
declare function tickModifier<T>(mod: TimedModifier<T>, dt: number): void;
|
|
128
|
-
declare function isModifierActive<T>(mod: TimedModifier<T>): boolean;
|
|
117
|
+
export declare function createTimedModifier<T>(value: T, duration: number, combine: (base: T, modifier: T) => T): TimedModifier<T>;
|
|
118
|
+
export declare function tickModifier<T>(mod: TimedModifier<T>, dt: number): void;
|
|
119
|
+
export declare function isModifierActive<T>(mod: TimedModifier<T>): boolean;
|
|
129
120
|
/**
|
|
130
121
|
* Returns combine(base, mod.value) when active, base otherwise.
|
|
131
122
|
* Zero-alloc only when T is number and combine is pure arithmetic.
|
|
132
123
|
*/
|
|
133
|
-
declare function applyModifier<T>(base: T, mod: TimedModifier<T>): T;
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/tween.d.ts
|
|
124
|
+
export declare function applyModifier<T>(base: T, mod: TimedModifier<T>): T;
|
|
136
125
|
/** Tween state. */
|
|
137
126
|
interface Tween {
|
|
138
127
|
/** Start value. */
|
|
@@ -155,18 +144,16 @@ interface Tween {
|
|
|
155
144
|
next: Tween | null;
|
|
156
145
|
}
|
|
157
146
|
/** Create a tween. */
|
|
158
|
-
declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
147
|
+
export declare function tween(from: number, to: number, duration: number, easing?: EasingFn): Tween;
|
|
159
148
|
/**
|
|
160
149
|
* Update a tween by dt seconds. Returns the current value.
|
|
161
150
|
* When complete, triggers onComplete callback and advances to chained tween.
|
|
162
151
|
*/
|
|
163
|
-
declare function updateTween(tw: Tween, dt: number): number;
|
|
152
|
+
export declare function updateTween(tw: Tween, dt: number): number;
|
|
164
153
|
/** Chain another tween to run after this one completes. Returns the chained tween. */
|
|
165
|
-
declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
154
|
+
export declare function chainTween(tw: Tween, next: Tween): Tween;
|
|
166
155
|
/** Reset a tween to its initial state. */
|
|
167
|
-
declare function resetTween(tw: Tween): void;
|
|
168
|
-
//#endregion
|
|
169
|
-
//#region src/tween-manager.d.ts
|
|
156
|
+
export declare function resetTween(tw: Tween): void;
|
|
170
157
|
/**
|
|
171
158
|
* Manages a collection of active tweens.
|
|
172
159
|
* Automatically removes completed tweens (or advances to chained tweens).
|
|
@@ -176,16 +163,15 @@ interface TweenManager {
|
|
|
176
163
|
readonly tweens: Tween[];
|
|
177
164
|
}
|
|
178
165
|
/** Create a new TweenManager. */
|
|
179
|
-
declare function createTweenManager(): TweenManager;
|
|
166
|
+
export declare function createTweenManager(): TweenManager;
|
|
180
167
|
/** Add a tween to the manager. */
|
|
181
|
-
declare function addTween(mgr: TweenManager, tw: Tween): void;
|
|
168
|
+
export declare function addTween(mgr: TweenManager, tw: Tween): void;
|
|
182
169
|
/**
|
|
183
170
|
* Update all managed tweens by dt seconds.
|
|
184
171
|
* Completed tweens are removed; chained tweens replace completed ones.
|
|
185
172
|
*/
|
|
186
|
-
declare function updateTweens(mgr: TweenManager, dt: number): void;
|
|
173
|
+
export declare function updateTweens(mgr: TweenManager, dt: number): void;
|
|
187
174
|
/** Remove all tweens. */
|
|
188
|
-
declare function clearTweens(mgr: TweenManager): void;
|
|
189
|
-
|
|
190
|
-
export { type PeriodicTween, type Spring, type SpringConfig, type SpriteAnimatorState, type StepSequence, type StepSequenceStep, type TimedModifier, type Tween, type TweenManager, addTween, applyModifier, chainTween, clearTweens, createSpriteAnimator, createStepSequence, createTimedModifier, createTweenManager, getCurrentFrame, isModifierActive, pauseAnimation, periodicTween, playAnimation, resetPeriodicTween, resetSpring, resetStepSequence, resetTween, resumeAnimation, setSpringTarget, spring, stopAnimation, tickModifier, tickStepSequence, tween, updateAnimator, updatePeriodicTween, updateSpring, updateTween, updateTweens };
|
|
175
|
+
export declare function clearTweens(mgr: TweenManager): void;
|
|
176
|
+
export type { PeriodicTween, Spring, SpringConfig, SpriteAnimatorState, StepSequence, StepSequenceStep, TimedModifier, Tween, TweenManager };
|
|
191
177
|
//# sourceMappingURL=index.d.ts.map
|
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":"
|
|
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;;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@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _flare_engine_math = require("@flare-engine/math");
|
|
3
|
-
|
|
3
|
+
// src/periodic.ts
|
|
4
4
|
/**
|
|
5
5
|
* Create a periodic oscillating tween.
|
|
6
6
|
*
|
|
@@ -39,8 +39,7 @@ function resetPeriodicTween(tw) {
|
|
|
39
39
|
tw.elapsed = 0;
|
|
40
40
|
tw.value = tw.from;
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
//#region src/spring.ts
|
|
42
|
+
// src/spring.ts
|
|
44
43
|
const REST_THRESHOLD = .001;
|
|
45
44
|
const VELOCITY_THRESHOLD = .001;
|
|
46
45
|
/** Create a spring with initial value. */
|
|
@@ -87,8 +86,7 @@ function resetSpring(s, value) {
|
|
|
87
86
|
s.velocity = 0;
|
|
88
87
|
s.atRest = true;
|
|
89
88
|
}
|
|
90
|
-
|
|
91
|
-
//#region src/sprite-animator.ts
|
|
89
|
+
// src/sprite-animator.ts
|
|
92
90
|
/** Create a sprite animator state bound to a sprite sheet. */
|
|
93
91
|
function createSpriteAnimator(sheet) {
|
|
94
92
|
return {
|
|
@@ -138,12 +136,14 @@ function updateAnimator(state, dt) {
|
|
|
138
136
|
while (state.elapsed >= seq.frameDuration) {
|
|
139
137
|
state.elapsed -= seq.frameDuration;
|
|
140
138
|
state.frameIndex++;
|
|
141
|
-
if (state.frameIndex >= seq.frameIndices.length)
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
139
|
+
if (state.frameIndex >= seq.frameIndices.length) {
|
|
140
|
+
if (seq.loop) state.frameIndex = 0;
|
|
141
|
+
else {
|
|
142
|
+
state.frameIndex = seq.frameIndices.length - 1;
|
|
143
|
+
state.finished = true;
|
|
144
|
+
state.elapsed = 0;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
return getCurrentFrame(state);
|
|
@@ -157,8 +157,7 @@ function getCurrentFrame(state) {
|
|
|
157
157
|
if (globalIdx === void 0) return null;
|
|
158
158
|
return state.sheet.frames[globalIdx] ?? null;
|
|
159
159
|
}
|
|
160
|
-
|
|
161
|
-
//#region src/step-sequence.ts
|
|
160
|
+
// src/step-sequence.ts
|
|
162
161
|
function createStepSequence(steps) {
|
|
163
162
|
if (steps.length === 0) throw new Error("createStepSequence: steps must not be empty");
|
|
164
163
|
for (let i = 0; i < steps.length; i++) if ((steps[i]?.duration ?? 0) <= 0) throw new Error("createStepSequence: each step duration must be > 0");
|
|
@@ -180,8 +179,7 @@ function resetStepSequence(seq) {
|
|
|
180
179
|
seq.current = 0;
|
|
181
180
|
seq.elapsed = 0;
|
|
182
181
|
}
|
|
183
|
-
|
|
184
|
-
//#region src/timed-modifier.ts
|
|
182
|
+
// src/timed-modifier.ts
|
|
185
183
|
function createTimedModifier(value, duration, combine) {
|
|
186
184
|
if (duration <= 0) throw new Error("createTimedModifier: duration must be > 0");
|
|
187
185
|
return {
|
|
@@ -203,8 +201,7 @@ function isModifierActive(mod) {
|
|
|
203
201
|
function applyModifier(base, mod) {
|
|
204
202
|
return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
|
|
205
203
|
}
|
|
206
|
-
|
|
207
|
-
//#region src/tween.ts
|
|
204
|
+
// src/tween.ts
|
|
208
205
|
/** Linear easing fallback. */
|
|
209
206
|
function linearEasing(t) {
|
|
210
207
|
return t;
|
|
@@ -252,8 +249,7 @@ function resetTween(tw) {
|
|
|
252
249
|
tw.value = tw.from;
|
|
253
250
|
tw.done = false;
|
|
254
251
|
}
|
|
255
|
-
|
|
256
|
-
//#region src/tween-manager.ts
|
|
252
|
+
// src/tween-manager.ts
|
|
257
253
|
/** Create a new TweenManager. */
|
|
258
254
|
function createTweenManager() {
|
|
259
255
|
return { tweens: [] };
|
|
@@ -272,21 +268,21 @@ function updateTweens(mgr, dt) {
|
|
|
272
268
|
while (i < tweens.length) {
|
|
273
269
|
const tw = tweens[i];
|
|
274
270
|
updateTween(tw, dt);
|
|
275
|
-
if (tw.done)
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
271
|
+
if (tw.done) {
|
|
272
|
+
if (tw.next) {
|
|
273
|
+
tweens[i] = tw.next;
|
|
274
|
+
i++;
|
|
275
|
+
} else {
|
|
276
|
+
tweens[i] = tweens[tweens.length - 1];
|
|
277
|
+
tweens.pop();
|
|
278
|
+
}
|
|
279
|
+
} else i++;
|
|
283
280
|
}
|
|
284
281
|
}
|
|
285
282
|
/** Remove all tweens. */
|
|
286
283
|
function clearTweens(mgr) {
|
|
287
284
|
mgr.tweens.length = 0;
|
|
288
285
|
}
|
|
289
|
-
//#endregion
|
|
290
286
|
exports.addTween = addTween;
|
|
291
287
|
exports.applyModifier = applyModifier;
|
|
292
288
|
exports.chainTween = chainTween;
|
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,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O;GAEL,OAAO,KAAK,OAAO,OAAO,SAAS;GACnC,OAAO,IAAI;EACb;OAEA;CAEJ;AACF;;AAGA,SAAgB,YAAY,KAAyB;CACnD,IAAK,OAAmB,SAAS;AACnC"}
|
|
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;;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Q,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;;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"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { easeInOutSine } from "@flare-engine/math";
|
|
2
|
-
|
|
2
|
+
// src/periodic.ts
|
|
3
3
|
/**
|
|
4
4
|
* Create a periodic oscillating tween.
|
|
5
5
|
*
|
|
@@ -38,8 +38,7 @@ function resetPeriodicTween(tw) {
|
|
|
38
38
|
tw.elapsed = 0;
|
|
39
39
|
tw.value = tw.from;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
//#region src/spring.ts
|
|
41
|
+
// src/spring.ts
|
|
43
42
|
const REST_THRESHOLD = .001;
|
|
44
43
|
const VELOCITY_THRESHOLD = .001;
|
|
45
44
|
/** Create a spring with initial value. */
|
|
@@ -86,8 +85,7 @@ function resetSpring(s, value) {
|
|
|
86
85
|
s.velocity = 0;
|
|
87
86
|
s.atRest = true;
|
|
88
87
|
}
|
|
89
|
-
|
|
90
|
-
//#region src/sprite-animator.ts
|
|
88
|
+
// src/sprite-animator.ts
|
|
91
89
|
/** Create a sprite animator state bound to a sprite sheet. */
|
|
92
90
|
function createSpriteAnimator(sheet) {
|
|
93
91
|
return {
|
|
@@ -137,12 +135,14 @@ function updateAnimator(state, dt) {
|
|
|
137
135
|
while (state.elapsed >= seq.frameDuration) {
|
|
138
136
|
state.elapsed -= seq.frameDuration;
|
|
139
137
|
state.frameIndex++;
|
|
140
|
-
if (state.frameIndex >= seq.frameIndices.length)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
138
|
+
if (state.frameIndex >= seq.frameIndices.length) {
|
|
139
|
+
if (seq.loop) state.frameIndex = 0;
|
|
140
|
+
else {
|
|
141
|
+
state.frameIndex = seq.frameIndices.length - 1;
|
|
142
|
+
state.finished = true;
|
|
143
|
+
state.elapsed = 0;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
return getCurrentFrame(state);
|
|
@@ -156,8 +156,7 @@ function getCurrentFrame(state) {
|
|
|
156
156
|
if (globalIdx === void 0) return null;
|
|
157
157
|
return state.sheet.frames[globalIdx] ?? null;
|
|
158
158
|
}
|
|
159
|
-
|
|
160
|
-
//#region src/step-sequence.ts
|
|
159
|
+
// src/step-sequence.ts
|
|
161
160
|
function createStepSequence(steps) {
|
|
162
161
|
if (steps.length === 0) throw new Error("createStepSequence: steps must not be empty");
|
|
163
162
|
for (let i = 0; i < steps.length; i++) if ((steps[i]?.duration ?? 0) <= 0) throw new Error("createStepSequence: each step duration must be > 0");
|
|
@@ -179,8 +178,7 @@ function resetStepSequence(seq) {
|
|
|
179
178
|
seq.current = 0;
|
|
180
179
|
seq.elapsed = 0;
|
|
181
180
|
}
|
|
182
|
-
|
|
183
|
-
//#region src/timed-modifier.ts
|
|
181
|
+
// src/timed-modifier.ts
|
|
184
182
|
function createTimedModifier(value, duration, combine) {
|
|
185
183
|
if (duration <= 0) throw new Error("createTimedModifier: duration must be > 0");
|
|
186
184
|
return {
|
|
@@ -202,8 +200,7 @@ function isModifierActive(mod) {
|
|
|
202
200
|
function applyModifier(base, mod) {
|
|
203
201
|
return mod.remaining > 0 ? mod.combine(base, mod.value) : base;
|
|
204
202
|
}
|
|
205
|
-
|
|
206
|
-
//#region src/tween.ts
|
|
203
|
+
// src/tween.ts
|
|
207
204
|
/** Linear easing fallback. */
|
|
208
205
|
function linearEasing(t) {
|
|
209
206
|
return t;
|
|
@@ -251,8 +248,7 @@ function resetTween(tw) {
|
|
|
251
248
|
tw.value = tw.from;
|
|
252
249
|
tw.done = false;
|
|
253
250
|
}
|
|
254
|
-
|
|
255
|
-
//#region src/tween-manager.ts
|
|
251
|
+
// src/tween-manager.ts
|
|
256
252
|
/** Create a new TweenManager. */
|
|
257
253
|
function createTweenManager() {
|
|
258
254
|
return { tweens: [] };
|
|
@@ -271,21 +267,21 @@ function updateTweens(mgr, dt) {
|
|
|
271
267
|
while (i < tweens.length) {
|
|
272
268
|
const tw = tweens[i];
|
|
273
269
|
updateTween(tw, dt);
|
|
274
|
-
if (tw.done)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
270
|
+
if (tw.done) {
|
|
271
|
+
if (tw.next) {
|
|
272
|
+
tweens[i] = tw.next;
|
|
273
|
+
i++;
|
|
274
|
+
} else {
|
|
275
|
+
tweens[i] = tweens[tweens.length - 1];
|
|
276
|
+
tweens.pop();
|
|
277
|
+
}
|
|
278
|
+
} else i++;
|
|
282
279
|
}
|
|
283
280
|
}
|
|
284
281
|
/** Remove all tweens. */
|
|
285
282
|
function clearTweens(mgr) {
|
|
286
283
|
mgr.tweens.length = 0;
|
|
287
284
|
}
|
|
288
|
-
//#endregion
|
|
289
285
|
export { addTween, applyModifier, chainTween, clearTweens, createSpriteAnimator, createStepSequence, createTimedModifier, createTweenManager, getCurrentFrame, isModifierActive, pauseAnimation, periodicTween, playAnimation, resetPeriodicTween, resetSpring, resetStepSequence, resetTween, resumeAnimation, setSpringTarget, spring, stopAnimation, tickModifier, tickStepSequence, tween, updateAnimator, updatePeriodicTween, updateSpring, updateTween, updateTweens };
|
|
290
286
|
|
|
291
287
|
//# sourceMappingURL=index.mjs.map
|
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,G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js","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;EACA,QAAQ;GACN,WAAW,QAAQ,aAAa;GAChC,SAAS,QAAQ,WAAW;GAC5B,M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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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/animation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.7",
|
|
4
4
|
"description": "Sprite animation, tweens, springs, step sequences",
|
|
5
5
|
"keywords": ["flare-engine", "animation", "tween", "spring"],
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"typecheck": "tsc --noEmit"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@flare-engine/assets": "^0.4.
|
|
41
|
-
"@flare-engine/math": "^0.4.
|
|
40
|
+
"@flare-engine/assets": "^0.4.7",
|
|
41
|
+
"@flare-engine/math": "^0.4.7"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=20"
|