@flare-engine/scene 0.4.10 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -24,7 +24,11 @@ interface Scene {
24
24
  onDraw?(): void;
25
25
  /** Called when the scene is deactivated (pushed or replaced). */
26
26
  onExit?(): void;
27
- /** Called when the scene is permanently removed. Cleanup resources. */
27
+ /**
28
+ * Called when the scene is permanently removed. Cleanup resources. Can also follow `onPreload`
29
+ * with no `onInit` in between — a target superseded by another `go()` during a fade-out, or
30
+ * still pending at `destroy()` — possibly while the `onPreload` promise is still unresolved.
31
+ */
28
32
  onDestroy?(): void;
29
33
  }
30
34
  /**
@@ -102,6 +106,7 @@ export declare class SceneManager {
102
106
  private _pendingData;
103
107
  private _preloadResolved;
104
108
  private _preloadGen;
109
+ private _warnedPendingGo;
105
110
  /** The currently active scene (top of stack). */
106
111
  get current(): Scene | undefined;
107
112
  /** Number of scenes on the stack. */
@@ -116,12 +121,16 @@ export declare class SceneManager {
116
121
  * Navigate to a scene by name. Clears the entire stack.
117
122
  * Calls onExit on the current scene, onDestroy on all stacked scenes,
118
123
  * then onPreload → onInit → onEnter on the new scene.
124
+ * A `go()` issued while an animated `go()` is still fading out supersedes
125
+ * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).
119
126
  */
120
127
  go(name: string, data?: unknown): Promise<void>;
121
128
  /**
122
129
  * Push a scene onto the stack. The current scene gets onExit, unless
123
130
  * `options.overlay` is true — then it stays live and is updated and drawn
124
131
  * before the pushed scene. The new scene gets onPreload → onInit → onEnter.
132
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
133
+ * the stack when the fade-out ends; nothing is called and nothing changes.
125
134
  */
126
135
  push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void>;
127
136
  /**
@@ -129,6 +138,8 @@ export declare class SceneManager {
129
138
  * `options.keep` is true — a kept scene stays initialized so the next `push`
130
139
  * of its name goes straight to onEnter. The scene below gets
131
140
  * onEnter(options.data) unless the popped scene was pushed as an overlay.
141
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
142
+ * the stack when the fade-out ends; nothing is called and nothing changes.
132
143
  */
133
144
  pop(options?: ScenePopOptions): void;
134
145
  /** Update the live scenes (bottom-up, overlays last) and any active transition. */
