-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimegame.js
467 lines (412 loc) · 13.9 KB
/
runtimegame.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival ([email protected]). All rights reserved.
* This project is released under the MIT License.
*/
/**
* Represents a game being played.
*
* @memberof gdjs
* @class RuntimeGame
* @param {Object} data The object (usually stored in data.json) containing the full project data
* @param {Object} spec Optional object for specifiying additional options: {forceFullscreen: ...}
*/
gdjs.RuntimeGame = function(data, spec) {
spec = spec || {};
this._variables = new gdjs.VariablesContainer(data.variables);
this._data = data;
this._imageManager = new gdjs.ImageManager(
data.resources ? data.resources.resources : undefined
);
this._soundManager = new gdjs.SoundManager(
data.resources ? data.resources.resources : undefined
);
this._fontManager = new gdjs.FontManager(
data.resources ? data.resources.resources : undefined
);
this._jsonManager = new gdjs.JsonManager(
data.resources ? data.resources.resources : undefined
);
this._maxFPS = data ? parseInt(data.properties.maxFPS, 10) : 60;
this._minFPS = data ? parseInt(data.properties.minFPS, 10) : 15;
this._defaultWidth = data.properties.windowWidth; //Default size for scenes cameras
this._defaultHeight = data.properties.windowHeight;
this._originalWidth = data.properties.windowWidth; //Original size of the game, won't be changed.
this._originalHeight = data.properties.windowHeight;
/** @type {string} */
this._scaleMode = data.properties.scaleMode || 'linear';
this._renderer = new gdjs.RuntimeGameRenderer(
this,
this._defaultWidth,
this._defaultHeight,
spec.forceFullscreen || false
);
//Game loop management (see startGameLoop method)
this._sceneStack = new gdjs.SceneStack(this);
this._notifySceneForResize = false; //When set to true, the current scene is notified that canvas size changed.
this._paused = false;
//Inputs :
this._inputManager = new gdjs.InputManager();
//Allow to specify an external layout to insert in the first scene:
this._injectExternalLayout = spec.injectExternalLayout || '';
//Optional client to connect to a debugger:
this._debuggerClient = gdjs.DebuggerClient
? new gdjs.DebuggerClient(this)
: null;
};
gdjs.RuntimeGame.prototype.getRenderer = function() {
return this._renderer;
};
/**
* Get the variables of the RuntimeGame.
* @return {gdjs.VariablesContainer} The global variables
*/
gdjs.RuntimeGame.prototype.getVariables = function() {
return this._variables;
};
/**
* Get the gdjs.SoundManager of the RuntimeGame.
* @return {gdjs.SoundManager} The sound manager.
*/
gdjs.RuntimeGame.prototype.getSoundManager = function() {
return this._soundManager;
};
/**
* Get the gdjs.ImageManager of the RuntimeGame.
* @return {gdjs.ImageManager} The image manager.
*/
gdjs.RuntimeGame.prototype.getImageManager = function() {
return this._imageManager;
};
/**
* Get the gdjs.FontManager of the RuntimeGame.
* @return {gdjs.FontManager} The font manager.
*/
gdjs.RuntimeGame.prototype.getFontManager = function() {
return this._fontManager;
};
/**
* Get the input manager of the game, storing mouse, keyboard
* and touches states.
* @return {gdjs.InputManager} The input manager owned by the game
*/
gdjs.RuntimeGame.prototype.getInputManager = function() {
return this._inputManager;
};
/**
* Get the JSON manager of the game, used to load JSON from game
* resources.
* @return {gdjs.JsonManager} The json manager for the game
*/
gdjs.RuntimeGame.prototype.getJsonManager = function() {
return this._jsonManager;
};
/**
* Get the object containing the game data
* @return {Object} The object associated to the game.
*/
gdjs.RuntimeGame.prototype.getGameData = function() {
return this._data;
};
/**
* Get the data associated to a scene.
*
* @param {string} sceneName The name of the scene. If not defined, the first scene will be returned.
* @return The data associated to the scene.
*/
gdjs.RuntimeGame.prototype.getSceneData = function(sceneName) {
var scene = undefined;
for (var i = 0, len = this._data.layouts.length; i < len; ++i) {
var sceneData = this._data.layouts[i];
if (sceneName === undefined || sceneData.name === sceneName) {
scene = sceneData;
break;
}
}
if (scene === undefined)
console.warn('The game has no scene called "' + sceneName + '"');
return scene;
};
/**
* Check if a scene exists
*
* @param {string} sceneName The name of the scene to search.
* @return {boolean} true if the scene exists. If sceneName is undefined, true if the game has a scene.
*/
gdjs.RuntimeGame.prototype.hasScene = function(sceneName) {
var isTrue = false;
for (var i = 0, len = this._data.layouts.length; i < len; ++i) {
var sceneData = this._data.layouts[i];
if (sceneName === undefined || sceneData.name == sceneName) {
isTrue = true;
break;
}
}
return isTrue;
};
/**
* Get the data associated to an external layout.
*
* @param {string} name The name of the external layout.
* @return {Object} The data associated to the external layout or null if not found.
*/
gdjs.RuntimeGame.prototype.getExternalLayoutData = function(name) {
var externalLayout = null;
for (var i = 0, len = this._data.externalLayouts.length; i < len; ++i) {
var layoutData = this._data.externalLayouts[i];
if (layoutData.name === name) {
externalLayout = layoutData;
break;
}
}
return externalLayout;
};
/**
* Get the data representing all the global objects of the game.
* @return {Object} The data associated to the global objects.
*/
gdjs.RuntimeGame.prototype.getInitialObjectsData = function() {
return this._data.objects || [];
};
/**
* Get the original width of the game, as set on the startup of the game.
*
* This is guaranteed to never change, even if the size of the game is changed afterwards.
*/
gdjs.RuntimeGame.prototype.getOriginalWidth = function() {
return this._originalWidth;
};
/**
* Get the original height of the game, as set on the startup of the game.
*
* This is guaranteed to never change, even if the size of the game is changed afterwards.
*/
gdjs.RuntimeGame.prototype.getOriginalHeight = function() {
return this._originalHeight;
};
/**
* Get the default width of the game: canvas is created with this width,
* and cameras of layers are created using this width when a scene is started.
* @returns {number} The default width for cameras of layers
*/
gdjs.RuntimeGame.prototype.getDefaultWidth = function() {
return this._defaultWidth;
};
/**
* Get the default height of the game: canvas is created with this height,
* and cameras of layers are created using this height when a scene is started.
* @returns {number} The default height for cameras of layers
*/
gdjs.RuntimeGame.prototype.getDefaultHeight = function() {
return this._defaultHeight;
};
/**
* Change the default width of the game: It won't affect the canvas size, but
* new scene cameras will be created with this size.
* @param {number} width The new default width
*/
gdjs.RuntimeGame.prototype.setDefaultWidth = function(width) {
this._defaultWidth = width;
};
/**
* Change the default height of the game: It won't affect the canvas size, but
* new scene cameras will be created with this size.
* @param {number} height The new default height
*/
gdjs.RuntimeGame.prototype.setDefaultHeight = function(height) {
this._defaultHeight = height;
};
/**
* Return the minimal fps that must be guaranteed by the game
* (otherwise, game is slowed down).
*/
gdjs.RuntimeGame.prototype.getMinimalFramerate = function() {
return this._minFPS;
};
/**
* Return the scale mode of the game ("linear" or "nearest").
*/
gdjs.RuntimeGame.prototype.getScaleMode = function() {
return this._scaleMode;
};
/**
* Set or unset the game as paused.
* When paused, the game won't step and will be freezed. Useful for debugging.
* @param {boolean} enable true to pause the game, false to unpause
*/
gdjs.RuntimeGame.prototype.pause = function(enable) {
this._paused = enable;
};
/**
* Load all assets, displaying progress in renderer.
*/
gdjs.RuntimeGame.prototype.loadAllAssets = function(
callback,
progressCallback
) {
var loadingScreen = new gdjs.LoadingScreenRenderer(
this.getRenderer(),
this._data.properties.loadingScreen
);
var allAssetsTotal = this._data.resources.resources.length;
var that = this;
this._imageManager.loadTextures(
function(count, total) {
var percent = Math.floor((count / allAssetsTotal) * 100);
loadingScreen.render(percent);
if (progressCallback) progressCallback(percent);
},
function(texturesTotalCount) {
that._soundManager.preloadAudio(
function(count, total) {
var percent = Math.floor(
((texturesTotalCount + count) / allAssetsTotal) * 100
);
loadingScreen.render(percent);
if (progressCallback) progressCallback(percent);
},
function(audioTotalCount) {
that._fontManager.loadFonts(
function(count, total) {
var percent = Math.floor(
((texturesTotalCount + audioTotalCount + count) /
allAssetsTotal) *
100
);
loadingScreen.render(percent);
if (progressCallback) progressCallback(percent);
},
function(fontTotalCount) {
that._jsonManager.preloadJsons(
function(count, total) {
var percent = Math.floor(
((texturesTotalCount +
audioTotalCount +
fontTotalCount +
count) /
allAssetsTotal) *
100
);
loadingScreen.render(percent);
if (progressCallback) progressCallback(percent);
},
function() {
loadingScreen.unload();
callback();
}
);
}
);
}
);
}
);
};
/**
* Start the game loop, to be called once assets are loaded.
*/
gdjs.RuntimeGame.prototype.startGameLoop = function() {
if (!this.hasScene()) {
console.log('The game has no scene.');
return;
}
this.adaptRendererSizeToFillScreen();
//Load the first scene
var firstSceneName = this._data.firstLayout;
this._sceneStack.push(
this.hasScene(firstSceneName) ? firstSceneName : this.getSceneData().name,
this._injectExternalLayout
);
//Uncomment to profile the first x frames of the game.
// var x = 500;
// var startTime = Date.now();
// console.profile("Stepping for " + x + " frames")
// for(var i = 0; i < x; ++i) {
// this._sceneStack.step(16);
// }
// console.profileEnd();
// var time = Date.now() - startTime;
// console.log("Took", time, "ms");
// return;
//The standard game loop
var that = this;
var accumulatedElapsedTime = 0;
this._renderer.startGameLoop(function(lastCallElapsedTime) {
if (that._paused) return true;
// Skip the frame if we rendering frames too fast
accumulatedElapsedTime += lastCallElapsedTime;
if (
that._maxFPS > 0 &&
1000.0 / accumulatedElapsedTime > that._maxFPS + 7
) {
// Only skip frame if the framerate is 7 frames above the maximum framerate.
// Most browser/engines will try to run at slightly more than 60 frames per second.
// If game is set to have a maximum FPS to 60, then one out of two frames will be dropped.
// Hence, we use a 7 frames margin to ensure that we're not skipping frames too much.
return true;
}
var elapsedTime = accumulatedElapsedTime;
accumulatedElapsedTime = 0;
//Manage resize events.
if (that._notifySceneForResize) {
that._sceneStack.onRendererResized();
that._notifySceneForResize = false;
}
//Render and step the scene.
if (that._sceneStack.step(elapsedTime)) {
that.getInputManager().onFrameEnded();
return true;
}
return false;
});
};
/**
* Enlarge/reduce the width (or the height) of the game to fill the screen.
* @param {?string} mode `adaptWidth` to change the width, `adaptHeight` to change the height. If not defined, will use the game "sizeOnStartupMode" .
*/
gdjs.RuntimeGame.prototype.adaptRendererSizeToFillScreen = function(mode) {
if (
!gdjs.RuntimeGameRenderer ||
!gdjs.RuntimeGameRenderer.getScreenWidth ||
!gdjs.RuntimeGameRenderer.getScreenHeight
)
return;
newMode =
mode !== undefined ? mode : this._data.properties.sizeOnStartupMode || '';
var screenWidth = gdjs.RuntimeGameRenderer.getScreenWidth();
var screenHeight = gdjs.RuntimeGameRenderer.getScreenHeight();
// Enlarge either the width or the eight to fill the screen
var renderer = this.getRenderer();
var width = renderer.getCurrentWidth();
var height = renderer.getCurrentHeight();
if (newMode === 'adaptWidth') {
width = (height * screenWidth) / screenHeight;
} else if (newMode === 'adaptHeight') {
height = (width * screenHeight) / screenWidth;
}
// Update the renderer size, and also the default size of the game so that
// camera of scenes uses this size (otherwise, the rendering would be stretched)
renderer.setSize(width, height);
this.setDefaultWidth(width);
this.setDefaultHeight(height);
};
/**
* Start a profiler for the currently running scene.
* @param {Function} onProfilerStopped Function to be called when the profiler is stopped. Will be passed the profiler as argument.
*/
gdjs.RuntimeGame.prototype.startCurrentSceneProfiler = function(
onProfilerStopped
) {
var currentScene = this._sceneStack.getCurrentScene();
if (!currentScene) return false;
currentScene.startProfiler(onProfilerStopped);
return true;
};
/**
* Stop the profiler for the currently running scene.
*/
gdjs.RuntimeGame.prototype.stopCurrentSceneProfiler = function() {
var currentScene = this._sceneStack.getCurrentScene();
if (!currentScene) return null;
currentScene.stopProfiler();
};