| 1 | class Game |
| 2 | { |
| 3 | activeScenes = []; |
| 4 | pendingRemoval = []; |
| 5 | |
| 6 | previousStep = 0; //used for calculating deltaTime |
| 7 | |
| 8 | images = {}; |
| 9 | |
| 10 | constructor () |
| 11 | { |
| 12 | this.previousStep = 0; |
| 13 | this.activeScenes = []; |
| 14 | this.update = this.update.bind(this); |
| 15 | } |
| 16 | |
| 17 | startGame () |
| 18 | { |
| 19 | this.update(); |
| 20 | GameInput.g_init(); |
| 21 | } |
| 22 | |
| 23 | preloadImagesThenStart (imageData, onComplete) |
| 24 | { |
| 25 | const cont = async () => |
| 26 | { |
| 27 | await this.preloadImages( |
| 28 | imageData |
| 29 | ); |
| 30 | |
| 31 | this.startGame(); |
| 32 | onComplete && onComplete(); |
| 33 | }; |
| 34 | |
| 35 | cont.apply(); |
| 36 | } |
| 37 | |
| 38 | setPrimaryScene (scene) |
| 39 | { |
| 40 | if (this.activeScenes.contains(scene)) |
| 41 | { |
| 42 | for (let i = 0; i < this.activeScenes.length; i++) |
| 43 | this.activeScenes[i].g_primary = false; |
| 44 | |
| 45 | scene.g_primary = true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | async preloadImages (imagesData) |
| 50 | { |
| 51 | const promises = []; |
| 52 | const context = this; |
| 53 | |
| 54 | for (var imageData of imagesData) |
| 55 | { |
| 56 | if (context.images[imageData.name]) |
| 57 | { |
| 58 | console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)"); |
| 59 | } |
| 60 | |
| 61 | promises.push(new Promise(resolve => |
| 62 | { |
| 63 | var img = new Image(); |
| 64 | var imageContext = { name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow, loop: imageData.loop, chain: imageData.chain }; |
| 65 | |
| 66 | img.onload = function () |
| 67 | { |
| 68 | |
| 69 | var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow); |
| 70 | finalImage.name = imageContext.name; |
| 71 | |
| 72 | context.images[imageContext.name] = finalImage; |
| 73 | |
| 74 | resolve(); |
| 75 | }; |
| 76 | |
| 77 | img.src = imageData.url; |
| 78 | img.setAttribute("style", "display:none"); |
| 79 | })); |
| 80 | } |
| 81 | await Promise.all(promises); |
| 82 | |
| 83 | console.log("all images loaded ", this.images); |
| 84 | |
| 85 | return this.images; |
| 86 | } |
| 87 | |
| 88 | loadScene (scene) |
| 89 | { |
| 90 | if (this.activeScenes.length == 0) |
| 91 | scene.g_primary = true; |
| 92 | |
| 93 | this.activeScenes.push(scene); |
| 94 | scene.game = this; |
| 95 | scene.onLoad(); |
| 96 | } |
| 97 | |
| 98 | removeScene (scene) |
| 99 | { |
| 100 | let index = this.activeScenes.indexOf(scene); |
| 101 | if (index >= 0 && !scene.destroyed) |
| 102 | { |
| 103 | if (!this.pendingRemoval.contains(index)) |
| 104 | this.pendingRemoval.push(index); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | update () |
| 109 | { |
| 110 | this.previousStep = this.previousStep || 0; |
| 111 | let stepTime = Date.now(); |
| 112 | let stepTimeCurr = (stepTime - this.previousStep) * 0.001; |
| 113 | stepTimeCurr = Math.min(stepTimeCurr, 0.1); |
| 114 | if (isNaN(stepTimeCurr)) |
| 115 | stepTimeCurr = 0; |
| 116 | |
| 117 | GameInput.g_updateKeys(); |
| 118 | |
| 119 | for (let i = 0; i < this.activeScenes.length; i++) |
| 120 | { |
| 121 | this.activeScenes[i].update(stepTimeCurr); |
| 122 | this.activeScenes[i].draw(); |
| 123 | } |
| 124 | |
| 125 | this.cleanActiveScenes(); |
| 126 | |
| 127 | this.previousStep = stepTime; |
| 128 | |
| 129 | TWEEN.update(); |
| 130 | |
| 131 | requestAnimationFrame(this.update); |
| 132 | } |
| 133 | |
| 134 | cleanActiveScenes () |
| 135 | { |
| 136 | if (this.pendingRemoval.length > 0) |
| 137 | { |
| 138 | this.pendingRemoval.sort((a, b) => a - b); |
| 139 | |
| 140 | for (let i = this.pendingRemoval.length - 1; i >= 0; i--) |
| 141 | { |
| 142 | if (this.activeScenes[this.pendingRemoval[i]].destroyed) |
| 143 | this.activeScenes.removeAt(this.pendingRemoval[i]); |
| 144 | |
| 145 | this.pendingRemoval.splice(i, 1); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |