| 1 | var Engine = |
| 2 | { |
| 3 | x: 0, |
| 4 | y: 0, |
| 5 | w: 0, |
| 6 | h: 0,//stored for window-building |
| 7 | } |
| 8 | |
| 9 | var 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, fps, 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, 1000 / fps); |
| 32 | loadedCallback(); |
| 33 | }, |
| 34 | |
| 35 | destroy: function() |
| 36 | { |
| 37 | Game.activeScene.destroy(); |
| 38 | window.clearInterval(Game.windowIntervalId); |
| 39 | document.getElementById(GameWindow.elementID).outerHTML = ""; |
| 40 | document.getElementById("g_gameTextInput").outerHTML = ""; |
| 41 | }, |
| 42 | |
| 43 | loadScene: function(newScene, destroyOld = false) |
| 44 | { |
| 45 | if(destroyOld) |
| 46 | { |
| 47 | Game.activeScene.destroy(); |
| 48 | } |
| 49 | |
| 50 | Game.activeScene = newScene; |
| 51 | }, |
| 52 | |
| 53 | setUpdateFunction: function(callback) |
| 54 | { |
| 55 | Game.userUpdateCallback = callback; |
| 56 | }, |
| 57 | |
| 58 | clearScene: function() |
| 59 | { |
| 60 | Game.activeScene.destroy(); |
| 61 | }, |
| 62 | |
| 63 | gameStep: function() |
| 64 | { |
| 65 | var stepTime = Date.now(); |
| 66 | |
| 67 | var stepTimeCurr = (stepTime - this.stepPrevious) * 0.001; |
| 68 | |
| 69 | stepTimeCurr = Math.min(stepTimeCurr, 0.1); |
| 70 | |
| 71 | if(isNaN(stepTimeCurr)) |
| 72 | stepTimeCurr = 0; |
| 73 | |
| 74 | Game.stepsSinceStart++; |
| 75 | |
| 76 | TextInput.g_update(); |
| 77 | |
| 78 | for(var i = 0; i < Game.queuedCallbacks.length; i++) |
| 79 | { |
| 80 | Game.queuedCallbacks[i].callback.call(Game.queuedCallbacks[i].context); |
| 81 | } |
| 82 | |
| 83 | Game.queuedCallbacks = []; |
| 84 | |
| 85 | GameInput.g_updateKeys(); |
| 86 | |
| 87 | var toUpdate = Game.activeScene.activeObjects.slice(); |
| 88 | |
| 89 | for(var i = 0; i < toUpdate.length; i++) |
| 90 | { |
| 91 | if(toUpdate[i].active || toUpdate[i].destroyed) |
| 92 | toUpdate[i].g_updateStep(stepTimeCurr); |
| 93 | } |
| 94 | |
| 95 | if (typeof Game.userUpdateCallback == 'function') { Game.userUpdateCallback(stepTimeCurr); } |
| 96 | |
| 97 | GameWindow.redraw(); |
| 98 | |
| 99 | this.stepPrevious = stepTime; |
| 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 | }; |