@@ -139,10 +150,13 @@ export declare class SceneManager {
139
150
  get transitionProgress(): number;
140
151
  /** Get the current transition phase. */
141
152
  get transitionPhase(): 'out' | 'in' | null;
153
+ /** True while an animated `go()` target is pending; warns once per manager in dev. */
154
+ private _refusedByPendingGo;
142
155
  private _getOrCreate;
143
156
  private _initIfNeeded;
144
157
  private _activateScene;
145
- private _exitAndDestroyCurrent;
158
+ /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */
159
+ private _teardownStack;
146
160
  /**
147
161
  * Destroy scenes popped with `keep: true` that were never pushed again. They
148
162
  * already got `onExit` on the pop. The cache entry is dropped only when it is
@@ -150,6 +164,14 @@ export declare class SceneManager {
150
164
  * replaced it with the pending scene.
151
165
  */
152
166
  private _destroyKept;
167
+ /**
168
+ * Destroy the target of an animated `go()` that was superseded before its
169
+ * out-phase finished. It got `onPreload` but never `onInit`. Fields are
170
+ * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;
171
+ * an instance someone else still holds (stack, kept set) or has already
172
+ * destroyed (evicted from the cache) is left alone.
173
+ */
174
+ private _discardPending;
153
175
  private _finishOutPhase;
154
176
  /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
155
177
  private _updateStack;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/scene.ts","../src/transition.ts","../src/scene-manager.ts"],"mappings":";;;;;;;UAOiB;;WAEN;;EAGT,OAAO;;EAGP,QAAQ;;EAGR,qBAAqB;;EAGrB;;EAGA,SAAS;;EAGT,UAAU;;EAGV;;EAGA;;EAGA;;;;;;;;;KAUU,gBAAgB,OAAO,OAAO,QAAQ,aAAa;;KC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;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;MAGJ,WAAW;;MAKX;;MAKA;;EAKJ,cAAc,YAAY;;EAK1B,SAAS,cAAc,SAAS;;;;;;;;EAW1B,GAAG,cAAc,iBAAiB;;;;;;;;EAqElC,KAAK,cAAc,gBAAgB,UAAU,mBAAmB;;;;;;;;;EAsBtE,IAAI,UAAU;;EAmBd,OAAO;;EAUP;;MASI;;MAMA;;UAKI;UAYA;UAYM;UAOA;;UAQN;;;;;;;UAmBA;;;;;;;;UAkBA;UAiBA;;UAwBA;UAYA"}
package/dist/index.d.ts CHANGED
@@ -24,7 +24,11 @@ interface Scene {
24
24
  onDraw?(): void;
25
25
  /** Called when the scene is deactivated (pushed or replaced). */
26
26
  onExit?(): void;
27
- /** Called when the scene is permanently removed. Cleanup resources. */
27
+ /**
28
+ * Called when the scene is permanently removed. Cleanup resources. Can also follow `onPreload`
29
+ * with no `onInit` in between — a target superseded by another `go()` during a fade-out, or
30
+ * still pending at `destroy()` — possibly while the `onPreload` promise is still unresolved.
31
+ */
28
32
  onDestroy?(): void;
29
33
  }
30
34
  /**
@@ -102,6 +106,7 @@ export declare class SceneManager {
102
106
  private _pendingData;
103
107
  private _preloadResolved;
104
108
  private _preloadGen;
109
+ private _warnedPendingGo;
105
110
  /** The currently active scene (top of stack). */
106
111
  get current(): Scene | undefined;
107
112
  /** Number of scenes on the stack. */
@@ -116,12 +121,16 @@ export declare class SceneManager {
116
121
  * Navigate to a scene by name. Clears the entire stack.
117
122
  * Calls onExit on the current scene, onDestroy on all stacked scenes,
118
123
  * then onPreload → onInit → onEnter on the new scene.
124
+ * A `go()` issued while an animated `go()` is still fading out supersedes
125
+ * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).
119
126
  */
120
127
  go(name: string, data?: unknown): Promise<void>;
121
128
  /**
122
129
  * Push a scene onto the stack. The current scene gets onExit, unless
123
130
  * `options.overlay` is true — then it stays live and is updated and drawn
124
131
  * before the pushed scene. The new scene gets onPreload → onInit → onEnter.
132
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
133
+ * the stack when the fade-out ends; nothing is called and nothing changes.
125
134
  */
126
135
  push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void>;
127
136
  /**
@@ -129,6 +138,8 @@ export declare class SceneManager {
129
138
  * `options.keep` is true — a kept scene stays initialized so the next `push`
130
139
  * of its name goes straight to onEnter. The scene below gets
131
140
  * onEnter(options.data) unless the popped scene was pushed as an overlay.
141
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
142
+ * the stack when the fade-out ends; nothing is called and nothing changes.
132
143
  */
133
144
  pop(options?: ScenePopOptions): void;
134
145
  /** Update the live scenes (bottom-up, overlays last) and any active transition. */
@@ -139,10 +150,13 @@ export declare class SceneManager {
139
150
  get transitionProgress(): number;
140
151
  /** Get the current transition phase. */
141
152
  get transitionPhase(): 'out' | 'in' | null;
153
+ /** True while an animated `go()` target is pending; warns once per manager in dev. */
154
+ private _refusedByPendingGo;
142
155
  private _getOrCreate;
143
156
  private _initIfNeeded;
144
157
  private _activateScene;
145
- private _exitAndDestroyCurrent;
158
+ /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */
159
+ private _teardownStack;
146
160
  /**
147
161
  * Destroy scenes popped with `keep: true` that were never pushed again. They
148
162
  * already got `onExit` on the pop. The cache entry is dropped only when it is
@@ -150,6 +164,14 @@ export declare class SceneManager {
150
164
  * replaced it with the pending scene.
151
165
  */
152
166
  private _destroyKept;
167
+ /**
168
+ * Destroy the target of an animated `go()` that was superseded before its
169
+ * out-phase finished. It got `onPreload` but never `onInit`. Fields are
170
+ * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;
171
+ * an instance someone else still holds (stack, kept set) or has already
172
+ * destroyed (evicted from the cache) is left alone.
173
+ */
174
+ private _discardPending;
153
175
  private _finishOutPhase;
154
176
  /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
155
177
  private _updateStack;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/scene.ts","../src/transition.ts","../src/scene-manager.ts"],"mappings":";;;;;;;UAOiB;;WAEN;;EAGT,OAAO;;EAGP,QAAQ;;EAGR,qBAAqB;;EAGrB;;EAGA,SAAS;;EAGT,UAAU;;EAGV;;EAGA;;EAGA;;;;;;;;;KAUU,gBAAgB,OAAO,OAAO,QAAQ,aAAa;;KCzCnD;;UAGK;;EAEf;;EAEA;;EAEA;;;UAIe;;WAEN;;WAGA;;WAGA,YAAY;;EAGrB,MAAM,sBAAsB;;EAG5B,OAAO,OAAO,iBAAiB,aAAa;;;wBAgC9B,kBAAkB;;wBAgBlB,eAAe,mBAAgB,SAAQ,WAAsB;;;;;wBAmB7D,gBACd,mBACA,YAAW,gBACX,SAAQ,WACP;UC/Fc;;EAEf;;UAGe;;EAEf;;EAEA;;;;;;;;;;;qBAYW;UACH;UACA;UACA;UACA;;UAEA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;;MAGJ,WAAW;;MAKX;;MAKA;;EAKJ,cAAc,YAAY;;EAK1B,SAAS,cAAc,SAAS;;;;;;EAS1B,GAAG,cAAc,iBAAiB;;;;;;EA6DlC,KAAK,cAAc,gBAAgB,UAAU,mBAAmB;;;;;;;EAmBtE,IAAI,UAAU;;EAkBd,OAAO;;EAUP;;MAgBI;;MAMA;UAII;UAYM;UAOA;UAmBN;;;;;;;UAmBA;UAWA;;UAiCA;UAYA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/scene.ts","../src/transition.ts","../src/scene-manager.ts"],"mappings":";;;;;;;UAOiB;;WAEN;;EAGT,OAAO;;EAGP,QAAQ;;EAGR,qBAAqB;;EAGrB;;EAGA,SAAS;;EAGT,UAAU;;EAGV;;EAGA;;;;;;EAOA;;;;;;;;;KAUU,gBAAgB,OAAO,OAAO,QAAQ,aAAa;;KC7CnD;;UAGK;;EAEf;;EAEA;;EAEA;;;UAIe;;WAEN;;WAGA;;WAGA,YAAY;;EAGrB,MAAM,sBAAsB;;EAG5B,OAAO,OAAO,iBAAiB,aAAa;;;wBAgC9B,kBAAkB;;wBAgBlB,eAAe,mBAAgB,SAAQ,WAAsB;;;;;wBAmB7D,gBACd,mBACA,YAAW,gBACX,SAAQ,WACP;UC/Fc;;EAEf;;UAGe;;EAEf;;EAEA;;;;;;;;;;;qBAYW;UACH;UACA;UACA;UACA;;UAEA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;MAGJ,WAAW;;MAKX;;MAKA;;EAKJ,cAAc,YAAY;;EAK1B,SAAS,cAAc,SAAS;;;;;;;;EAW1B,GAAG,cAAc,iBAAiB;;;;;;;;EAqElC,KAAK,cAAc,gBAAgB,UAAU,mBAAmB;;;;;;;;;EAsBtE,IAAI,UAAU;;EAmBd,OAAO;;EAUP;;MASI;;MAMA;;UAKI;UAYA;UAYM;UAOA;;UAQN;;;;;;;UAmBA;;;;;;;;UAkBA;UAiBA;;UAwBA;UAYA"}
package/dist/index.js CHANGED
@@ -112,6 +112,7 @@ var SceneManager = class {
112
112
  _pendingData = void 0;
113
113
  _preloadResolved = true;
114
114
  _preloadGen = 0;
115
+ _warnedPendingGo = false;
115
116
  /** The currently active scene (top of stack). */
116
117
  get current() {
117
118
  return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
@@ -136,6 +137,8 @@ var SceneManager = class {
136
137
  * Navigate to a scene by name. Clears the entire stack.
137
138
  * Calls onExit on the current scene, onDestroy on all stacked scenes,
138
139
  * then onPreload → onInit → onEnter on the new scene.
140
+ * A `go()` issued while an animated `go()` is still fading out supersedes
141
+ * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).
139
142
  */
140
143
  async go(name, data) {
141
144
  if (!this._factories.has(name)) {
@@ -143,6 +146,7 @@ var SceneManager = class {
143
146
  return;
144
147
  }
145
148
  if (this._transition.duration > 0 && this.current) {
149
+ this._discardPending();
146
150
  this._scenes.delete(name);
147
151
  this._initialized.delete(name);
148
152
  const scene = this._getOrCreate(name);
@@ -165,7 +169,9 @@ var SceneManager = class {
165
169
  });
166
170
  this._transitionState = this._transition.start("out");
167
171
  } else {
168
- this._exitAndDestroyCurrent();
172
+ this._discardPending();
173
+ this._transitionState = null;
174
+ this._teardownStack();
169
175
  const scene = this._getOrCreate(name);
170
176
  await this._activateScene(scene, data);
171
177
  }
@@ -174,8 +180,11 @@ var SceneManager = class {
174
180
  * Push a scene onto the stack. The current scene gets onExit, unless
175
181
  * `options.overlay` is true — then it stays live and is updated and drawn
176
182
  * before the pushed scene. The new scene gets onPreload → onInit → onEnter.
183
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
184
+ * the stack when the fade-out ends; nothing is called and nothing changes.
177
185
  */
178
186
  async push(name, data, options) {
187
+ if (this._refusedByPendingGo()) return;
179
188
  const scene = this._getOrCreate(name);
180
189
  if (!scene) return;
181
190
  const overlay = options?.overlay === true;
@@ -191,8 +200,11 @@ var SceneManager = class {
191
200
  * `options.keep` is true — a kept scene stays initialized so the next `push`
192
201
  * of its name goes straight to onEnter. The scene below gets
193
202
  * onEnter(options.data) unless the popped scene was pushed as an overlay.
203
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
204
+ * the stack when the fade-out ends; nothing is called and nothing changes.
194
205
  */
195
206
  pop(options) {
207
+ if (this._refusedByPendingGo()) return;
196
208
  if (this._stack.length <= 1) return;
197
209
  const top = this._stack.pop();
198
210
  const wasOverlay = this._overlay.pop() === true;
@@ -215,18 +227,11 @@ var SceneManager = class {
215
227
  }
216
228
  /** Destroy all scenes and clear state. */
217
229
  destroy() {
218
- for (let i = this._stack.length - 1; i >= 0; i--) {
219
- const scene = this._stack[i];
220
- scene.onExit?.();
221
- scene.onDestroy?.();
222
- }
223
- this._stack.length = 0;
224
- this._overlay.length = 0;
225
- this._destroyKept();
230
+ this._discardPending();
231
+ this._teardownStack();
226
232
  this._scenes.clear();
227
233
  this._initialized.clear();
228
234
  this._transitionState = null;
229
- this._pendingScene = null;
230
235
  }
231
236
  /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */
232
237
  get transitionProgress() {
@@ -237,6 +242,16 @@ var SceneManager = class {
237
242
  get transitionPhase() {
238
243
  return this._transitionState?.phase ?? null;
239
244
  }
245
+ /** True while an animated `go()` target is pending; warns once per manager in dev. */
246
+ _refusedByPendingGo() {
247
+ const pending = this._pendingScene;
248
+ if (!pending) return false;
249
+ if (!this._warnedPendingGo && typeof __DEV__ !== "undefined" && __DEV__) {
250
+ this._warnedPendingGo = true;
251
+ console.warn(`[SceneManager] push()/pop() ignored while go('${pending.name}') is fading out; call them after the fade-out ends`);
252
+ }
253
+ return true;
254
+ }
240
255
  _getOrCreate(name) {
241
256
  let scene = this._scenes.get(name);
242
257
  if (scene) return scene;
@@ -253,22 +268,13 @@ var SceneManager = class {
253
268
  scene.onInit?.();
254
269
  }
255
270
  async _activateScene(scene, data) {
256
- for (let i = this._stack.length - 1; i >= 0; i--) {
257
- const s = this._stack[i];
258
- s.onExit?.();
259
- s.onDestroy?.();
260
- this._scenes.delete(s.name);
261
- this._initialized.delete(s.name);
262
- }
263
- this._stack.length = 0;
264
- this._overlay.length = 0;
265
- this._destroyKept();
266
271
  await this._initIfNeeded(scene);
267
272
  this._stack.push(scene);
268
273
  this._overlay.push(false);
269
274
  scene.onEnter?.(data);
270
275
  }
271
- _exitAndDestroyCurrent() {
276
+ /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */
277
+ _teardownStack() {
272
278
  for (let i = this._stack.length - 1; i >= 0; i--) {
273
279
  const s = this._stack[i];
274
280
  s.onExit?.();
@@ -296,6 +302,23 @@ var SceneManager = class {
296
302
  }
297
303
  this._kept.clear();
298
304
  }
305
+ /**
306
+ * Destroy the target of an animated `go()` that was superseded before its
307
+ * out-phase finished. It got `onPreload` but never `onInit`. Fields are
308
+ * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;
309
+ * an instance someone else still holds (stack, kept set) or has already
310
+ * destroyed (evicted from the cache) is left alone.
311
+ */
312
+ _discardPending() {
313
+ const pending = this._pendingScene;
314
+ if (!pending) return;
315
+ this._pendingScene = null;
316
+ this._pendingData = void 0;
317
+ if (this._scenes.get(pending.name) !== pending || this._stack.includes(pending) || this._kept.has(pending)) return;
318
+ pending.onDestroy?.();
319
+ this._scenes.delete(pending.name);
320
+ this._initialized.delete(pending.name);
321
+ }
299
322
  _finishOutPhase() {
300
323
  if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
301
324
  const pending = this._pendingScene;
@@ -303,16 +326,7 @@ var SceneManager = class {
303
326
  this._pendingScene = null;
304
327
  this._pendingData = void 0;
305
328
  if (pending) {
306
- for (let i = this._stack.length - 1; i >= 0; i--) {
307
- const s = this._stack[i];
308
- s.onExit?.();
309
- s.onDestroy?.();
310
- this._scenes.delete(s.name);
311
- this._initialized.delete(s.name);
312
- }
313
- this._stack.length = 0;
314
- this._overlay.length = 0;
315
- this._destroyKept();
329
+ this._teardownStack();
316
330
  this._scenes.set(pending.name, pending);
317
331
  this._initialized.add(pending.name);
318
332
  pending.onInit?.();
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["World","Camera2D"],"sources":["../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\ndeclare const __DEV__: boolean | undefined;\n\nexport interface ScenePushOptions {\n /** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */\n overlay?: boolean;\n}\n\nexport interface ScenePopOptions {\n /** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */\n keep?: boolean;\n /** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */\n data?: unknown;\n}\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)\n * - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)\n * - `update(dt)` — update the live scenes (bottom-up) and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n /** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */\n private _overlay: boolean[] = [];\n /** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */\n private _kept = new Set<Scene>();\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n private _preloadResolved = true;\n private _preloadGen = 0;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n */\n async go(name: string, data?: unknown): Promise<void> {\n if (!this._factories.has(name)) {\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.warn(\n `[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(', ')}`,\n );\n }\n return;\n }\n\n if (this._transition.duration > 0 && this.current) {\n // Animated transition — evict target from cache first so restart\n // builds a fresh instance with a fresh World/Camera2D.\n this._scenes.delete(name);\n this._initialized.delete(name);\n const scene = this._getOrCreate(name)!;\n this._pendingScene = scene;\n this._pendingData = data;\n\n // Start preload concurrently with the fade-out.\n this._initialized.add(scene.name);\n this._preloadResolved = false;\n const gen = ++this._preloadGen;\n const preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n this._finishOutPhase();\n },\n (err) => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.error(\n `[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`,\n err,\n );\n }\n this._finishOutPhase();\n },\n );\n }\n\n this._transitionState = this._transition.start('out');\n } else {\n // Instant transition — destroy first so _getOrCreate builds fresh.\n this._exitAndDestroyCurrent();\n const scene = this._getOrCreate(name)!;\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit, unless\n * `options.overlay` is true — then it stays live and is updated and drawn\n * before the pushed scene. The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n const overlay = options?.overlay === true;\n if (!overlay) this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._kept.delete(scene);\n this._stack.push(scene);\n this._overlay.push(overlay);\n scene.onEnter?.(data);\n }\n\n /**\n * Pop the top scene. Calls onExit on it, then onDestroy unless\n * `options.keep` is true — a kept scene stays initialized so the next `push`\n * of its name goes straight to onEnter. The scene below gets\n * onEnter(options.data) unless the popped scene was pushed as an overlay.\n */\n pop(options?: ScenePopOptions): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n const wasOverlay = this._overlay.pop() === true;\n top.onExit?.();\n if (options?.keep === true) {\n this._kept.add(top);\n } else {\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n }\n\n if (!wasOverlay) this.current?.onEnter?.(options?.data);\n }\n\n /** Update the live scenes (bottom-up, overlays last) and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n this._updateStack(dt);\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const scene = this._stack[i]!;\n scene.onExit?.();\n scene.onDestroy?.();\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n this._pendingScene = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory(new World(), new Camera2D());\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n // Destroy all stacked scenes\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n this._overlay.push(false);\n scene.onEnter?.(data);\n }\n\n private _exitAndDestroyCurrent(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n }\n\n /**\n * Destroy scenes popped with `keep: true` that were never pushed again. They\n * already got `onExit` on the pop. The cache entry is dropped only when it is\n * still this instance — an animated `go()` to the same name has already\n * replaced it with the pending scene.\n */\n private _destroyKept(): void {\n for (const s of this._kept) {\n s.onDestroy?.();\n if (this._scenes.get(s.name) === s) {\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n }\n this._kept.clear();\n }\n\n private _finishOutPhase(): void {\n if (!this._transitionState?.done || this._transitionState.phase !== 'out') return;\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n\n // Preload already ran in go(); call onInit then enter.\n this._scenes.set(pending.name, pending);\n this._initialized.add(pending.name);\n pending.onInit?.();\n this._stack.push(pending);\n this._overlay.push(false);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n }\n\n /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */\n private _updateStack(dt: number): void {\n const stack = this._stack;\n let from = stack.length - 1;\n if (from < 0) return;\n while (from > 0 && this._overlay[from]) from--;\n for (let i = from; i < stack.length; i++) {\n const scene = stack[i]!;\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Hold until preload (started concurrently in go()) resolves.\n if (!this._preloadResolved) {\n this._updateStack(dt);\n return;\n }\n\n this._finishOutPhase();\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the live scenes during transition\n this._updateStack(dt);\n }\n}\n"],"mappings":";;;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO;EACL,QAAQ;GACN,MAAM;EACR;EACA,QAAQ,OAAwB,IAA6B;GAC3D,IAAI,MAAM,MAAM,OAAO;GACvB,OAAO,KAAK;GACZ,IAAI,OAAO,GAAG;IACZ,MAAM;IACN,MAAM,WAAW;IACjB,MAAM,OAAO;GACf,OACE,MAAM,WAAW,OAAO,GAAG;GAE7B,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,iBAA6B;CAC3C,OAAO;EACL,MAAM;EACN,UAAU;EACV,MAAM,OAAO;GACX,OAAO;IAAE,UAAU;IAAG,MAAM;IAAM;GAAM;EAC1C;EACA,OAAO,OAAO;GACZ,MAAM,WAAW;GACjB,MAAM,OAAO;GACb,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,eAAe,WAAW,IAAK,SAAmB,UAAsB;CACtF,MAAM,WAAW,aAAa,UAAU,MAAM;CAC9C,OAAO;EACL,MAAM;EACN;EACA,MAAM,OAAO;GACX,SAAS,MAAM;GACf,OAAO;IAAE,UAAU;IAAG,MAAM;IAAO;GAAM;EAC3C;EACA,OAAO,OAAO,IAAI;GAChB,OAAO,SAAS,QAAQ,OAAO,EAAE;EACnC;CACF;AACF;;;;;AAMA,SAAgB,gBACd,WAAW,IACX,YAA4B,QAC5B,SAAmB,UACP;CACZ,MAAM,WAAW,aAAa,UAAU,MAAM;CAC9C,OAAO;EACL,MAAM;EACN;EACA;EACA,M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GACrB,KAAK,SAAS,SAAS;GACvB,KAAK,aAAa;GAGlB,KAAK,QAAQ,IAAI,QAAQ,MAAM,OAAO;GACtC,KAAK,aAAa,IAAI,QAAQ,IAAI;GAClC,QAAQ,SAAS;GACjB,KAAK,OAAO,KAAK,OAAO;GACxB,KAAK,SAAS,KAAK,KAAK;GACxB,QAAQ,UAAU,IAAI;EACxB;EAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;CACrD;;CAGA,aAAqB,IAAkB;EACrC,MAAM,QAAQ,KAAK;EACnB,IAAI,OAAO,MAAM,SAAS;EAC1B,IAAI,OAAO,GAAG;EACd,OAAO,OAAO,KAAK,KAAK,SAAS,OAAO;EACxC,KAAK,IAAI,IAAI,MAAM,IAAI,MAAM,QAAQ,KAAK;GACxC,MAAM,QAAQ,MAAM;GACpB,MAAM,WAAW,EAAE;GACnB,MAAM,SAAS;EACjB;CACF;CAEA,kBAA0B,IAAkB;EAC1C,IAAI,CAAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MAAM;GAC9B,IAAI,KAAK,iBAAiB,UAAU,OAAO;IAEzC,IAAI,CAAC,KAAK,kBAAkB;KAC1B,KAAK,aAAa,EAAE;KACpB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,KAAK,aAAa,EAAE;CACtB;AACF"}
1
+ {"version":3,"file":"index.js","names":["World","Camera2D"],"sources":["../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\ndeclare const __DEV__: boolean | undefined;\n\nexport interface ScenePushOptions {\n /** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */\n overlay?: boolean;\n}\n\nexport interface ScenePopOptions {\n /** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */\n keep?: boolean;\n /** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */\n data?: unknown;\n}\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)\n * - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)\n * - `update(dt)` — update the live scenes (bottom-up) and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n /** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */\n private _overlay: boolean[] = [];\n /** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */\n private _kept = new Set<Scene>();\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n private _preloadResolved = true;\n private _preloadGen = 0;\n private _warnedPendingGo = false;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n * A `go()` issued while an animated `go()` is still fading out supersedes\n * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).\n */\n async go(name: string, data?: unknown): Promise<void> {\n if (!this._factories.has(name)) {\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.warn(\n `[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(', ')}`,\n );\n }\n return;\n }\n\n if (this._transition.duration > 0 && this.current) {\n this._discardPending();\n // Animated transition — evict target from cache first so restart\n // builds a fresh instance with a fresh World/Camera2D.\n this._scenes.delete(name);\n this._initialized.delete(name);\n const scene = this._getOrCreate(name)!;\n this._pendingScene = scene;\n this._pendingData = data;\n\n // Start preload concurrently with the fade-out.\n this._initialized.add(scene.name);\n this._preloadResolved = false;\n const gen = ++this._preloadGen;\n const preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n this._finishOutPhase();\n },\n (err) => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.error(\n `[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`,\n err,\n );\n }\n this._finishOutPhase();\n },\n );\n }\n\n this._transitionState = this._transition.start('out');\n } else {\n // A pending animated go() is superseded; end any transition in progress\n // too, or _finishOutPhase would later replace the instant scene with the\n // stale target. Its late preload handler is harmless once the state is null.\n this._discardPending();\n this._transitionState = null;\n // Instant transition — destroy first so _getOrCreate builds fresh.\n this._teardownStack();\n const scene = this._getOrCreate(name)!;\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit, unless\n * `options.overlay` is true — then it stays live and is updated and drawn\n * before the pushed scene. The new scene gets onPreload → onInit → onEnter.\n * Ignored while an animated `go()` is still fading out — that `go()` clears\n * the stack when the fade-out ends; nothing is called and nothing changes.\n */\n async push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void> {\n if (this._refusedByPendingGo()) return;\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n const overlay = options?.overlay === true;\n if (!overlay) this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._kept.delete(scene);\n this._stack.push(scene);\n this._overlay.push(overlay);\n scene.onEnter?.(data);\n }\n\n /**\n * Pop the top scene. Calls onExit on it, then onDestroy unless\n * `options.keep` is true — a kept scene stays initialized so the next `push`\n * of its name goes straight to onEnter. The scene below gets\n * onEnter(options.data) unless the popped scene was pushed as an overlay.\n * Ignored while an animated `go()` is still fading out — that `go()` clears\n * the stack when the fade-out ends; nothing is called and nothing changes.\n */\n pop(options?: ScenePopOptions): void {\n if (this._refusedByPendingGo()) return;\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n const wasOverlay = this._overlay.pop() === true;\n top.onExit?.();\n if (options?.keep === true) {\n this._kept.add(top);\n } else {\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n }\n\n if (!wasOverlay) this.current?.onEnter?.(options?.data);\n }\n\n /** Update the live scenes (bottom-up, overlays last) and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n this._updateStack(dt);\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n this._discardPending();\n this._teardownStack();\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n /** True while an animated `go()` target is pending; warns once per manager in dev. */\n private _refusedByPendingGo(): boolean {\n const pending = this._pendingScene;\n if (!pending) return false;\n if (!this._warnedPendingGo && typeof __DEV__ !== 'undefined' && __DEV__) {\n this._warnedPendingGo = true;\n console.warn(\n `[SceneManager] push()/pop() ignored while go('${pending.name}') is fading out; call them after the fade-out ends`,\n );\n }\n return true;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory(new World(), new Camera2D());\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n this._overlay.push(false);\n scene.onEnter?.(data);\n }\n\n /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */\n private _teardownStack(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n }\n\n /**\n * Destroy scenes popped with `keep: true` that were never pushed again. They\n * already got `onExit` on the pop. The cache entry is dropped only when it is\n * still this instance — an animated `go()` to the same name has already\n * replaced it with the pending scene.\n */\n private _destroyKept(): void {\n for (const s of this._kept) {\n s.onDestroy?.();\n if (this._scenes.get(s.name) === s) {\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n }\n this._kept.clear();\n }\n\n /**\n * Destroy the target of an animated `go()` that was superseded before its\n * out-phase finished. It got `onPreload` but never `onInit`. Fields are\n * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;\n * an instance someone else still holds (stack, kept set) or has already\n * destroyed (evicted from the cache) is left alone.\n */\n private _discardPending(): void {\n const pending = this._pendingScene;\n if (!pending) return;\n this._pendingScene = null;\n this._pendingData = undefined;\n if (\n this._scenes.get(pending.name) !== pending ||\n this._stack.includes(pending) ||\n this._kept.has(pending)\n ) {\n return;\n }\n pending.onDestroy?.();\n this._scenes.delete(pending.name);\n this._initialized.delete(pending.name);\n }\n\n private _finishOutPhase(): void {\n if (!this._transitionState?.done || this._transitionState.phase !== 'out') return;\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n this._teardownStack();\n\n // Preload already ran in go(); call onInit then enter.\n this._scenes.set(pending.name, pending);\n this._initialized.add(pending.name);\n pending.onInit?.();\n this._stack.push(pending);\n this._overlay.push(false);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n }\n\n /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */\n private _updateStack(dt: number): void {\n const stack = this._stack;\n let from = stack.length - 1;\n if (from < 0) return;\n while (from > 0 && this._overlay[from]) from--;\n for (let i = from; i < stack.length; i++) {\n const scene = stack[i]!;\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Hold until preload (started concurrently in go()) resolves.\n if (!this._preloadResolved) {\n this._updateStack(dt);\n return;\n }\n\n this._finishOutPhase();\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the live scenes during transition\n this._updateStack(dt);\n }\n}\n"],"mappings":";;;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO;EACL,QAAQ;GACN,MAAM;EACR;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ACxC,KAAK,IAAI,IAAI,MAAM,IAAI,MAAM,QAAQ,KAAK;GACxC,MAAM,QAAQ,MAAM;GACpB,MAAM,WAAW,EAAE;GACnB,MAAM,SAAS;EACjB;CACF;CAEA,kBAA0B,IAAkB;EAC1C,IAAI,CAAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MAAM;GAC9B,IAAI,KAAK,iBAAiB,UAAU,OAAO;IAEzC,IAAI,CAAC,KAAK,kBAAkB;KAC1B,KAAK,aAAa,EAAE;KACpB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,KAAK,aAAa,EAAE;CACtB;AACF"}
package/dist/index.mjs CHANGED
@@ -111,6 +111,7 @@ var SceneManager = class {
111
111
  _pendingData = void 0;
112
112
  _preloadResolved = true;
113
113
  _preloadGen = 0;
114
+ _warnedPendingGo = false;
114
115
  /** The currently active scene (top of stack). */
115
116
  get current() {
116
117
  return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
@@ -135,6 +136,8 @@ var SceneManager = class {
135
136
  * Navigate to a scene by name. Clears the entire stack.
136
137
  * Calls onExit on the current scene, onDestroy on all stacked scenes,
137
138
  * then onPreload → onInit → onEnter on the new scene.
139
+ * A `go()` issued while an animated `go()` is still fading out supersedes
140
+ * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).
138
141
  */
139
142
  async go(name, data) {
140
143
  if (!this._factories.has(name)) {
@@ -142,6 +145,7 @@ var SceneManager = class {
142
145
  return;
143
146
  }
144
147
  if (this._transition.duration > 0 && this.current) {
148
+ this._discardPending();
145
149
  this._scenes.delete(name);
146
150
  this._initialized.delete(name);
147
151
  const scene = this._getOrCreate(name);
@@ -164,7 +168,9 @@ var SceneManager = class {
164
168
  });
165
169
  this._transitionState = this._transition.start("out");
166
170
  } else {
167
- this._exitAndDestroyCurrent();
171
+ this._discardPending();
172
+ this._transitionState = null;
173
+ this._teardownStack();
168
174
  const scene = this._getOrCreate(name);
169
175
  await this._activateScene(scene, data);
170
176
  }
@@ -173,8 +179,11 @@ var SceneManager = class {
173
179
  * Push a scene onto the stack. The current scene gets onExit, unless
174
180
  * `options.overlay` is true — then it stays live and is updated and drawn
175
181
  * before the pushed scene. The new scene gets onPreload → onInit → onEnter.
182
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
183
+ * the stack when the fade-out ends; nothing is called and nothing changes.
176
184
  */
177
185
  async push(name, data, options) {
186
+ if (this._refusedByPendingGo()) return;
178
187
  const scene = this._getOrCreate(name);
179
188
  if (!scene) return;
180
189
  const overlay = options?.overlay === true;
@@ -190,8 +199,11 @@ var SceneManager = class {
190
199
  * `options.keep` is true — a kept scene stays initialized so the next `push`
191
200
  * of its name goes straight to onEnter. The scene below gets
192
201
  * onEnter(options.data) unless the popped scene was pushed as an overlay.
202
+ * Ignored while an animated `go()` is still fading out — that `go()` clears
203
+ * the stack when the fade-out ends; nothing is called and nothing changes.
193
204
  */
194
205
  pop(options) {
206
+ if (this._refusedByPendingGo()) return;
195
207
  if (this._stack.length <= 1) return;
196
208
  const top = this._stack.pop();
197
209
  const wasOverlay = this._overlay.pop() === true;
@@ -214,18 +226,11 @@ var SceneManager = class {
214
226
  }
215
227
  /** Destroy all scenes and clear state. */
216
228
  destroy() {
217
- for (let i = this._stack.length - 1; i >= 0; i--) {
218
- const scene = this._stack[i];
219
- scene.onExit?.();
220
- scene.onDestroy?.();
221
- }
222
- this._stack.length = 0;
223
- this._overlay.length = 0;
224
- this._destroyKept();
229
+ this._discardPending();
230
+ this._teardownStack();
225
231
  this._scenes.clear();
226
232
  this._initialized.clear();
227
233
  this._transitionState = null;
228
- this._pendingScene = null;
229
234
  }
230
235
  /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */
231
236
  get transitionProgress() {
@@ -236,6 +241,16 @@ var SceneManager = class {
236
241
  get transitionPhase() {
237
242
  return this._transitionState?.phase ?? null;
238
243
  }
244
+ /** True while an animated `go()` target is pending; warns once per manager in dev. */
245
+ _refusedByPendingGo() {
246
+ const pending = this._pendingScene;
247
+ if (!pending) return false;
248
+ if (!this._warnedPendingGo && typeof __DEV__ !== "undefined" && __DEV__) {
249
+ this._warnedPendingGo = true;
250
+ console.warn(`[SceneManager] push()/pop() ignored while go('${pending.name}') is fading out; call them after the fade-out ends`);
251
+ }
252
+ return true;
253
+ }
239
254
  _getOrCreate(name) {
240
255
  let scene = this._scenes.get(name);
241
256
  if (scene) return scene;
@@ -252,22 +267,13 @@ var SceneManager = class {
252
267
  scene.onInit?.();
253
268
  }
254
269
  async _activateScene(scene, data) {
255
- for (let i = this._stack.length - 1; i >= 0; i--) {
256
- const s = this._stack[i];
257
- s.onExit?.();
258
- s.onDestroy?.();
259
- this._scenes.delete(s.name);
260
- this._initialized.delete(s.name);
261
- }
262
- this._stack.length = 0;
263
- this._overlay.length = 0;
264
- this._destroyKept();
265
270
  await this._initIfNeeded(scene);
266
271
  this._stack.push(scene);
267
272
  this._overlay.push(false);
268
273
  scene.onEnter?.(data);
269
274
  }
270
- _exitAndDestroyCurrent() {
275
+ /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */
276
+ _teardownStack() {
271
277
  for (let i = this._stack.length - 1; i >= 0; i--) {
272
278
  const s = this._stack[i];
273
279
  s.onExit?.();
@@ -295,6 +301,23 @@ var SceneManager = class {
295
301
  }
296
302
  this._kept.clear();
297
303
  }
304
+ /**
305
+ * Destroy the target of an animated `go()` that was superseded before its
306
+ * out-phase finished. It got `onPreload` but never `onInit`. Fields are
307
+ * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;
308
+ * an instance someone else still holds (stack, kept set) or has already
309
+ * destroyed (evicted from the cache) is left alone.
310
+ */
311
+ _discardPending() {
312
+ const pending = this._pendingScene;
313
+ if (!pending) return;
314
+ this._pendingScene = null;
315
+ this._pendingData = void 0;
316
+ if (this._scenes.get(pending.name) !== pending || this._stack.includes(pending) || this._kept.has(pending)) return;
317
+ pending.onDestroy?.();
318
+ this._scenes.delete(pending.name);
319
+ this._initialized.delete(pending.name);
320
+ }
298
321
  _finishOutPhase() {
299
322
  if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
300
323
  const pending = this._pendingScene;
@@ -302,16 +325,7 @@ var SceneManager = class {
302
325
  this._pendingScene = null;
303
326
  this._pendingData = void 0;
304
327
  if (pending) {
305
- for (let i = this._stack.length - 1; i >= 0; i--) {
306
- const s = this._stack[i];
307
- s.onExit?.();
308
- s.onDestroy?.();
309
- this._scenes.delete(s.name);
310
- this._initialized.delete(s.name);
311
- }
312
- this._stack.length = 0;
313
- this._overlay.length = 0;
314
- this._destroyKept();
328
+ this._teardownStack();
315
329
  this._scenes.set(pending.name, pending);
316
330
  this._initialized.add(pending.name);
317
331
  pending.onInit?.();
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\ndeclare const __DEV__: boolean | undefined;\n\nexport interface ScenePushOptions {\n /** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */\n overlay?: boolean;\n}\n\nexport interface ScenePopOptions {\n /** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */\n keep?: boolean;\n /** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */\n data?: unknown;\n}\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)\n * - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)\n * - `update(dt)` — update the live scenes (bottom-up) and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n /** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */\n private _overlay: boolean[] = [];\n /** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */\n private _kept = new Set<Scene>();\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n private _preloadResolved = true;\n private _preloadGen = 0;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n */\n async go(name: string, data?: unknown): Promise<void> {\n if (!this._factories.has(name)) {\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.warn(\n `[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(', ')}`,\n );\n }\n return;\n }\n\n if (this._transition.duration > 0 && this.current) {\n // Animated transition — evict target from cache first so restart\n // builds a fresh instance with a fresh World/Camera2D.\n this._scenes.delete(name);\n this._initialized.delete(name);\n const scene = this._getOrCreate(name)!;\n this._pendingScene = scene;\n this._pendingData = data;\n\n // Start preload concurrently with the fade-out.\n this._initialized.add(scene.name);\n this._preloadResolved = false;\n const gen = ++this._preloadGen;\n const preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n this._finishOutPhase();\n },\n (err) => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.error(\n `[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`,\n err,\n );\n }\n this._finishOutPhase();\n },\n );\n }\n\n this._transitionState = this._transition.start('out');\n } else {\n // Instant transition — destroy first so _getOrCreate builds fresh.\n this._exitAndDestroyCurrent();\n const scene = this._getOrCreate(name)!;\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit, unless\n * `options.overlay` is true — then it stays live and is updated and drawn\n * before the pushed scene. The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n const overlay = options?.overlay === true;\n if (!overlay) this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._kept.delete(scene);\n this._stack.push(scene);\n this._overlay.push(overlay);\n scene.onEnter?.(data);\n }\n\n /**\n * Pop the top scene. Calls onExit on it, then onDestroy unless\n * `options.keep` is true — a kept scene stays initialized so the next `push`\n * of its name goes straight to onEnter. The scene below gets\n * onEnter(options.data) unless the popped scene was pushed as an overlay.\n */\n pop(options?: ScenePopOptions): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n const wasOverlay = this._overlay.pop() === true;\n top.onExit?.();\n if (options?.keep === true) {\n this._kept.add(top);\n } else {\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n }\n\n if (!wasOverlay) this.current?.onEnter?.(options?.data);\n }\n\n /** Update the live scenes (bottom-up, overlays last) and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n this._updateStack(dt);\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const scene = this._stack[i]!;\n scene.onExit?.();\n scene.onDestroy?.();\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n this._pendingScene = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory(new World(), new Camera2D());\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n // Destroy all stacked scenes\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n this._overlay.push(false);\n scene.onEnter?.(data);\n }\n\n private _exitAndDestroyCurrent(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n }\n\n /**\n * Destroy scenes popped with `keep: true` that were never pushed again. They\n * already got `onExit` on the pop. The cache entry is dropped only when it is\n * still this instance — an animated `go()` to the same name has already\n * replaced it with the pending scene.\n */\n private _destroyKept(): void {\n for (const s of this._kept) {\n s.onDestroy?.();\n if (this._scenes.get(s.name) === s) {\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n }\n this._kept.clear();\n }\n\n private _finishOutPhase(): void {\n if (!this._transitionState?.done || this._transitionState.phase !== 'out') return;\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n\n // Preload already ran in go(); call onInit then enter.\n this._scenes.set(pending.name, pending);\n this._initialized.add(pending.name);\n pending.onInit?.();\n this._stack.push(pending);\n this._overlay.push(false);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n }\n\n /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */\n private _updateStack(dt: number): void {\n const stack = this._stack;\n let from = stack.length - 1;\n if (from < 0) return;\n while (from > 0 && this._overlay[from]) from--;\n for (let i = from; i < stack.length; i++) {\n const scene = stack[i]!;\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Hold until preload (started concurrently in go()) resolves.\n if (!this._preloadResolved) {\n this._updateStack(dt);\n return;\n }\n\n this._finishOutPhase();\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the live scenes during transition\n this._updateStack(dt);\n }\n}\n"],"mappings":";;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO;EACL,QAAQ;GACN,MAAM;EACR;EACA,QAAQ,OAAwB,IAA6B;GAC3D,IAAI,MAAM,MAAM,OAAO;GACvB,OAAO,KAAK;GACZ,IAAI,OAAO,GAAG;IACZ,MAAM;IACN,MAAM,WAAW;IACjB,MAAM,OAAO;GACf,OACE,MAAM,WAAW,OAAO,GAAG;GAE7B,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,iBAA6B;CAC3C,OAAO;EACL,MAAM;EACN,UAAU;EACV,MAAM,OAAO;GACX,OAAO;IAAE,UAAU;IAAG,MAAM;IAAM;GAAM;EAC1C;EACA,OAAO,OAAO;GACZ,MAAM,WAAW;GACjB,MAAM,OAAO;GACb,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,eAAe,WAAW,IAAK,SAAmB,UAAsB;CACtF,MAAM,WAAW,aAAa,UAAU,MAAM;CAC9C,OAAO;EACL,MAAM;EACN;EACA,MAAM,OAAO;GACX,SAAS,MAAM;GACf,OAAO;IAAE,UAAU;IAAG,MAAM;IAAO;GAAM;EAC3C;EACA,OAAO,OAAO,IAAI;GAChB,OAAO,SAAS,QAAQ,OAAO,EAAE;EACnC;CACF;AACF;;;;;AAMA,SAAgB,gBACd,WAAW,IACX,YAA4B,QAC5B,SAAmB,UACP;CACZ,MAAM,WAAW,aAAa,UAAU,MAAM;CAC9C,OAAO;EACL,MAAM;EACN;EACA;EACA,MAAM,OAAO;GACX,SAAS,MAAM;GACf,OAAO;IAAE,UAAU;IAAG,MAAM;IAAO;GAAM;EAC3C;EACA,OAAO,OAAO,IAAI;GAChB,OAAO,SAAS,QAAQ,OAAO,EAAE;E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KAAK,OAAO;GACxB,KAAK,SAAS,KAAK,KAAK;GACxB,QAAQ,UAAU,IAAI;EACxB;EAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;CACrD;;CAGA,aAAqB,IAAkB;EACrC,MAAM,QAAQ,KAAK;EACnB,IAAI,OAAO,MAAM,SAAS;EAC1B,IAAI,OAAO,GAAG;EACd,OAAO,OAAO,KAAK,KAAK,SAAS,OAAO;EACxC,KAAK,IAAI,IAAI,MAAM,IAAI,MAAM,QAAQ,KAAK;GACxC,MAAM,QAAQ,MAAM;GACpB,MAAM,WAAW,EAAE;GACnB,MAAM,SAAS;EACjB;CACF;CAEA,kBAA0B,IAAkB;EAC1C,IAAI,CAAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MAAM;GAC9B,IAAI,KAAK,iBAAiB,UAAU,OAAO;IAEzC,IAAI,CAAC,KAAK,kBAAkB;KAC1B,KAAK,aAAa,EAAE;KACpB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,KAAK,aAAa,EAAE;CACtB;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\ndeclare const __DEV__: boolean | undefined;\n\nexport interface ScenePushOptions {\n /** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */\n overlay?: boolean;\n}\n\nexport interface ScenePopOptions {\n /** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */\n keep?: boolean;\n /** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */\n data?: unknown;\n}\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)\n * - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)\n * - `update(dt)` — update the live scenes (bottom-up) and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n /** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */\n private _overlay: boolean[] = [];\n /** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */\n private _kept = new Set<Scene>();\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n private _preloadResolved = true;\n private _preloadGen = 0;\n private _warnedPendingGo = false;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n * A `go()` issued while an animated `go()` is still fading out supersedes\n * it: the superseded target gets `onDestroy` (never `onInit` / `onEnter`).\n */\n async go(name: string, data?: unknown): Promise<void> {\n if (!this._factories.has(name)) {\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.warn(\n `[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(', ')}`,\n );\n }\n return;\n }\n\n if (this._transition.duration > 0 && this.current) {\n this._discardPending();\n // Animated transition — evict target from cache first so restart\n // builds a fresh instance with a fresh World/Camera2D.\n this._scenes.delete(name);\n this._initialized.delete(name);\n const scene = this._getOrCreate(name)!;\n this._pendingScene = scene;\n this._pendingData = data;\n\n // Start preload concurrently with the fade-out.\n this._initialized.add(scene.name);\n this._preloadResolved = false;\n const gen = ++this._preloadGen;\n const preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n this._finishOutPhase();\n },\n (err) => {\n if (gen !== this._preloadGen) return;\n this._preloadResolved = true;\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.error(\n `[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`,\n err,\n );\n }\n this._finishOutPhase();\n },\n );\n }\n\n this._transitionState = this._transition.start('out');\n } else {\n // A pending animated go() is superseded; end any transition in progress\n // too, or _finishOutPhase would later replace the instant scene with the\n // stale target. Its late preload handler is harmless once the state is null.\n this._discardPending();\n this._transitionState = null;\n // Instant transition — destroy first so _getOrCreate builds fresh.\n this._teardownStack();\n const scene = this._getOrCreate(name)!;\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit, unless\n * `options.overlay` is true — then it stays live and is updated and drawn\n * before the pushed scene. The new scene gets onPreload → onInit → onEnter.\n * Ignored while an animated `go()` is still fading out — that `go()` clears\n * the stack when the fade-out ends; nothing is called and nothing changes.\n */\n async push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void> {\n if (this._refusedByPendingGo()) return;\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n const overlay = options?.overlay === true;\n if (!overlay) this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._kept.delete(scene);\n this._stack.push(scene);\n this._overlay.push(overlay);\n scene.onEnter?.(data);\n }\n\n /**\n * Pop the top scene. Calls onExit on it, then onDestroy unless\n * `options.keep` is true — a kept scene stays initialized so the next `push`\n * of its name goes straight to onEnter. The scene below gets\n * onEnter(options.data) unless the popped scene was pushed as an overlay.\n * Ignored while an animated `go()` is still fading out — that `go()` clears\n * the stack when the fade-out ends; nothing is called and nothing changes.\n */\n pop(options?: ScenePopOptions): void {\n if (this._refusedByPendingGo()) return;\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n const wasOverlay = this._overlay.pop() === true;\n top.onExit?.();\n if (options?.keep === true) {\n this._kept.add(top);\n } else {\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n }\n\n if (!wasOverlay) this.current?.onEnter?.(options?.data);\n }\n\n /** Update the live scenes (bottom-up, overlays last) and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n this._updateStack(dt);\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n this._discardPending();\n this._teardownStack();\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n /** True while an animated `go()` target is pending; warns once per manager in dev. */\n private _refusedByPendingGo(): boolean {\n const pending = this._pendingScene;\n if (!pending) return false;\n if (!this._warnedPendingGo && typeof __DEV__ !== 'undefined' && __DEV__) {\n this._warnedPendingGo = true;\n console.warn(\n `[SceneManager] push()/pop() ignored while go('${pending.name}') is fading out; call them after the fade-out ends`,\n );\n }\n return true;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory(new World(), new Camera2D());\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n this._overlay.push(false);\n scene.onEnter?.(data);\n }\n\n /** Exit and destroy every stacked scene, top first, then empty the stack and destroy the kept scenes. */\n private _teardownStack(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n this._overlay.length = 0;\n this._destroyKept();\n }\n\n /**\n * Destroy scenes popped with `keep: true` that were never pushed again. They\n * already got `onExit` on the pop. The cache entry is dropped only when it is\n * still this instance — an animated `go()` to the same name has already\n * replaced it with the pending scene.\n */\n private _destroyKept(): void {\n for (const s of this._kept) {\n s.onDestroy?.();\n if (this._scenes.get(s.name) === s) {\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n }\n this._kept.clear();\n }\n\n /**\n * Destroy the target of an animated `go()` that was superseded before its\n * out-phase finished. It got `onPreload` but never `onInit`. Fields are\n * cleared first so a re-entrant call from `onDestroy` cannot destroy twice;\n * an instance someone else still holds (stack, kept set) or has already\n * destroyed (evicted from the cache) is left alone.\n */\n private _discardPending(): void {\n const pending = this._pendingScene;\n if (!pending) return;\n this._pendingScene = null;\n this._pendingData = undefined;\n if (\n this._scenes.get(pending.name) !== pending ||\n this._stack.includes(pending) ||\n this._kept.has(pending)\n ) {\n return;\n }\n pending.onDestroy?.();\n this._scenes.delete(pending.name);\n this._initialized.delete(pending.name);\n }\n\n private _finishOutPhase(): void {\n if (!this._transitionState?.done || this._transitionState.phase !== 'out') return;\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n this._teardownStack();\n\n // Preload already ran in go(); call onInit then enter.\n this._scenes.set(pending.name, pending);\n this._initialized.add(pending.name);\n pending.onInit?.();\n this._stack.push(pending);\n this._overlay.push(false);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n }\n\n /** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */\n private _updateStack(dt: number): void {\n const stack = this._stack;\n let from = stack.length - 1;\n if (from < 0) return;\n while (from > 0 && this._overlay[from]) from--;\n for (let i = from; i < stack.length; i++) {\n const scene = stack[i]!;\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Hold until preload (started concurrently in go()) resolves.\n if (!this._preloadResolved) {\n this._updateStack(dt);\n return;\n }\n\n this._finishOutPhase();\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the live scenes during transition\n this._updateStack(dt);\n }\n}\n"],"mappings":";;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO;EACL,QAAQ;GACN,MAAM;EACR;EACA,QAAQ,OAAwB,IAA6B;GAC3D,IAAI,MAAM,MAAM,OAAO;GACvB,OAAO,KAAK;GACZ,IAAI,OAAO,GAAG;IACZ,MAAM;IACN,MAAM,WAAW;IACjB,MAAM,OAAO;GACf,OACE,MAAM,WAAW,OA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E5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MAAM;GAC9B,IAAI,KAAK,iBAAiB,UAAU,OAAO;IAEzC,IAAI,CAAC,KAAK,kBAAkB;KAC1B,KAAK,aAAa,EAAE;KACpB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,KAAK,aAAa,EAAE;CACtB;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flare-engine/scene",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "Scene manager, transitions, lifecycle",
5
5
  "keywords": ["flare-engine", "scene", "transitions", "react-native", "typescript"],
6
6
  "license": "Apache-2.0",
@@ -37,9 +37,9 @@
37
37
  "typecheck": "tsc --noEmit"
38
38
  },
39
39
  "dependencies": {
40
- "@flare-engine/ecs": "^0.4.10",
41
- "@flare-engine/math": "^0.4.10",
42
- "@flare-engine/render": "^0.4.10"
40
+ "@flare-engine/ecs": "^0.4.12",
41
+ "@flare-engine/math": "^0.4.12",
42
+ "@flare-engine/render": "^0.4.12"
43
43
  },
44
44
  "engines": {
45
45
  "node": ">=20"