BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine.js

engine.js

missed scene reference
timfa Tim Falken · pushed by timfa · 8 years ago
a9a9589a History
engine.js 2.5 KiB Download Raw
1//engine
2
3var Engine =
4{
5 x: 0,
6 y: 0,
7 w: 0,
8 h: 0,//stored for window-building
9}
10
11//<DOC> category Game
12var Game =
13{
14 stepPrevious: 0,
15
16 //<DOC> <parameter> Game.activeScene.activeObjects<br><br>array containing all existing objects.
17 activeScene: new Scene(),
18 userUpdateCallback: null,
19
20 //<DOC> <parameter> Game.stepsSinceStart<br><br> Total amount of update steps since the start of the game.
21 stepsSinceStart: 0,
22
23 //<DOC> <function> Game.initialize(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)<br><br>Starts the game, and creates a window at ('windowX', 'windowY'), using 'windowWidth' and 'windowHeight' as sizes for the game window in pixels. 'fps' controls the game's frames-per-second'. As soon as the game is loaded up, it'll call the function 'loadedCallback', so you can properly start the game.
24 initialize: function(windowX, windowY, windowWidth, windowHeight, fps, loadedCallback)
25 {
26 console.log("GAME: Initializing. Loading dependencies...");
27 Engine.x = windowX;
28 Engine.y = windowY;
29 Engine.w = windowWidth;
30 Engine.h = windowHeight;
31
32 GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);
33 GameInput.g_init();
34 window.setInterval(Game.gameStep, 1000 / fps);
35 loadedCallback();
36 },
37
38 loadScene: function(newScene, preserveOld = false)
39 {
40 if(!preserveOld)
41 {
42 Game.activeScene.destroy();
43 }
44
45 Game.activeScene = newScene;
46 },
47
48 //<DOC> <function> Game.setUpdateFunction(callback)<br><br>Assigns a function 'callback(deltaTime)' to be called every frame of the game. You can use this to run global code for your game.
49 setUpdateFunction: function(callback)
50 {
51 Game.userUpdateCallback = callback;
52 },
53
54 //<DOC> <function> Game.clearScene()<br><br>destroys every object in the scene.
55 clearScene: function()
56 {
57 Game.activeScene.destroy();
58 },
59
60 gameStep: function()
61 {
62 var stepTime = Date.now();
63
64 var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;
65
66 stepTimeCurr = Math.min(stepTimeCurr, 0.1);
67
68 if(isNaN(stepTimeCurr))
69 stepTimeCurr = 0;
70
71 Game.stepsSinceStart++;
72
73 GameInput.g_updateKeys();
74
75 var toUpdate = Game.activeScene.activeObjects.slice();
76
77 for(var i = 0; i < toUpdate.length; i++)
78 {
79 if(toUpdate[i].active || toUpdate[i].destroyed)
80 toUpdate[i].g_updateStep(stepTimeCurr);
81 }
82
83 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
84
85 GameWindow.redraw();
86
87 this.stepPrevious = stepTime;
88 },
89
90 reorderObjects: function()
91 {
92 this.activeScene.activeObjects.sort(function(a, b)
93 {
94 return a.renderer.layerDepth - b.renderer.layerDepth;
95 })
96 }
97};