@vpmedia/phaser 1.118.0 → 1.119.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +23 -24
- package/dist/index.js.map +1 -1
- package/dist/phaser/core/game.d.ts +34 -8
- package/dist/phaser/core/game.d.ts.map +1 -1
- package/dist/phaser/core/scene.d.ts +3 -2
- package/dist/phaser/core/scene.d.ts.map +1 -1
- package/dist/phaser/core/scene_manager.d.ts +28 -17
- package/dist/phaser/core/scene_manager.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/phaser/core/game.ts +45 -22
- package/src/phaser/core/scene.ts +4 -2
- package/src/phaser/core/scene_manager.test.ts +193 -0
- package/src/phaser/core/scene_manager.ts +51 -36
- package/src/phaser/display/webgl/renderer.ts +1 -1
|
@@ -7,34 +7,47 @@ export type SceneHooks = {
|
|
|
7
7
|
preload?: () => void;
|
|
8
8
|
create?: () => void;
|
|
9
9
|
update?: () => void;
|
|
10
|
+
render?: () => void;
|
|
10
11
|
resize?: (width: number, height: number) => void;
|
|
11
12
|
pauseUpdate?: () => void;
|
|
12
13
|
shutdown?: () => void;
|
|
13
14
|
};
|
|
14
15
|
|
|
16
|
+
/** A scene once the manager holds it: its hooks plus the wiring the manager adds. */
|
|
17
|
+
export type SceneState = SceneHooks & {
|
|
18
|
+
game?: Game | null;
|
|
19
|
+
key?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** What the manager accepts as a scene: an instance, a plain hook object, or a class to build one. */
|
|
23
|
+
export type SceneDefinition = SceneState | (new (game: Game) => SceneState);
|
|
24
|
+
|
|
25
|
+
/** A hook invoked with the scene as its receiver and the game as its argument. */
|
|
26
|
+
type SceneCallback = (game: Game) => void;
|
|
27
|
+
|
|
15
28
|
export class SceneManager {
|
|
16
29
|
public game!: Game;
|
|
17
|
-
public states!:
|
|
18
|
-
public _pendingState!:
|
|
19
|
-
public _clearWorld!:
|
|
20
|
-
public _clearCache!:
|
|
21
|
-
public _created!:
|
|
22
|
-
public _args!:
|
|
30
|
+
public states!: Record<string, SceneState>;
|
|
31
|
+
public _pendingState!: SceneDefinition | string | null;
|
|
32
|
+
public _clearWorld!: boolean;
|
|
33
|
+
public _clearCache!: boolean;
|
|
34
|
+
public _created!: boolean;
|
|
35
|
+
public _args!: unknown[];
|
|
23
36
|
public current!: string;
|
|
24
|
-
public onInitCallback!:
|
|
25
|
-
public onPreloadCallback!:
|
|
26
|
-
public onCreateCallback!:
|
|
27
|
-
public onUpdateCallback!:
|
|
28
|
-
public onResizeCallback!:
|
|
29
|
-
public onPauseUpdateCallback!:
|
|
30
|
-
public onShutDownCallback!:
|
|
31
|
-
public callbackContext!:
|
|
37
|
+
public onInitCallback!: ((...args: unknown[]) => void) | null;
|
|
38
|
+
public onPreloadCallback!: SceneCallback | null;
|
|
39
|
+
public onCreateCallback!: SceneCallback | null;
|
|
40
|
+
public onUpdateCallback!: SceneCallback | null;
|
|
41
|
+
public onResizeCallback!: ((width: number, height: number) => void) | null;
|
|
42
|
+
public onPauseUpdateCallback!: SceneCallback | null;
|
|
43
|
+
public onShutDownCallback!: SceneCallback | null;
|
|
44
|
+
public callbackContext!: SceneState;
|
|
32
45
|
/**
|
|
33
46
|
* Creates a new SceneManager instance.
|
|
34
47
|
* @param {Game} game - The game instance this manager belongs to.
|
|
35
48
|
* @param {string} pendingState - The state to load when the game boots.
|
|
36
49
|
*/
|
|
37
|
-
public constructor(game: Game, pendingState: string) {
|
|
50
|
+
public constructor(game: Game, pendingState: SceneDefinition | string | null) {
|
|
38
51
|
this.game = game;
|
|
39
52
|
this.states = {};
|
|
40
53
|
this._pendingState = null;
|
|
@@ -72,15 +85,15 @@ export class SceneManager {
|
|
|
72
85
|
* @param {boolean} autoStart - Whether to start this state immediately.
|
|
73
86
|
* @returns {Scene|object} The created scene or state object.
|
|
74
87
|
*/
|
|
75
|
-
public add(key: string, state:
|
|
76
|
-
let newState
|
|
77
|
-
if (state
|
|
78
|
-
newState = state;
|
|
79
|
-
} else if (typeof state === 'object') {
|
|
80
|
-
newState = state;
|
|
81
|
-
newState.game = this.game;
|
|
82
|
-
} else if (typeof state === 'function') {
|
|
88
|
+
public add(key: string, state: SceneDefinition, autoStart = false): SceneState {
|
|
89
|
+
let newState: SceneState;
|
|
90
|
+
if (typeof state === 'function') {
|
|
83
91
|
newState = new state(this.game);
|
|
92
|
+
} else {
|
|
93
|
+
newState = state;
|
|
94
|
+
if (!(state instanceof Scene)) {
|
|
95
|
+
newState.game = this.game;
|
|
96
|
+
}
|
|
84
97
|
}
|
|
85
98
|
this.states[key] = newState;
|
|
86
99
|
if (autoStart) {
|
|
@@ -149,7 +162,7 @@ export class SceneManager {
|
|
|
149
162
|
* This method is called before the game loop updates.
|
|
150
163
|
*/
|
|
151
164
|
public preUpdate(): void {
|
|
152
|
-
if (this._pendingState && this.game.isBooted) {
|
|
165
|
+
if (typeof this._pendingState === 'string' && this._pendingState && this.game.isBooted) {
|
|
153
166
|
// var previousStateKey = this.current;
|
|
154
167
|
// Already got a state running?
|
|
155
168
|
this.clearCurrentState();
|
|
@@ -207,11 +220,9 @@ export class SceneManager {
|
|
|
207
220
|
* @returns {boolean} True if the scene exists, false otherwise.
|
|
208
221
|
*/
|
|
209
222
|
public checkState(key: string): boolean {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
return false;
|
|
223
|
+
const state = this.states[key];
|
|
224
|
+
if (state) {
|
|
225
|
+
return Boolean(state.preload ?? state.create ?? state.update ?? state.render);
|
|
215
226
|
}
|
|
216
227
|
return false;
|
|
217
228
|
}
|
|
@@ -221,8 +232,11 @@ export class SceneManager {
|
|
|
221
232
|
* @param {string} key - The unique key for the state to link.
|
|
222
233
|
*/
|
|
223
234
|
public link(key: string): void {
|
|
224
|
-
this.states[key]
|
|
225
|
-
|
|
235
|
+
const state = this.states[key];
|
|
236
|
+
if (state) {
|
|
237
|
+
state.game = this.game;
|
|
238
|
+
state.key = key;
|
|
239
|
+
}
|
|
226
240
|
}
|
|
227
241
|
|
|
228
242
|
/**
|
|
@@ -230,8 +244,9 @@ export class SceneManager {
|
|
|
230
244
|
* @param {string} key - The unique key for the state to unlink.
|
|
231
245
|
*/
|
|
232
246
|
public unlink(key: string): void {
|
|
233
|
-
|
|
234
|
-
|
|
247
|
+
const state = this.states[key];
|
|
248
|
+
if (state) {
|
|
249
|
+
state.game = null;
|
|
235
250
|
}
|
|
236
251
|
}
|
|
237
252
|
|
|
@@ -240,7 +255,7 @@ export class SceneManager {
|
|
|
240
255
|
* @param {string} key - The unique key for the state to set as current.
|
|
241
256
|
*/
|
|
242
257
|
public setCurrentState(key: string): void {
|
|
243
|
-
this.callbackContext = this.states[key]
|
|
258
|
+
this.callbackContext = this.states[key]!;
|
|
244
259
|
this.link(key);
|
|
245
260
|
// Used when the state is set as being the current active state
|
|
246
261
|
this.onInitCallback = this.callbackContext.init ?? this.dummy;
|
|
@@ -265,7 +280,7 @@ export class SceneManager {
|
|
|
265
280
|
* @returns {T} The current scene state.
|
|
266
281
|
*/
|
|
267
282
|
public getCurrentState<T = Partial<Scene>>(): T {
|
|
268
|
-
return this.states[this.current];
|
|
283
|
+
return this.states[this.current] as T;
|
|
269
284
|
}
|
|
270
285
|
|
|
271
286
|
/**
|
|
@@ -273,7 +288,7 @@ export class SceneManager {
|
|
|
273
288
|
* This method is called when scene loading is complete.
|
|
274
289
|
*/
|
|
275
290
|
public loadComplete(): void {
|
|
276
|
-
if (this._created
|
|
291
|
+
if (!this._created && this.onCreateCallback) {
|
|
277
292
|
this._created = true;
|
|
278
293
|
this.onCreateCallback.call(this.callbackContext, this.game);
|
|
279
294
|
} else {
|
|
@@ -71,7 +71,7 @@ export class WebGLRenderer {
|
|
|
71
71
|
this.height = game.height;
|
|
72
72
|
this.view = game.canvas;
|
|
73
73
|
this._contextOptions = {
|
|
74
|
-
alpha: game.config.transparent,
|
|
74
|
+
alpha: Boolean(game.config.transparent),
|
|
75
75
|
depth: false,
|
|
76
76
|
antialias: game.config.antialias,
|
|
77
77
|
premultipliedAlpha: game.config.transparent && game.config.transparent !== 'notMultiplied',
|