@flare-engine/scene 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -7
- package/dist/index.d.mts +10 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +31 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,15 +12,11 @@ bun add @flare-engine/scene
|
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
14
|
import { SceneManager, fadeTransition } from '@flare-engine/scene';
|
|
15
|
-
import type { World } from '@flare-engine/ecs';
|
|
16
|
-
import type { Camera2D } from '@flare-engine/render';
|
|
17
|
-
|
|
18
|
-
// world and camera are injected by SceneManager after the factory runs
|
|
19
|
-
declare const world: World;
|
|
20
|
-
declare const camera: Camera2D;
|
|
21
15
|
|
|
22
16
|
const manager = new SceneManager();
|
|
23
|
-
|
|
17
|
+
// SceneManager passes the World and Camera2D it created for this scene into
|
|
18
|
+
// the factory — a zero-argument factory would also be a valid SceneFactory.
|
|
19
|
+
manager.register('menu', (world, camera) => ({
|
|
24
20
|
name: 'menu',
|
|
25
21
|
world,
|
|
26
22
|
camera,
|
package/dist/index.d.mts
CHANGED
|
@@ -28,8 +28,14 @@ interface Scene {
|
|
|
28
28
|
/** Called when the scene is permanently removed. Cleanup resources. */
|
|
29
29
|
onDestroy?(): void;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
type
|
|
31
|
+
/**
|
|
32
|
+
* Factory function type for creating scenes. Receives the `World` and
|
|
33
|
+
* `Camera2D` the manager created for this scene instance, so a factory can
|
|
34
|
+
* populate the world it will actually get before returning it as part of the
|
|
35
|
+
* `Scene`. A zero-argument `() => Scene` factory (ignoring both parameters)
|
|
36
|
+
* remains a valid `SceneFactory`.
|
|
37
|
+
*/
|
|
38
|
+
type SceneFactory = (world: World, camera: Camera2D) => Scene;
|
|
33
39
|
//#endregion
|
|
34
40
|
//#region src/transition.d.ts
|
|
35
41
|
/** Slide direction — where the outgoing scene moves off-screen to. */
|
|
@@ -86,6 +92,7 @@ declare class SceneManager {
|
|
|
86
92
|
private _pendingScene;
|
|
87
93
|
private _pendingData;
|
|
88
94
|
private _preloadResolved;
|
|
95
|
+
private _preloadGen;
|
|
89
96
|
/** The currently active scene (top of stack). */
|
|
90
97
|
get current(): Scene | undefined;
|
|
91
98
|
/** Number of scenes on the stack. */
|
|
@@ -121,6 +128,7 @@ declare class SceneManager {
|
|
|
121
128
|
private _initIfNeeded;
|
|
122
129
|
private _activateScene;
|
|
123
130
|
private _exitAndDestroyCurrent;
|
|
131
|
+
private _finishOutPhase;
|
|
124
132
|
private _updateTransition;
|
|
125
133
|
}
|
|
126
134
|
//#endregion
|
package/dist/index.d.mts.map
CHANGED
|
@@ -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
|
|
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;;;;KCzCnD;;UAGK;;EAEf;;EAEA;;EAEA;;;UAIe;;WAEN;;WAGA;;WAGA,YAAY;;EAGrB,MAAM,sBAAsB;;EAG5B,OAAO,OAAO,iBAAiB,aAAa;;;iBAgC9B,kBAAkB;;iBAgBlB,eAAe,mBAAgB,SAAQ,WAAsB;;;;;iBAmB7D,gBACd,mBACA,YAAW,gBACX,SAAQ,WACP;;;;;;;;;;;;cCtFU;UACH;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;MAGJ,WAAW;;MAKX;;MAKA;;EAKJ,cAAc,YAAY;;EAK1B,SAAS,cAAc,SAAS;;;;;;EAS1B,GAAG,cAAc,iBAAiB;;;;;EA4DlC,KAAK,cAAc,iBAAiB;;EAW1C;;EAaA,OAAO;;EAcP;;MAcI;;MAMA;UAII;UAYM;UAOA;UAgBN;UAWA;UA6BA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -28,8 +28,14 @@ interface Scene {
|
|
|
28
28
|
/** Called when the scene is permanently removed. Cleanup resources. */
|
|
29
29
|
onDestroy?(): void;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
type
|
|
31
|
+
/**
|
|
32
|
+
* Factory function type for creating scenes. Receives the `World` and
|
|
33
|
+
* `Camera2D` the manager created for this scene instance, so a factory can
|
|
34
|
+
* populate the world it will actually get before returning it as part of the
|
|
35
|
+
* `Scene`. A zero-argument `() => Scene` factory (ignoring both parameters)
|
|
36
|
+
* remains a valid `SceneFactory`.
|
|
37
|
+
*/
|
|
38
|
+
type SceneFactory = (world: World, camera: Camera2D) => Scene;
|
|
33
39
|
//#endregion
|
|
34
40
|
//#region src/transition.d.ts
|
|
35
41
|
/** Slide direction — where the outgoing scene moves off-screen to. */
|
|
@@ -86,6 +92,7 @@ declare class SceneManager {
|
|
|
86
92
|
private _pendingScene;
|
|
87
93
|
private _pendingData;
|
|
88
94
|
private _preloadResolved;
|
|
95
|
+
private _preloadGen;
|
|
89
96
|
/** The currently active scene (top of stack). */
|
|
90
97
|
get current(): Scene | undefined;
|
|
91
98
|
/** Number of scenes on the stack. */
|
|
@@ -121,6 +128,7 @@ declare class SceneManager {
|
|
|
121
128
|
private _initIfNeeded;
|
|
122
129
|
private _activateScene;
|
|
123
130
|
private _exitAndDestroyCurrent;
|
|
131
|
+
private _finishOutPhase;
|
|
124
132
|
private _updateTransition;
|
|
125
133
|
}
|
|
126
134
|
//#endregion
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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
|
|
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;;;iBAgC9B,kBAAkB;;iBAgBlB,eAAe,mBAAgB,SAAQ,WAAsB;;;;;iBAmB7D,gBACd,mBACA,YAAW,gBACX,SAAQ,WACP;;;;;;;;;;;;cCtFU;UACH;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;MAGJ,WAAW;;MAKX;;MAKA;;EAKJ,cAAc,YAAY;;EAK1B,SAAS,cAAc,SAAS;;;;;;EAS1B,GAAG,cAAc,iBAAiB;;;;;EA4DlC,KAAK,cAAc,iBAAiB;;EAW1C;;EAaA,OAAO;;EAcP;;MAcI;;MAMA;UAII;UAYM;UAOA;UAgBN;UAWA;UA6BA"}
|
package/dist/index.js
CHANGED
|
@@ -108,6 +108,7 @@ var SceneManager = class {
|
|
|
108
108
|
_pendingScene = null;
|
|
109
109
|
_pendingData = void 0;
|
|
110
110
|
_preloadResolved = true;
|
|
111
|
+
_preloadGen = 0;
|
|
111
112
|
/** The currently active scene (top of stack). */
|
|
112
113
|
get current() {
|
|
113
114
|
return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
|
|
@@ -146,13 +147,18 @@ var SceneManager = class {
|
|
|
146
147
|
this._pendingData = data;
|
|
147
148
|
this._initialized.add(scene.name);
|
|
148
149
|
this._preloadResolved = false;
|
|
150
|
+
const gen = ++this._preloadGen;
|
|
149
151
|
const preload = scene.onPreload?.();
|
|
150
152
|
if (preload == null) this._preloadResolved = true;
|
|
151
153
|
else preload.then(() => {
|
|
154
|
+
if (gen !== this._preloadGen) return;
|
|
152
155
|
this._preloadResolved = true;
|
|
156
|
+
this._finishOutPhase();
|
|
153
157
|
}, (err) => {
|
|
158
|
+
if (gen !== this._preloadGen) return;
|
|
154
159
|
this._preloadResolved = true;
|
|
155
160
|
if (typeof __DEV__ !== "undefined" && __DEV__) console.error(`[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`, err);
|
|
161
|
+
this._finishOutPhase();
|
|
156
162
|
});
|
|
157
163
|
this._transitionState = this._transition.start("out");
|
|
158
164
|
} else {
|
|
@@ -222,9 +228,7 @@ var SceneManager = class {
|
|
|
222
228
|
if (scene) return scene;
|
|
223
229
|
const factory = this._factories.get(name);
|
|
224
230
|
if (!factory) return null;
|
|
225
|
-
scene = factory();
|
|
226
|
-
scene.world = new _flare_engine_ecs.World();
|
|
227
|
-
scene.camera = new _flare_engine_render.Camera2D();
|
|
231
|
+
scene = factory(new _flare_engine_ecs.World(), new _flare_engine_render.Camera2D());
|
|
228
232
|
this._scenes.set(name, scene);
|
|
229
233
|
return scene;
|
|
230
234
|
}
|
|
@@ -257,6 +261,29 @@ var SceneManager = class {
|
|
|
257
261
|
}
|
|
258
262
|
this._stack.length = 0;
|
|
259
263
|
}
|
|
264
|
+
_finishOutPhase() {
|
|
265
|
+
if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
|
|
266
|
+
const pending = this._pendingScene;
|
|
267
|
+
const data = this._pendingData;
|
|
268
|
+
this._pendingScene = null;
|
|
269
|
+
this._pendingData = void 0;
|
|
270
|
+
if (pending) {
|
|
271
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
272
|
+
const s = this._stack[i];
|
|
273
|
+
s.onExit?.();
|
|
274
|
+
s.onDestroy?.();
|
|
275
|
+
this._scenes.delete(s.name);
|
|
276
|
+
this._initialized.delete(s.name);
|
|
277
|
+
}
|
|
278
|
+
this._stack.length = 0;
|
|
279
|
+
this._scenes.set(pending.name, pending);
|
|
280
|
+
this._initialized.add(pending.name);
|
|
281
|
+
pending.onInit?.();
|
|
282
|
+
this._stack.push(pending);
|
|
283
|
+
pending.onEnter?.(data);
|
|
284
|
+
}
|
|
285
|
+
this._transitionState = this._transition.start("in");
|
|
286
|
+
}
|
|
260
287
|
_updateTransition(dt) {
|
|
261
288
|
if (!this._transitionState) return;
|
|
262
289
|
this._transition.update(this._transitionState, dt);
|
|
@@ -266,26 +293,7 @@ var SceneManager = class {
|
|
|
266
293
|
this.current?.onDraw?.();
|
|
267
294
|
return;
|
|
268
295
|
}
|
|
269
|
-
|
|
270
|
-
const data = this._pendingData;
|
|
271
|
-
this._pendingScene = null;
|
|
272
|
-
this._pendingData = void 0;
|
|
273
|
-
if (pending) {
|
|
274
|
-
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
275
|
-
const s = this._stack[i];
|
|
276
|
-
s.onExit?.();
|
|
277
|
-
s.onDestroy?.();
|
|
278
|
-
this._scenes.delete(s.name);
|
|
279
|
-
this._initialized.delete(s.name);
|
|
280
|
-
}
|
|
281
|
-
this._stack.length = 0;
|
|
282
|
-
this._scenes.set(pending.name, pending);
|
|
283
|
-
this._initialized.add(pending.name);
|
|
284
|
-
pending.onInit?.();
|
|
285
|
-
this._stack.push(pending);
|
|
286
|
-
pending.onEnter?.(data);
|
|
287
|
-
}
|
|
288
|
-
this._transitionState = this._transition.start("in");
|
|
296
|
+
this._finishOutPhase();
|
|
289
297
|
} else this._transitionState = null;
|
|
290
298
|
this.current?.onUpdate?.(dt);
|
|
291
299
|
this.current?.onDraw?.();
|
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\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?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene 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 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\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 preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n this._preloadResolved = true;\n },\n (err) => {\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 },\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.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\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._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();\n // Inject World and Camera\n scene.world = new World();\n scene.camera = 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\n await this._initIfNeeded(scene);\n this._stack.push(scene);\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 }\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.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n return;\n }\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n // Destroy current stack\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\n // Preload already awaited 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 pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\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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S;IAGrB,KAAK,QAAQ,IAAI,QAAQ,MAAM,OAAO;IACtC,KAAK,aAAa,IAAI,QAAQ,IAAI;IAClC,QAAQ,SAAS;IACjB,KAAK,OAAO,KAAK,OAAO;IACxB,QAAQ,UAAU,IAAI;GACxB;GAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;EACrD,OAEE,KAAK,mBAAmB;EAK5B,KAAK,SAAS,WAAW,EAAE;EAC3B,KAAK,SAAS,SAAS;CACzB;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\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?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene 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 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.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\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._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\n await this._initIfNeeded(scene);\n this._stack.push(scene);\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 }\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\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 pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\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.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\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 current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n }\n}\n"],"mappings":";;;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO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OAAO,SAAS,GAAG,KAAK,GAAG,KAAK;IAChD,MAAM,IAAI,KAAK,OAAO;IACtB,EAAE,SAAS;IACX,EAAE,YAAY;IACd,KAAK,QAAQ,OAAO,EAAE,IAAI;IAC1B,KAAK,aAAa,OAAO,EAAE,IAAI;GACjC;GACA,KAAK,OAAO,SAAS;GAGrB,KAAK,QAAQ,IAAI,QAAQ,MAAM,OAAO;GACtC,KAAK,aAAa,IAAI,QAAQ,IAAI;GAClC,QAAQ,SAAS;GACjB,KAAK,OAAO,KAAK,OAAO;GACxB,QAAQ,UAAU,IAAI;EACxB;EAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;CACrD;CAEA,kBAA0B,IAAkB;EAC1C,IAAI,CAAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MACxB,IAAI,KAAK,iBAAiB,UAAU,OAAO;GAEzC,IAAI,CAAC,KAAK,kBAAkB;IAC1B,KAAK,SAAS,WAAW,EAAE;IAC3B,KAAK,SAAS,SAAS;IACvB;GACF;GAEA,KAAK,gBAAgB;EACvB,OAEE,KAAK,mBAAmB;EAK5B,KAAK,SAAS,WAAW,EAAE;EAC3B,KAAK,SAAS,SAAS;CACzB;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -107,6 +107,7 @@ var SceneManager = class {
|
|
|
107
107
|
_pendingScene = null;
|
|
108
108
|
_pendingData = void 0;
|
|
109
109
|
_preloadResolved = true;
|
|
110
|
+
_preloadGen = 0;
|
|
110
111
|
/** The currently active scene (top of stack). */
|
|
111
112
|
get current() {
|
|
112
113
|
return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
|
|
@@ -145,13 +146,18 @@ var SceneManager = class {
|
|
|
145
146
|
this._pendingData = data;
|
|
146
147
|
this._initialized.add(scene.name);
|
|
147
148
|
this._preloadResolved = false;
|
|
149
|
+
const gen = ++this._preloadGen;
|
|
148
150
|
const preload = scene.onPreload?.();
|
|
149
151
|
if (preload == null) this._preloadResolved = true;
|
|
150
152
|
else preload.then(() => {
|
|
153
|
+
if (gen !== this._preloadGen) return;
|
|
151
154
|
this._preloadResolved = true;
|
|
155
|
+
this._finishOutPhase();
|
|
152
156
|
}, (err) => {
|
|
157
|
+
if (gen !== this._preloadGen) return;
|
|
153
158
|
this._preloadResolved = true;
|
|
154
159
|
if (typeof __DEV__ !== "undefined" && __DEV__) console.error(`[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`, err);
|
|
160
|
+
this._finishOutPhase();
|
|
155
161
|
});
|
|
156
162
|
this._transitionState = this._transition.start("out");
|
|
157
163
|
} else {
|
|
@@ -221,9 +227,7 @@ var SceneManager = class {
|
|
|
221
227
|
if (scene) return scene;
|
|
222
228
|
const factory = this._factories.get(name);
|
|
223
229
|
if (!factory) return null;
|
|
224
|
-
scene = factory();
|
|
225
|
-
scene.world = new World();
|
|
226
|
-
scene.camera = new Camera2D();
|
|
230
|
+
scene = factory(new World(), new Camera2D());
|
|
227
231
|
this._scenes.set(name, scene);
|
|
228
232
|
return scene;
|
|
229
233
|
}
|
|
@@ -256,6 +260,29 @@ var SceneManager = class {
|
|
|
256
260
|
}
|
|
257
261
|
this._stack.length = 0;
|
|
258
262
|
}
|
|
263
|
+
_finishOutPhase() {
|
|
264
|
+
if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
|
|
265
|
+
const pending = this._pendingScene;
|
|
266
|
+
const data = this._pendingData;
|
|
267
|
+
this._pendingScene = null;
|
|
268
|
+
this._pendingData = void 0;
|
|
269
|
+
if (pending) {
|
|
270
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
271
|
+
const s = this._stack[i];
|
|
272
|
+
s.onExit?.();
|
|
273
|
+
s.onDestroy?.();
|
|
274
|
+
this._scenes.delete(s.name);
|
|
275
|
+
this._initialized.delete(s.name);
|
|
276
|
+
}
|
|
277
|
+
this._stack.length = 0;
|
|
278
|
+
this._scenes.set(pending.name, pending);
|
|
279
|
+
this._initialized.add(pending.name);
|
|
280
|
+
pending.onInit?.();
|
|
281
|
+
this._stack.push(pending);
|
|
282
|
+
pending.onEnter?.(data);
|
|
283
|
+
}
|
|
284
|
+
this._transitionState = this._transition.start("in");
|
|
285
|
+
}
|
|
259
286
|
_updateTransition(dt) {
|
|
260
287
|
if (!this._transitionState) return;
|
|
261
288
|
this._transition.update(this._transitionState, dt);
|
|
@@ -265,26 +292,7 @@ var SceneManager = class {
|
|
|
265
292
|
this.current?.onDraw?.();
|
|
266
293
|
return;
|
|
267
294
|
}
|
|
268
|
-
|
|
269
|
-
const data = this._pendingData;
|
|
270
|
-
this._pendingScene = null;
|
|
271
|
-
this._pendingData = void 0;
|
|
272
|
-
if (pending) {
|
|
273
|
-
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
274
|
-
const s = this._stack[i];
|
|
275
|
-
s.onExit?.();
|
|
276
|
-
s.onDestroy?.();
|
|
277
|
-
this._scenes.delete(s.name);
|
|
278
|
-
this._initialized.delete(s.name);
|
|
279
|
-
}
|
|
280
|
-
this._stack.length = 0;
|
|
281
|
-
this._scenes.set(pending.name, pending);
|
|
282
|
-
this._initialized.add(pending.name);
|
|
283
|
-
pending.onInit?.();
|
|
284
|
-
this._stack.push(pending);
|
|
285
|
-
pending.onEnter?.(data);
|
|
286
|
-
}
|
|
287
|
-
this._transitionState = this._transition.start("in");
|
|
295
|
+
this._finishOutPhase();
|
|
288
296
|
} else this._transitionState = null;
|
|
289
297
|
this.current?.onUpdate?.(dt);
|
|
290
298
|
this.current?.onDraw?.();
|
package/dist/index.mjs.map
CHANGED
|
@@ -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\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?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene 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 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\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 preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n this._preloadResolved = true;\n },\n (err) => {\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 },\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.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\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._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();\n // Inject World and Camera\n scene.world = new World();\n scene.camera = 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\n await this._initIfNeeded(scene);\n this._stack.push(scene);\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 }\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.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n return;\n }\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n // Destroy current stack\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\n // Preload already awaited 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 pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\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,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AG,KAAK,GAAG,KAAK;GAChD,MAAM,IAAI,KAAK,OAAO;GACtB,EAAE,SAAS;GACX,EAAE,YAAY;GACd,KAAK,QAAQ,OAAO,EAAE,IAAI;GAC1B,KAAK,aAAa,OAAO,EAAE,IAAI;EACjC;EACA,KAAK,OAAO,SAAS;CACvB;CAEA,kBAA0B,IAAkB;EAC1C,IAAI,CAAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MACxB,IAAI,KAAK,iBAAiB,UAAU,OAAO;GAEzC,IAAI,CAAC,KAAK,kBAAkB;IAC1B,KAAK,SAAS,WAAW,EAAE;IAC3B,KAAK,SAAS,SAAS;IACvB;GACF;GAEA,MAAM,UAAU,KAAK;GACrB,MAAM,OAAO,KAAK;GAClB,KAAK,gBAAgB;GACrB,KAAK,eAAe,KAAA;GAEpB,IAAI,SAAS;IAEX,KAAK,IAAI,IAAI,KAAK,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK;KAChD,MAAM,IAAI,KAAK,OAAO;KACtB,EAAE,SAAS;KACX,EAAE,YAAY;KACd,KAAK,QAAQ,OAAO,EAAE,IAAI;KAC1B,KAAK,aAAa,OAAO,EAAE,IAAI;IACjC;IACA,KAAK,OAAO,SAAS;IAGrB,KAAK,QAAQ,IAAI,QAAQ,MAAM,OAAO;IACtC,KAAK,aAAa,IAAI,QAAQ,IAAI;IAClC,QAAQ,SAAS;IACjB,KAAK,OAAO,KAAK,OAAO;IACxB,QAAQ,UAAU,IAAI;GACxB;GAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;EACrD,OAEE,KAAK,mBAAmB;EAK5B,KAAK,SAAS,WAAW,EAAE;EAC3B,KAAK,SAAS,SAAS;CACzB;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\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?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene 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 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.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\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._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\n await this._initIfNeeded(scene);\n this._stack.push(scene);\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 }\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\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 pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\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.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\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 current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\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;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AAC,KAAK,kBAAkB;EAE5B,KAAK,YAAY,OAAO,KAAK,kBAAkB,EAAE;EAEjD,IAAI,KAAK,iBAAiB,MACxB,IAAI,KAAK,iBAAiB,UAAU,OAAO;GAEzC,IAAI,CAAC,KAAK,kBAAkB;IAC1B,KAAK,SAAS,WAAW,EAAE;IAC3B,KAAK,SAAS,SAAS;IACvB;GACF;GAEA,KAAK,gBAAgB;EACvB,OAEE,KAAK,mBAAmB;EAK5B,KAAK,SAAS,WAAW,EAAE;EAC3B,KAAK,SAAS,SAAS;CACzB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/scene",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Scene manager, transitions, lifecycle",
|
|
5
5
|
"keywords": ["flare-engine", "scene", "transitions"],
|
|
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.
|
|
41
|
-
"@flare-engine/math": "^0.
|
|
42
|
-
"@flare-engine/render": "^0.
|
|
40
|
+
"@flare-engine/ecs": "^0.4.0",
|
|
41
|
+
"@flare-engine/math": "^0.4.0",
|
|
42
|
+
"@flare-engine/render": "^0.4.0"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=20"
|