@flare-engine/scene 0.4.8 → 0.4.10
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 +1 -1
- package/dist/index.d.mts +39 -10
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +39 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +72 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +72 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @flare-engine/scene
|
|
2
2
|
|
|
3
|
-
Scene lifecycle + transitions — `SceneManager` with `register` / `go(name, data?)` / `push` / `pop
|
|
3
|
+
Scene lifecycle + transitions — `SceneManager` with `register` / `go(name, data?)` / `push(name, data?, { overlay? })` / `pop` (an overlay keeps the scene below live, drawn beneath it), `Scene` interface (`onPreload` / `onInit` / `onEnter` / `onUpdate` / `onDraw` / `onExit` / `onDestroy`), and three transition factories (`noneTransition`, `fadeTransition`, `slideTransition`).
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
package/dist/index.d.mts
CHANGED
|
@@ -68,20 +68,34 @@ export declare function fadeTransition(duration?: number, easing?: EasingFn): Tr
|
|
|
68
68
|
* Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).
|
|
69
69
|
*/
|
|
70
70
|
export declare function slideTransition(duration?: number, direction?: SlideDirection, easing?: EasingFn): Transition;
|
|
71
|
+
interface ScenePushOptions {
|
|
72
|
+
/** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */
|
|
73
|
+
overlay?: boolean;
|
|
74
|
+
}
|
|
75
|
+
interface ScenePopOptions {
|
|
76
|
+
/** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */
|
|
77
|
+
keep?: boolean;
|
|
78
|
+
/** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */
|
|
79
|
+
data?: unknown;
|
|
80
|
+
}
|
|
71
81
|
/**
|
|
72
82
|
* SceneManager — manages scene registration, navigation, and a scene stack.
|
|
73
83
|
*
|
|
74
84
|
* - `register(name, factory)` — register a scene factory
|
|
75
85
|
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
76
|
-
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
77
|
-
* - `pop()` — pop the top scene and resume the one below
|
|
78
|
-
* - `update(dt)` — update the
|
|
86
|
+
* - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)
|
|
87
|
+
* - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)
|
|
88
|
+
* - `update(dt)` — update the live scenes (bottom-up) and any active transition
|
|
79
89
|
*/
|
|
80
90
|
export declare class SceneManager {
|
|
81
91
|
private _factories;
|
|
82
92
|
private _scenes;
|
|
83
93
|
private _initialized;
|
|
84
94
|
private _stack;
|
|
95
|
+
/** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */
|
|
96
|
+
private _overlay;
|
|
97
|
+
/** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */
|
|
98
|
+
private _kept;
|
|
85
99
|
private _transition;
|
|
86
100
|
private _transitionState;
|
|
87
101
|
private _pendingScene;
|
|
@@ -105,13 +119,19 @@ export declare class SceneManager {
|
|
|
105
119
|
*/
|
|
106
120
|
go(name: string, data?: unknown): Promise<void>;
|
|
107
121
|
/**
|
|
108
|
-
* Push a scene onto the stack. The current scene gets onExit
|
|
109
|
-
*
|
|
122
|
+
* Push a scene onto the stack. The current scene gets onExit, unless
|
|
123
|
+
* `options.overlay` is true — then it stays live and is updated and drawn
|
|
124
|
+
* before the pushed scene. The new scene gets onPreload → onInit → onEnter.
|
|
125
|
+
*/
|
|
126
|
+
push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Pop the top scene. Calls onExit on it, then onDestroy unless
|
|
129
|
+
* `options.keep` is true — a kept scene stays initialized so the next `push`
|
|
130
|
+
* of its name goes straight to onEnter. The scene below gets
|
|
131
|
+
* onEnter(options.data) unless the popped scene was pushed as an overlay.
|
|
110
132
|
*/
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
pop(): void;
|
|
114
|
-
/** Update the active scene and any active transition. */
|
|
133
|
+
pop(options?: ScenePopOptions): void;
|
|
134
|
+
/** Update the live scenes (bottom-up, overlays last) and any active transition. */
|
|
115
135
|
update(dt: number): void;
|
|
116
136
|
/** Destroy all scenes and clear state. */
|
|
117
137
|
destroy(): void;
|
|
@@ -123,8 +143,17 @@ export declare class SceneManager {
|
|
|
123
143
|
private _initIfNeeded;
|
|
124
144
|
private _activateScene;
|
|
125
145
|
private _exitAndDestroyCurrent;
|
|
146
|
+
/**
|
|
147
|
+
* Destroy scenes popped with `keep: true` that were never pushed again. They
|
|
148
|
+
* already got `onExit` on the pop. The cache entry is dropped only when it is
|
|
149
|
+
* still this instance — an animated `go()` to the same name has already
|
|
150
|
+
* replaced it with the pending scene.
|
|
151
|
+
*/
|
|
152
|
+
private _destroyKept;
|
|
126
153
|
private _finishOutPhase;
|
|
154
|
+
/** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
|
|
155
|
+
private _updateStack;
|
|
127
156
|
private _updateTransition;
|
|
128
157
|
}
|
|
129
|
-
export type { Scene, SceneFactory, SlideDirection, Transition, TransitionState };
|
|
158
|
+
export type { Scene, SceneFactory, ScenePopOptions, ScenePushOptions, SlideDirection, Transition, TransitionState };
|
|
130
159
|
//# sourceMappingURL=index.d.mts.map
|
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;;EA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
CHANGED
|
@@ -68,20 +68,34 @@ export declare function fadeTransition(duration?: number, easing?: EasingFn): Tr
|
|
|
68
68
|
* Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).
|
|
69
69
|
*/
|
|
70
70
|
export declare function slideTransition(duration?: number, direction?: SlideDirection, easing?: EasingFn): Transition;
|
|
71
|
+
interface ScenePushOptions {
|
|
72
|
+
/** Keep the scene below live: it is updated and drawn first, gets no `onExit` on push and no `onEnter` on the matching `pop()`. */
|
|
73
|
+
overlay?: boolean;
|
|
74
|
+
}
|
|
75
|
+
interface ScenePopOptions {
|
|
76
|
+
/** Keep the popped scene initialized: it gets `onExit` only, and the next `push` of its name goes straight to `onEnter`. */
|
|
77
|
+
keep?: boolean;
|
|
78
|
+
/** Forwarded to the revealed scene's `onEnter` (dropped when the popped scene was an overlay). */
|
|
79
|
+
data?: unknown;
|
|
80
|
+
}
|
|
71
81
|
/**
|
|
72
82
|
* SceneManager — manages scene registration, navigation, and a scene stack.
|
|
73
83
|
*
|
|
74
84
|
* - `register(name, factory)` — register a scene factory
|
|
75
85
|
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
76
|
-
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
77
|
-
* - `pop()` — pop the top scene and resume the one below
|
|
78
|
-
* - `update(dt)` — update the
|
|
86
|
+
* - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)
|
|
87
|
+
* - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)
|
|
88
|
+
* - `update(dt)` — update the live scenes (bottom-up) and any active transition
|
|
79
89
|
*/
|
|
80
90
|
export declare class SceneManager {
|
|
81
91
|
private _factories;
|
|
82
92
|
private _scenes;
|
|
83
93
|
private _initialized;
|
|
84
94
|
private _stack;
|
|
95
|
+
/** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */
|
|
96
|
+
private _overlay;
|
|
97
|
+
/** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */
|
|
98
|
+
private _kept;
|
|
85
99
|
private _transition;
|
|
86
100
|
private _transitionState;
|
|
87
101
|
private _pendingScene;
|
|
@@ -105,13 +119,19 @@ export declare class SceneManager {
|
|
|
105
119
|
*/
|
|
106
120
|
go(name: string, data?: unknown): Promise<void>;
|
|
107
121
|
/**
|
|
108
|
-
* Push a scene onto the stack. The current scene gets onExit
|
|
109
|
-
*
|
|
122
|
+
* Push a scene onto the stack. The current scene gets onExit, unless
|
|
123
|
+
* `options.overlay` is true — then it stays live and is updated and drawn
|
|
124
|
+
* before the pushed scene. The new scene gets onPreload → onInit → onEnter.
|
|
125
|
+
*/
|
|
126
|
+
push(name: string, data?: unknown, options?: ScenePushOptions): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Pop the top scene. Calls onExit on it, then onDestroy unless
|
|
129
|
+
* `options.keep` is true — a kept scene stays initialized so the next `push`
|
|
130
|
+
* of its name goes straight to onEnter. The scene below gets
|
|
131
|
+
* onEnter(options.data) unless the popped scene was pushed as an overlay.
|
|
110
132
|
*/
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
pop(): void;
|
|
114
|
-
/** Update the active scene and any active transition. */
|
|
133
|
+
pop(options?: ScenePopOptions): void;
|
|
134
|
+
/** Update the live scenes (bottom-up, overlays last) and any active transition. */
|
|
115
135
|
update(dt: number): void;
|
|
116
136
|
/** Destroy all scenes and clear state. */
|
|
117
137
|
destroy(): void;
|
|
@@ -123,8 +143,17 @@ export declare class SceneManager {
|
|
|
123
143
|
private _initIfNeeded;
|
|
124
144
|
private _activateScene;
|
|
125
145
|
private _exitAndDestroyCurrent;
|
|
146
|
+
/**
|
|
147
|
+
* Destroy scenes popped with `keep: true` that were never pushed again. They
|
|
148
|
+
* already got `onExit` on the pop. The cache entry is dropped only when it is
|
|
149
|
+
* still this instance — an animated `go()` to the same name has already
|
|
150
|
+
* replaced it with the pending scene.
|
|
151
|
+
*/
|
|
152
|
+
private _destroyKept;
|
|
126
153
|
private _finishOutPhase;
|
|
154
|
+
/** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
|
|
155
|
+
private _updateStack;
|
|
127
156
|
private _updateTransition;
|
|
128
157
|
}
|
|
129
|
-
export type { Scene, SceneFactory, SlideDirection, Transition, TransitionState };
|
|
158
|
+
export type { Scene, SceneFactory, ScenePopOptions, ScenePushOptions, SlideDirection, Transition, TransitionState };
|
|
130
159
|
//# sourceMappingURL=index.d.ts.map
|
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;;;;;;;;;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
|
|
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"}
|
package/dist/index.js
CHANGED
|
@@ -93,15 +93,19 @@ function slideTransition(duration = .3, direction = "left", easing = identity) {
|
|
|
93
93
|
*
|
|
94
94
|
* - `register(name, factory)` — register a scene factory
|
|
95
95
|
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
96
|
-
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
97
|
-
* - `pop()` — pop the top scene and resume the one below
|
|
98
|
-
* - `update(dt)` — update the
|
|
96
|
+
* - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)
|
|
97
|
+
* - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)
|
|
98
|
+
* - `update(dt)` — update the live scenes (bottom-up) and any active transition
|
|
99
99
|
*/
|
|
100
100
|
var SceneManager = class {
|
|
101
101
|
_factories = /* @__PURE__ */ new Map();
|
|
102
102
|
_scenes = /* @__PURE__ */ new Map();
|
|
103
103
|
_initialized = /* @__PURE__ */ new Set();
|
|
104
104
|
_stack = [];
|
|
105
|
+
/** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */
|
|
106
|
+
_overlay = [];
|
|
107
|
+
/** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */
|
|
108
|
+
_kept = /* @__PURE__ */ new Set();
|
|
105
109
|
_transition = noneTransition();
|
|
106
110
|
_transitionState = null;
|
|
107
111
|
_pendingScene = null;
|
|
@@ -167,38 +171,47 @@ var SceneManager = class {
|
|
|
167
171
|
}
|
|
168
172
|
}
|
|
169
173
|
/**
|
|
170
|
-
* Push a scene onto the stack. The current scene gets onExit
|
|
171
|
-
*
|
|
174
|
+
* Push a scene onto the stack. The current scene gets onExit, unless
|
|
175
|
+
* `options.overlay` is true — then it stays live and is updated and drawn
|
|
176
|
+
* before the pushed scene. The new scene gets onPreload → onInit → onEnter.
|
|
172
177
|
*/
|
|
173
|
-
async push(name, data) {
|
|
178
|
+
async push(name, data, options) {
|
|
174
179
|
const scene = this._getOrCreate(name);
|
|
175
180
|
if (!scene) return;
|
|
176
|
-
|
|
181
|
+
const overlay = options?.overlay === true;
|
|
182
|
+
if (!overlay) this.current?.onExit?.();
|
|
177
183
|
await this._initIfNeeded(scene);
|
|
184
|
+
this._kept.delete(scene);
|
|
178
185
|
this._stack.push(scene);
|
|
186
|
+
this._overlay.push(overlay);
|
|
179
187
|
scene.onEnter?.(data);
|
|
180
188
|
}
|
|
181
|
-
/**
|
|
182
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Pop the top scene. Calls onExit on it, then onDestroy unless
|
|
191
|
+
* `options.keep` is true — a kept scene stays initialized so the next `push`
|
|
192
|
+
* of its name goes straight to onEnter. The scene below gets
|
|
193
|
+
* onEnter(options.data) unless the popped scene was pushed as an overlay.
|
|
194
|
+
*/
|
|
195
|
+
pop(options) {
|
|
183
196
|
if (this._stack.length <= 1) return;
|
|
184
197
|
const top = this._stack.pop();
|
|
198
|
+
const wasOverlay = this._overlay.pop() === true;
|
|
185
199
|
top.onExit?.();
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
200
|
+
if (options?.keep === true) this._kept.add(top);
|
|
201
|
+
else {
|
|
202
|
+
top.onDestroy?.();
|
|
203
|
+
this._scenes.delete(top.name);
|
|
204
|
+
this._initialized.delete(top.name);
|
|
205
|
+
}
|
|
206
|
+
if (!wasOverlay) this.current?.onEnter?.(options?.data);
|
|
190
207
|
}
|
|
191
|
-
/** Update the
|
|
208
|
+
/** Update the live scenes (bottom-up, overlays last) and any active transition. */
|
|
192
209
|
update(dt) {
|
|
193
210
|
if (this._transitionState) {
|
|
194
211
|
this._updateTransition(dt);
|
|
195
212
|
return;
|
|
196
213
|
}
|
|
197
|
-
|
|
198
|
-
if (scene) {
|
|
199
|
-
scene.onUpdate?.(dt);
|
|
200
|
-
scene.onDraw?.();
|
|
201
|
-
}
|
|
214
|
+
this._updateStack(dt);
|
|
202
215
|
}
|
|
203
216
|
/** Destroy all scenes and clear state. */
|
|
204
217
|
destroy() {
|
|
@@ -208,6 +221,8 @@ var SceneManager = class {
|
|
|
208
221
|
scene.onDestroy?.();
|
|
209
222
|
}
|
|
210
223
|
this._stack.length = 0;
|
|
224
|
+
this._overlay.length = 0;
|
|
225
|
+
this._destroyKept();
|
|
211
226
|
this._scenes.clear();
|
|
212
227
|
this._initialized.clear();
|
|
213
228
|
this._transitionState = null;
|
|
@@ -246,8 +261,11 @@ var SceneManager = class {
|
|
|
246
261
|
this._initialized.delete(s.name);
|
|
247
262
|
}
|
|
248
263
|
this._stack.length = 0;
|
|
264
|
+
this._overlay.length = 0;
|
|
265
|
+
this._destroyKept();
|
|
249
266
|
await this._initIfNeeded(scene);
|
|
250
267
|
this._stack.push(scene);
|
|
268
|
+
this._overlay.push(false);
|
|
251
269
|
scene.onEnter?.(data);
|
|
252
270
|
}
|
|
253
271
|
_exitAndDestroyCurrent() {
|
|
@@ -259,6 +277,24 @@ var SceneManager = class {
|
|
|
259
277
|
this._initialized.delete(s.name);
|
|
260
278
|
}
|
|
261
279
|
this._stack.length = 0;
|
|
280
|
+
this._overlay.length = 0;
|
|
281
|
+
this._destroyKept();
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Destroy scenes popped with `keep: true` that were never pushed again. They
|
|
285
|
+
* already got `onExit` on the pop. The cache entry is dropped only when it is
|
|
286
|
+
* still this instance — an animated `go()` to the same name has already
|
|
287
|
+
* replaced it with the pending scene.
|
|
288
|
+
*/
|
|
289
|
+
_destroyKept() {
|
|
290
|
+
for (const s of this._kept) {
|
|
291
|
+
s.onDestroy?.();
|
|
292
|
+
if (this._scenes.get(s.name) === s) {
|
|
293
|
+
this._scenes.delete(s.name);
|
|
294
|
+
this._initialized.delete(s.name);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
this._kept.clear();
|
|
262
298
|
}
|
|
263
299
|
_finishOutPhase() {
|
|
264
300
|
if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
|
|
@@ -275,29 +311,42 @@ var SceneManager = class {
|
|
|
275
311
|
this._initialized.delete(s.name);
|
|
276
312
|
}
|
|
277
313
|
this._stack.length = 0;
|
|
314
|
+
this._overlay.length = 0;
|
|
315
|
+
this._destroyKept();
|
|
278
316
|
this._scenes.set(pending.name, pending);
|
|
279
317
|
this._initialized.add(pending.name);
|
|
280
318
|
pending.onInit?.();
|
|
281
319
|
this._stack.push(pending);
|
|
320
|
+
this._overlay.push(false);
|
|
282
321
|
pending.onEnter?.(data);
|
|
283
322
|
}
|
|
284
323
|
this._transitionState = this._transition.start("in");
|
|
285
324
|
}
|
|
325
|
+
/** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
|
|
326
|
+
_updateStack(dt) {
|
|
327
|
+
const stack = this._stack;
|
|
328
|
+
let from = stack.length - 1;
|
|
329
|
+
if (from < 0) return;
|
|
330
|
+
while (from > 0 && this._overlay[from]) from--;
|
|
331
|
+
for (let i = from; i < stack.length; i++) {
|
|
332
|
+
const scene = stack[i];
|
|
333
|
+
scene.onUpdate?.(dt);
|
|
334
|
+
scene.onDraw?.();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
286
337
|
_updateTransition(dt) {
|
|
287
338
|
if (!this._transitionState) return;
|
|
288
339
|
this._transition.update(this._transitionState, dt);
|
|
289
340
|
if (this._transitionState.done) {
|
|
290
341
|
if (this._transitionState.phase === "out") {
|
|
291
342
|
if (!this._preloadResolved) {
|
|
292
|
-
this.
|
|
293
|
-
this.current?.onDraw?.();
|
|
343
|
+
this._updateStack(dt);
|
|
294
344
|
return;
|
|
295
345
|
}
|
|
296
346
|
this._finishOutPhase();
|
|
297
347
|
} else this._transitionState = null;
|
|
298
348
|
}
|
|
299
|
-
this.
|
|
300
|
-
this.current?.onDraw?.();
|
|
349
|
+
this._updateStack(dt);
|
|
301
350
|
}
|
|
302
351
|
};
|
|
303
352
|
exports.SceneManager = SceneManager;
|
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 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?.onUp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EAE3E,MAAM,UAAU,KAAK;EACrB,MAAM,OAAO,KAAK;EAClB,KAAK,gBAAgB;EACrB,KAAK,eAAe,KAAA;EAEpB,IAAI,SAAS;GACX,KAAK,IAAI,IAAI,KAAK,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,MAAM;GAC9B,IAAI,KAAK,iBAAiB,UAAU,OAAO;IAEzC,IAAI,CAAC,KAAK,kBAAkB;KAC1B,KAAK,SAAS,WAAW,EAAE;KAC3B,KAAK,SAAS,SAAS;KACvB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,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\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;E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 +92,19 @@ function slideTransition(duration = .3, direction = "left", easing = identity) {
|
|
|
92
92
|
*
|
|
93
93
|
* - `register(name, factory)` — register a scene factory
|
|
94
94
|
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
95
|
-
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
96
|
-
* - `pop()` — pop the top scene and resume the one below
|
|
97
|
-
* - `update(dt)` — update the
|
|
95
|
+
* - `push(name, data?, options?)` — push a scene onto the stack (pauses current unless `options.overlay`)
|
|
96
|
+
* - `pop(options?)` — pop the top scene and resume the one below (destroys it unless `options.keep`)
|
|
97
|
+
* - `update(dt)` — update the live scenes (bottom-up) and any active transition
|
|
98
98
|
*/
|
|
99
99
|
var SceneManager = class {
|
|
100
100
|
_factories = /* @__PURE__ */ new Map();
|
|
101
101
|
_scenes = /* @__PURE__ */ new Map();
|
|
102
102
|
_initialized = /* @__PURE__ */ new Set();
|
|
103
103
|
_stack = [];
|
|
104
|
+
/** Parallel to `_stack`: whether the scene at that index was pushed as an overlay. */
|
|
105
|
+
_overlay = [];
|
|
106
|
+
/** Scenes popped with `keep: true`: initialized and cached, but off the stack until pushed again. */
|
|
107
|
+
_kept = /* @__PURE__ */ new Set();
|
|
104
108
|
_transition = noneTransition();
|
|
105
109
|
_transitionState = null;
|
|
106
110
|
_pendingScene = null;
|
|
@@ -166,38 +170,47 @@ var SceneManager = class {
|
|
|
166
170
|
}
|
|
167
171
|
}
|
|
168
172
|
/**
|
|
169
|
-
* Push a scene onto the stack. The current scene gets onExit
|
|
170
|
-
*
|
|
173
|
+
* Push a scene onto the stack. The current scene gets onExit, unless
|
|
174
|
+
* `options.overlay` is true — then it stays live and is updated and drawn
|
|
175
|
+
* before the pushed scene. The new scene gets onPreload → onInit → onEnter.
|
|
171
176
|
*/
|
|
172
|
-
async push(name, data) {
|
|
177
|
+
async push(name, data, options) {
|
|
173
178
|
const scene = this._getOrCreate(name);
|
|
174
179
|
if (!scene) return;
|
|
175
|
-
|
|
180
|
+
const overlay = options?.overlay === true;
|
|
181
|
+
if (!overlay) this.current?.onExit?.();
|
|
176
182
|
await this._initIfNeeded(scene);
|
|
183
|
+
this._kept.delete(scene);
|
|
177
184
|
this._stack.push(scene);
|
|
185
|
+
this._overlay.push(overlay);
|
|
178
186
|
scene.onEnter?.(data);
|
|
179
187
|
}
|
|
180
|
-
/**
|
|
181
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Pop the top scene. Calls onExit on it, then onDestroy unless
|
|
190
|
+
* `options.keep` is true — a kept scene stays initialized so the next `push`
|
|
191
|
+
* of its name goes straight to onEnter. The scene below gets
|
|
192
|
+
* onEnter(options.data) unless the popped scene was pushed as an overlay.
|
|
193
|
+
*/
|
|
194
|
+
pop(options) {
|
|
182
195
|
if (this._stack.length <= 1) return;
|
|
183
196
|
const top = this._stack.pop();
|
|
197
|
+
const wasOverlay = this._overlay.pop() === true;
|
|
184
198
|
top.onExit?.();
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
199
|
+
if (options?.keep === true) this._kept.add(top);
|
|
200
|
+
else {
|
|
201
|
+
top.onDestroy?.();
|
|
202
|
+
this._scenes.delete(top.name);
|
|
203
|
+
this._initialized.delete(top.name);
|
|
204
|
+
}
|
|
205
|
+
if (!wasOverlay) this.current?.onEnter?.(options?.data);
|
|
189
206
|
}
|
|
190
|
-
/** Update the
|
|
207
|
+
/** Update the live scenes (bottom-up, overlays last) and any active transition. */
|
|
191
208
|
update(dt) {
|
|
192
209
|
if (this._transitionState) {
|
|
193
210
|
this._updateTransition(dt);
|
|
194
211
|
return;
|
|
195
212
|
}
|
|
196
|
-
|
|
197
|
-
if (scene) {
|
|
198
|
-
scene.onUpdate?.(dt);
|
|
199
|
-
scene.onDraw?.();
|
|
200
|
-
}
|
|
213
|
+
this._updateStack(dt);
|
|
201
214
|
}
|
|
202
215
|
/** Destroy all scenes and clear state. */
|
|
203
216
|
destroy() {
|
|
@@ -207,6 +220,8 @@ var SceneManager = class {
|
|
|
207
220
|
scene.onDestroy?.();
|
|
208
221
|
}
|
|
209
222
|
this._stack.length = 0;
|
|
223
|
+
this._overlay.length = 0;
|
|
224
|
+
this._destroyKept();
|
|
210
225
|
this._scenes.clear();
|
|
211
226
|
this._initialized.clear();
|
|
212
227
|
this._transitionState = null;
|
|
@@ -245,8 +260,11 @@ var SceneManager = class {
|
|
|
245
260
|
this._initialized.delete(s.name);
|
|
246
261
|
}
|
|
247
262
|
this._stack.length = 0;
|
|
263
|
+
this._overlay.length = 0;
|
|
264
|
+
this._destroyKept();
|
|
248
265
|
await this._initIfNeeded(scene);
|
|
249
266
|
this._stack.push(scene);
|
|
267
|
+
this._overlay.push(false);
|
|
250
268
|
scene.onEnter?.(data);
|
|
251
269
|
}
|
|
252
270
|
_exitAndDestroyCurrent() {
|
|
@@ -258,6 +276,24 @@ var SceneManager = class {
|
|
|
258
276
|
this._initialized.delete(s.name);
|
|
259
277
|
}
|
|
260
278
|
this._stack.length = 0;
|
|
279
|
+
this._overlay.length = 0;
|
|
280
|
+
this._destroyKept();
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Destroy scenes popped with `keep: true` that were never pushed again. They
|
|
284
|
+
* already got `onExit` on the pop. The cache entry is dropped only when it is
|
|
285
|
+
* still this instance — an animated `go()` to the same name has already
|
|
286
|
+
* replaced it with the pending scene.
|
|
287
|
+
*/
|
|
288
|
+
_destroyKept() {
|
|
289
|
+
for (const s of this._kept) {
|
|
290
|
+
s.onDestroy?.();
|
|
291
|
+
if (this._scenes.get(s.name) === s) {
|
|
292
|
+
this._scenes.delete(s.name);
|
|
293
|
+
this._initialized.delete(s.name);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
this._kept.clear();
|
|
261
297
|
}
|
|
262
298
|
_finishOutPhase() {
|
|
263
299
|
if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
|
|
@@ -274,29 +310,42 @@ var SceneManager = class {
|
|
|
274
310
|
this._initialized.delete(s.name);
|
|
275
311
|
}
|
|
276
312
|
this._stack.length = 0;
|
|
313
|
+
this._overlay.length = 0;
|
|
314
|
+
this._destroyKept();
|
|
277
315
|
this._scenes.set(pending.name, pending);
|
|
278
316
|
this._initialized.add(pending.name);
|
|
279
317
|
pending.onInit?.();
|
|
280
318
|
this._stack.push(pending);
|
|
319
|
+
this._overlay.push(false);
|
|
281
320
|
pending.onEnter?.(data);
|
|
282
321
|
}
|
|
283
322
|
this._transitionState = this._transition.start("in");
|
|
284
323
|
}
|
|
324
|
+
/** Update then draw each live scene bottom-up: from the lowest scene not under an overlay to the top. */
|
|
325
|
+
_updateStack(dt) {
|
|
326
|
+
const stack = this._stack;
|
|
327
|
+
let from = stack.length - 1;
|
|
328
|
+
if (from < 0) return;
|
|
329
|
+
while (from > 0 && this._overlay[from]) from--;
|
|
330
|
+
for (let i = from; i < stack.length; i++) {
|
|
331
|
+
const scene = stack[i];
|
|
332
|
+
scene.onUpdate?.(dt);
|
|
333
|
+
scene.onDraw?.();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
285
336
|
_updateTransition(dt) {
|
|
286
337
|
if (!this._transitionState) return;
|
|
287
338
|
this._transition.update(this._transitionState, dt);
|
|
288
339
|
if (this._transitionState.done) {
|
|
289
340
|
if (this._transitionState.phase === "out") {
|
|
290
341
|
if (!this._preloadResolved) {
|
|
291
|
-
this.
|
|
292
|
-
this.current?.onDraw?.();
|
|
342
|
+
this._updateStack(dt);
|
|
293
343
|
return;
|
|
294
344
|
}
|
|
295
345
|
this._finishOutPhase();
|
|
296
346
|
} else this._transitionState = null;
|
|
297
347
|
}
|
|
298
|
-
this.
|
|
299
|
-
this.current?.onDraw?.();
|
|
348
|
+
this._updateStack(dt);
|
|
300
349
|
}
|
|
301
350
|
};
|
|
302
351
|
export { SceneManager, fadeTransition, noneTransition, slideTransition };
|
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 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,OA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YAAY,MAAM,IAAI;CACrD;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,SAAS,WAAW,EAAE;KAC3B,KAAK,SAAS,SAAS;KACvB;IACF;IAEA,KAAK,gBAAgB;GACvB,OAEE,KAAK,mBAAmB;EAE5B;EAGA,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\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 trans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AAY,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"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/scene",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"description": "Scene manager, transitions, lifecycle",
|
|
5
|
-
"keywords": ["flare-engine", "scene", "transitions"],
|
|
5
|
+
"keywords": ["flare-engine", "scene", "transitions", "react-native", "typescript"],
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
"typecheck": "tsc --noEmit"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@flare-engine/ecs": "^0.4.
|
|
41
|
-
"@flare-engine/math": "^0.4.
|
|
42
|
-
"@flare-engine/render": "^0.4.
|
|
40
|
+
"@flare-engine/ecs": "^0.4.10",
|
|
41
|
+
"@flare-engine/math": "^0.4.10",
|
|
42
|
+
"@flare-engine/render": "^0.4.10"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=20"
|