@flare-engine/scene 0.2.1 → 0.3.1
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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +16 -6
- package/dist/index.d.mts +93 -87
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +93 -87
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +301 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +297 -245
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -10
package/dist/index.js
CHANGED
|
@@ -1,284 +1,310 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
SceneManager: () => SceneManager,
|
|
24
|
-
fadeTransition: () => fadeTransition,
|
|
25
|
-
noneTransition: () => noneTransition,
|
|
26
|
-
slideTransition: () => slideTransition
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(index_exports);
|
|
29
|
-
|
|
30
|
-
// src/transition.ts
|
|
31
|
-
var identity = (t) => t;
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _flare_engine_ecs = require("@flare-engine/ecs");
|
|
3
|
+
let _flare_engine_render = require("@flare-engine/render");
|
|
4
|
+
//#region src/transition.ts
|
|
5
|
+
const identity = (t) => t;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a stateful advance function that tracks raw (linear) progress in its
|
|
8
|
+
* own closure so the easing function is applied without accumulating drift.
|
|
9
|
+
* Call `reset()` when starting a new transition phase.
|
|
10
|
+
*/
|
|
32
11
|
function makeAdvancer(duration, easing) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
};
|
|
12
|
+
let raw = 0;
|
|
13
|
+
return {
|
|
14
|
+
reset() {
|
|
15
|
+
raw = 0;
|
|
16
|
+
},
|
|
17
|
+
advance(state, dt) {
|
|
18
|
+
if (state.done) return state;
|
|
19
|
+
raw += dt / duration;
|
|
20
|
+
if (raw >= 1) {
|
|
21
|
+
raw = 1;
|
|
22
|
+
state.progress = 1;
|
|
23
|
+
state.done = true;
|
|
24
|
+
} else state.progress = easing(raw);
|
|
25
|
+
return state;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
51
28
|
}
|
|
29
|
+
/** No transition — instant scene switch. */
|
|
52
30
|
function noneTransition() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
31
|
+
return {
|
|
32
|
+
kind: "none",
|
|
33
|
+
duration: 0,
|
|
34
|
+
start(phase) {
|
|
35
|
+
return {
|
|
36
|
+
progress: 1,
|
|
37
|
+
done: true,
|
|
38
|
+
phase
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
update(state) {
|
|
42
|
+
state.progress = 1;
|
|
43
|
+
state.done = true;
|
|
44
|
+
return state;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
65
47
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
48
|
+
/** Fade transition — consumer interprets `progress` as alpha. */
|
|
49
|
+
function fadeTransition(duration = .3, easing = identity) {
|
|
50
|
+
const advancer = makeAdvancer(duration, easing);
|
|
51
|
+
return {
|
|
52
|
+
kind: "fade",
|
|
53
|
+
duration,
|
|
54
|
+
start(phase) {
|
|
55
|
+
advancer.reset();
|
|
56
|
+
return {
|
|
57
|
+
progress: 0,
|
|
58
|
+
done: false,
|
|
59
|
+
phase
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
update(state, dt) {
|
|
63
|
+
return advancer.advance(state, dt);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
79
66
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.
|
|
69
|
+
* Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).
|
|
70
|
+
*/
|
|
71
|
+
function slideTransition(duration = .3, direction = "left", easing = identity) {
|
|
72
|
+
const advancer = makeAdvancer(duration, easing);
|
|
73
|
+
return {
|
|
74
|
+
kind: "slide",
|
|
75
|
+
duration,
|
|
76
|
+
direction,
|
|
77
|
+
start(phase) {
|
|
78
|
+
advancer.reset();
|
|
79
|
+
return {
|
|
80
|
+
progress: 0,
|
|
81
|
+
done: false,
|
|
82
|
+
phase
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
update(state, dt) {
|
|
86
|
+
return advancer.advance(state, dt);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
94
89
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/scene-manager.ts
|
|
92
|
+
/**
|
|
93
|
+
* SceneManager — manages scene registration, navigation, and a scene stack.
|
|
94
|
+
*
|
|
95
|
+
* - `register(name, factory)` — register a scene factory
|
|
96
|
+
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
97
|
+
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
98
|
+
* - `pop()` — pop the top scene and resume the one below
|
|
99
|
+
* - `update(dt)` — update the active scene and any active transition
|
|
100
|
+
*/
|
|
99
101
|
var SceneManager = class {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
102
|
+
_factories = /* @__PURE__ */ new Map();
|
|
103
|
+
_scenes = /* @__PURE__ */ new Map();
|
|
104
|
+
_initialized = /* @__PURE__ */ new Set();
|
|
105
|
+
_stack = [];
|
|
106
|
+
_transition = noneTransition();
|
|
107
|
+
_transitionState = null;
|
|
108
|
+
_pendingScene = null;
|
|
109
|
+
_pendingData = void 0;
|
|
110
|
+
_preloadResolved = true;
|
|
111
|
+
_preloadGen = 0;
|
|
112
|
+
/** The currently active scene (top of stack). */
|
|
113
|
+
get current() {
|
|
114
|
+
return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
|
|
115
|
+
}
|
|
116
|
+
/** Number of scenes on the stack. */
|
|
117
|
+
get stackSize() {
|
|
118
|
+
return this._stack.length;
|
|
119
|
+
}
|
|
120
|
+
/** Whether a transition is currently in progress. */
|
|
121
|
+
get isTransitioning() {
|
|
122
|
+
return this._transitionState !== null;
|
|
123
|
+
}
|
|
124
|
+
/** Set the default transition for scene switches. */
|
|
125
|
+
setTransition(transition) {
|
|
126
|
+
this._transition = transition;
|
|
127
|
+
}
|
|
128
|
+
/** Register a scene factory by name. */
|
|
129
|
+
register(name, factory) {
|
|
130
|
+
this._factories.set(name, factory);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Navigate to a scene by name. Clears the entire stack.
|
|
134
|
+
* Calls onExit on the current scene, onDestroy on all stacked scenes,
|
|
135
|
+
* then onPreload → onInit → onEnter on the new scene.
|
|
136
|
+
*/
|
|
137
|
+
async go(name, data) {
|
|
138
|
+
if (!this._factories.has(name)) {
|
|
139
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.warn(`[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(", ")}`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this._transition.duration > 0 && this.current) {
|
|
143
|
+
this._scenes.delete(name);
|
|
144
|
+
this._initialized.delete(name);
|
|
145
|
+
const scene = this._getOrCreate(name);
|
|
146
|
+
this._pendingScene = scene;
|
|
147
|
+
this._pendingData = data;
|
|
148
|
+
this._initialized.add(scene.name);
|
|
149
|
+
this._preloadResolved = false;
|
|
150
|
+
const gen = ++this._preloadGen;
|
|
151
|
+
const preload = scene.onPreload?.();
|
|
152
|
+
if (preload == null) this._preloadResolved = true;
|
|
153
|
+
else preload.then(() => {
|
|
154
|
+
if (gen !== this._preloadGen) return;
|
|
155
|
+
this._preloadResolved = true;
|
|
156
|
+
this._finishOutPhase();
|
|
157
|
+
}, (err) => {
|
|
158
|
+
if (gen !== this._preloadGen) return;
|
|
159
|
+
this._preloadResolved = true;
|
|
160
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.error(`[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`, err);
|
|
161
|
+
this._finishOutPhase();
|
|
162
|
+
});
|
|
163
|
+
this._transitionState = this._transition.start("out");
|
|
164
|
+
} else {
|
|
165
|
+
this._exitAndDestroyCurrent();
|
|
166
|
+
const scene = this._getOrCreate(name);
|
|
167
|
+
await this._activateScene(scene, data);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Push a scene onto the stack. The current scene gets onExit.
|
|
172
|
+
* The new scene gets onPreload → onInit → onEnter.
|
|
173
|
+
*/
|
|
174
|
+
async push(name, data) {
|
|
175
|
+
const scene = this._getOrCreate(name);
|
|
176
|
+
if (!scene) return;
|
|
177
|
+
this.current?.onExit?.();
|
|
178
|
+
await this._initIfNeeded(scene);
|
|
179
|
+
this._stack.push(scene);
|
|
180
|
+
scene.onEnter?.(data);
|
|
181
|
+
}
|
|
182
|
+
/** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */
|
|
183
|
+
pop() {
|
|
184
|
+
if (this._stack.length <= 1) return;
|
|
185
|
+
const top = this._stack.pop();
|
|
186
|
+
top.onExit?.();
|
|
187
|
+
top.onDestroy?.();
|
|
188
|
+
this._scenes.delete(top.name);
|
|
189
|
+
this._initialized.delete(top.name);
|
|
190
|
+
this.current?.onEnter?.();
|
|
191
|
+
}
|
|
192
|
+
/** Update the active scene and any active transition. */
|
|
193
|
+
update(dt) {
|
|
194
|
+
if (this._transitionState) {
|
|
195
|
+
this._updateTransition(dt);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const scene = this.current;
|
|
199
|
+
if (scene) {
|
|
200
|
+
scene.onUpdate?.(dt);
|
|
201
|
+
scene.onDraw?.();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Destroy all scenes and clear state. */
|
|
205
|
+
destroy() {
|
|
206
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
207
|
+
const scene = this._stack[i];
|
|
208
|
+
scene.onExit?.();
|
|
209
|
+
scene.onDestroy?.();
|
|
210
|
+
}
|
|
211
|
+
this._stack.length = 0;
|
|
212
|
+
this._scenes.clear();
|
|
213
|
+
this._initialized.clear();
|
|
214
|
+
this._transitionState = null;
|
|
215
|
+
this._pendingScene = null;
|
|
216
|
+
}
|
|
217
|
+
/** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */
|
|
218
|
+
get transitionProgress() {
|
|
219
|
+
if (!this._transitionState) return 0;
|
|
220
|
+
return this._transitionState.progress;
|
|
221
|
+
}
|
|
222
|
+
/** Get the current transition phase. */
|
|
223
|
+
get transitionPhase() {
|
|
224
|
+
return this._transitionState?.phase ?? null;
|
|
225
|
+
}
|
|
226
|
+
_getOrCreate(name) {
|
|
227
|
+
let scene = this._scenes.get(name);
|
|
228
|
+
if (scene) return scene;
|
|
229
|
+
const factory = this._factories.get(name);
|
|
230
|
+
if (!factory) return null;
|
|
231
|
+
scene = factory();
|
|
232
|
+
scene.world = new _flare_engine_ecs.World();
|
|
233
|
+
scene.camera = new _flare_engine_render.Camera2D();
|
|
234
|
+
this._scenes.set(name, scene);
|
|
235
|
+
return scene;
|
|
236
|
+
}
|
|
237
|
+
async _initIfNeeded(scene) {
|
|
238
|
+
if (this._initialized.has(scene.name)) return;
|
|
239
|
+
this._initialized.add(scene.name);
|
|
240
|
+
await scene.onPreload?.();
|
|
241
|
+
scene.onInit?.();
|
|
242
|
+
}
|
|
243
|
+
async _activateScene(scene, data) {
|
|
244
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
245
|
+
const s = this._stack[i];
|
|
246
|
+
s.onExit?.();
|
|
247
|
+
s.onDestroy?.();
|
|
248
|
+
this._scenes.delete(s.name);
|
|
249
|
+
this._initialized.delete(s.name);
|
|
250
|
+
}
|
|
251
|
+
this._stack.length = 0;
|
|
252
|
+
await this._initIfNeeded(scene);
|
|
253
|
+
this._stack.push(scene);
|
|
254
|
+
scene.onEnter?.(data);
|
|
255
|
+
}
|
|
256
|
+
_exitAndDestroyCurrent() {
|
|
257
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
258
|
+
const s = this._stack[i];
|
|
259
|
+
s.onExit?.();
|
|
260
|
+
s.onDestroy?.();
|
|
261
|
+
this._scenes.delete(s.name);
|
|
262
|
+
this._initialized.delete(s.name);
|
|
263
|
+
}
|
|
264
|
+
this._stack.length = 0;
|
|
265
|
+
}
|
|
266
|
+
_finishOutPhase() {
|
|
267
|
+
if (!this._transitionState?.done || this._transitionState.phase !== "out") return;
|
|
268
|
+
const pending = this._pendingScene;
|
|
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");
|
|
288
|
+
}
|
|
289
|
+
_updateTransition(dt) {
|
|
290
|
+
if (!this._transitionState) return;
|
|
291
|
+
this._transition.update(this._transitionState, dt);
|
|
292
|
+
if (this._transitionState.done) if (this._transitionState.phase === "out") {
|
|
293
|
+
if (!this._preloadResolved) {
|
|
294
|
+
this.current?.onUpdate?.(dt);
|
|
295
|
+
this.current?.onDraw?.();
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
this._finishOutPhase();
|
|
299
|
+
} else this._transitionState = null;
|
|
300
|
+
this.current?.onUpdate?.(dt);
|
|
301
|
+
this.current?.onDraw?.();
|
|
302
|
+
}
|
|
276
303
|
};
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
});
|
|
304
|
+
//#endregion
|
|
305
|
+
exports.SceneManager = SceneManager;
|
|
306
|
+
exports.fadeTransition = fadeTransition;
|
|
307
|
+
exports.noneTransition = noneTransition;
|
|
308
|
+
exports.slideTransition = slideTransition;
|
|
309
|
+
|
|
284
310
|
//# sourceMappingURL=index.js.map
|