BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / engine.js

engine.js

move to dirs
timfa Tim · pushed by timfa · 4 years ago
e719f4ca History
engine.js 2.1 KiB Download Raw
1var Engine =
2{
3 x: 0,
4 y: 0,
5 w: 0,
6 h: 0,//stored for window-building
7}
8
9var Game =
10{
11 stepPrevious: 0,
12
13 activeScene: new Scene(),
14 userUpdateCallback: null,
15
16 stepsSinceStart: 0,
17
18 queuedCallbacks: [],
19
20 initialize: function(windowX, windowY, windowWidth, windowHeight, loadedCallback)
21 {
22 console.log("GAME: Initializing. Loading dependencies...");
23 Engine.x = windowX;
24 Engine.y = windowY;
25 Engine.w = windowWidth;
26 Engine.h = windowHeight;
27
28 GameWindow.init(Engine.x, Engine.y, Engine.w, Engine.h);
29 GameInput.g_init();
30 TextInput.g_init();
31 window.setInterval(Game.gameStep, 1);
32 GameWindow.redraw();
33 loadedCallback();
34 },
35
36 destroy: function()
37 {
38 Game.activeScene.destroy();
39 window.clearInterval(Game.windowIntervalId);
40 document.getElementById(GameWindow.elementID).outerHTML = "";
41 document.getElementById("g_gameTextInput").outerHTML = "";
42 },
43
44 loadScene: function(newScene, destroyOld = false)
45 {
46 if(destroyOld)
47 {
48 Game.activeScene.destroy();
49 }
50
51 Game.activeScene = newScene;
52 },
53
54 setUpdateFunction: function(callback)
55 {
56 Game.userUpdateCallback = callback;
57 },
58
59 clearScene: function()
60 {
61 Game.activeScene.destroy();
62 },
63
64 gameStep: function()
65 {
66 var stepTime = Date.now();
67
68 var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001;
69
70 stepTimeCurr = Math.min(stepTimeCurr, 0.1);
71
72 if(isNaN(stepTimeCurr))
73 stepTimeCurr = 0;
74
75 Game.stepsSinceStart++;
76
77 TextInput.g_update();
78
79 for(var i = 0; i < Game.queuedCallbacks.length; i++)
80 {
81 Game.queuedCallbacks[i].callback.call(Game.queuedCallbacks[i].context);
82 }
83
84 Game.queuedCallbacks = [];
85
86 GameInput.g_updateKeys();
87
88 var toUpdate = Game.activeScene.activeObjects.slice();
89
90 for(var i = 0; i < toUpdate.length; i++)
91 {
92 if(toUpdate[i].active || toUpdate[i].destroyed)
93 toUpdate[i].g_updateStep(stepTimeCurr);
94 }
95
96 if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); }
97
98 this.stepPrevious = stepTime;
99 TWEEN.update();
100 },
101
102 reorderObjects: function()
103 {
104 this.activeScene.activeObjects.sort(function(a, b)
105 {
106 return a.renderer.layerDepth - b.renderer.layerDepth;
107 })
108 }
109};