-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenestack.js
108 lines (89 loc) · 3.18 KB
/
scenestack.js
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
/**
* Hold the stack of scenes (gdjs.RuntimeScene) being played.
*
* @memberof gdjs
* @param {gdjs.RuntimeGame} runtimeGame The runtime game that is using the scene stack
* @class SceneStack
*/
gdjs.SceneStack = function(runtimeGame) {
if (!runtimeGame) {
throw "SceneStack must be constructed with a gdjs.RuntimeGame."
}
this._runtimeGame = runtimeGame;
/** @type gdjs.RuntimeScene[] */
this._stack = [];
};
gdjs.SceneStack.prototype.onRendererResized = function() {
for(var i = 0;i < this._stack.length; ++i) {
this._stack[i].onCanvasResized();
}
};
gdjs.SceneStack.prototype.step = function(elapsedTime) {
if (this._stack.length === 0) return false;
var currentScene = this._stack[this._stack.length - 1];
if (currentScene.renderAndStep(elapsedTime)) {
var request = currentScene.getRequestedChange();
//Something special was requested by the current scene.
if (request === gdjs.RuntimeScene.STOP_GAME) {
this._runtimeGame.getRenderer().stopGame();
return true;
} else if (request === gdjs.RuntimeScene.POP_SCENE) {
this.pop();
} else if (request === gdjs.RuntimeScene.PUSH_SCENE) {
this.push(currentScene.getRequestedScene());
} else if (request === gdjs.RuntimeScene.REPLACE_SCENE) {
this.replace(currentScene.getRequestedScene());
} else if (request === gdjs.RuntimeScene.CLEAR_SCENES) {
this.replace(currentScene.getRequestedScene(), true);
} else {
console.error("Unrecognized change in scene stack.");
return false;
}
}
return true;
};
gdjs.SceneStack.prototype.renderWithoutStep = function(elapsedTime) {
if (this._stack.length === 0) return false;
var currentScene = this._stack[this._stack.length - 1];
currentScene.render(elapsedTime);
return true;
};
gdjs.SceneStack.prototype.pop = function() {
if (this._stack.length <= 1) return null;
var scene = this._stack.pop();
scene.unloadScene();
return scene;
};
gdjs.SceneStack.prototype.push = function(newSceneName, externalLayoutName) {
var newScene = new gdjs.RuntimeScene(this._runtimeGame);
newScene.loadFromScene(this._runtimeGame.getSceneData(newSceneName));
//Optionally create the objects from an external layout.
if (externalLayoutName) {
var externalLayoutData = this._runtimeGame.getExternalLayoutData(externalLayoutName);
if (externalLayoutData)
newScene.createObjectsFrom(externalLayoutData.instances, 0, 0);
}
this._stack.push(newScene);
return newScene;
};
gdjs.SceneStack.prototype.replace = function(newSceneName, clear) {
if (!!clear) {
while (this._stack.length !== 0) {
var scene = this._stack.pop();
scene.unloadScene();
}
} else {
if (this._stack.length !== 0) {
var scene = this._stack.pop();
scene.unloadScene();
}
}
return this.push(newSceneName);
};
/**
* Return the current gdjs.RuntimeScene being played, or null if none is run.
*/
gdjs.SceneStack.prototype.getCurrentScene = function() {
if (this._stack.length === 0) return null;
return this._stack[this._stack.length - 1];
};