/
scenestack.ts
167 lines (152 loc) · 5 KB
/
scenestack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
namespace gdjs {
const logger = new gdjs.Logger('Scene stack');
/**
* Hold the stack of scenes ({@link gdjs.RuntimeScene}) being played.
*/
export class SceneStack {
_runtimeGame: gdjs.RuntimeGame;
_stack: gdjs.RuntimeScene[] = [];
_wasFirstSceneLoaded: boolean = false;
/**
* @param runtimeGame The runtime game that is using the scene stack
*/
constructor(runtimeGame: gdjs.RuntimeGame) {
if (!runtimeGame) {
throw 'SceneStack must be constructed with a gdjs.RuntimeGame.';
}
this._runtimeGame = runtimeGame;
}
/**
* Called by the RuntimeGame when the game resolution is changed.
* Useful to notify scene and layers that resolution is changed, as they
* might be caching it.
*/
onGameResolutionResized(): void {
for (let i = 0; i < this._stack.length; ++i) {
this._stack[i].onGameResolutionResized();
}
}
step(elapsedTime: float): boolean {
if (this._stack.length === 0) {
return false;
}
const currentScene = this._stack[this._stack.length - 1];
if (currentScene.renderAndStep(elapsedTime)) {
const request = currentScene.getRequestedChange();
//Something special was requested by the current scene.
if (request === gdjs.SceneChangeRequest.STOP_GAME) {
this._runtimeGame.getRenderer().stopGame();
return true;
} else if (request === gdjs.SceneChangeRequest.POP_SCENE) {
this.pop();
} else if (request === gdjs.SceneChangeRequest.PUSH_SCENE) {
this.push(currentScene.getRequestedScene());
} else if (request === gdjs.SceneChangeRequest.REPLACE_SCENE) {
this.replace(currentScene.getRequestedScene());
} else if (request === gdjs.SceneChangeRequest.CLEAR_SCENES) {
this.replace(currentScene.getRequestedScene(), true);
} else {
logger.error('Unrecognized change in scene stack: ' + request);
return false;
}
}
return true;
}
renderWithoutStep(): boolean {
if (this._stack.length === 0) {
return false;
}
const currentScene = this._stack[this._stack.length - 1];
currentScene.render();
return true;
}
pop(): gdjs.RuntimeScene | null {
if (this._stack.length <= 1) {
return null;
}
// Unload the current scene
const scene = this._stack.pop();
if (!scene) {
return null;
}
scene.unloadScene();
// Tell the new current scene it's being resumed
const currentScene = this._stack[this._stack.length - 1];
if (currentScene) {
currentScene.onResume();
}
return scene;
}
/**
* Pause the scene currently being played and start the new scene that is specified.
* If `externalLayoutName` is set, also instantiate the objects from this external layout.
*/
push(newSceneName: string, externalLayoutName?: string): gdjs.RuntimeScene {
// Tell the scene it's being paused
const currentScene = this._stack[this._stack.length - 1];
if (currentScene) {
currentScene.onPause();
}
// Load the new one
const newScene = new gdjs.RuntimeScene(this._runtimeGame);
newScene.loadFromScene(this._runtimeGame.getSceneData(newSceneName));
this._wasFirstSceneLoaded = true;
// Optionally create the objects from an external layout.
if (externalLayoutName) {
const externalLayoutData = this._runtimeGame.getExternalLayoutData(
externalLayoutName
);
if (externalLayoutData) {
newScene.createObjectsFrom(
externalLayoutData.instances,
0,
0,
/*trackByPersistentUuid=*/
true
);
}
}
this._stack.push(newScene);
return newScene;
}
/**
* Start the specified scene, replacing the one currently being played.
* If `clear` is set to true, all running scenes are also removed from the stack of scenes.
*/
replace(newSceneName: string, clear?: boolean): gdjs.RuntimeScene {
if (!!clear) {
// Unload all the scenes
while (this._stack.length !== 0) {
let scene = this._stack.pop();
if (scene) {
scene.unloadScene();
}
}
} else {
// Unload the current scene
if (this._stack.length !== 0) {
let scene = this._stack.pop();
if (scene) {
scene.unloadScene();
}
}
}
return this.push(newSceneName);
}
/**
* Return the current gdjs.RuntimeScene being played, or null if none is run.
*/
getCurrentScene(): gdjs.RuntimeScene | null {
if (this._stack.length === 0) {
return null;
}
return this._stack[this._stack.length - 1];
}
/**
* Return true if a scene was loaded, false otherwise (i.e: game not yet started).
*/
wasFirstSceneLoaded(): boolean {
return this._wasFirstSceneLoaded;
}
}
}