| 1 | class Game |
| 2 | { |
| 3 | activeScenes = []; |
| 4 | pendingRemoval = []; |
| 5 | |
| 6 | previousStep = 0; //used for calculating deltaTime |
| 7 | isRunning = false; |
| 8 | rafId = null; |
| 9 | |
| 10 | images = {}; |
| 11 | shaders = {}; |
| 12 | |
| 13 | constructor () |
| 14 | { |
| 15 | this.previousStep = 0; |
| 16 | this.activeScenes = []; |
| 17 | this.isRunning = false; |
| 18 | this.rafId = null; |
| 19 | this.shaders = {}; |
| 20 | this.update = this.update.bind(this); |
| 21 | } |
| 22 | |
| 23 | startGame () |
| 24 | { |
| 25 | GameInput.g_init(); |
| 26 | |
| 27 | if (this.isRunning) |
| 28 | { return; } |
| 29 | |
| 30 | this.isRunning = true; |
| 31 | this.previousStep = Date.now(); |
| 32 | this.update(); |
| 33 | } |
| 34 | |
| 35 | stopGame () |
| 36 | { |
| 37 | this.isRunning = false; |
| 38 | if (this.rafId != null) |
| 39 | { |
| 40 | cancelAnimationFrame(this.rafId); |
| 41 | this.rafId = null; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | preloadImagesThenStart (imageData, onComplete, onError) |
| 46 | { |
| 47 | const cont = async () => |
| 48 | { |
| 49 | try |
| 50 | { |
| 51 | await this.preloadImages( |
| 52 | imageData |
| 53 | ); |
| 54 | |
| 55 | this.startGame(); |
| 56 | onComplete && onComplete(); |
| 57 | } |
| 58 | catch (err) |
| 59 | { |
| 60 | console.error("GAME: Image preload failed.", err); |
| 61 | onError && onError(err); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | cont(); |
| 66 | } |
| 67 | |
| 68 | setPrimaryScene (scene) |
| 69 | { |
| 70 | if (this.activeScenes.contains(scene)) |
| 71 | { |
| 72 | for (let i = 0; i < this.activeScenes.length; i++) |
| 73 | { this.activeScenes[i].g_primary = false; } |
| 74 | |
| 75 | scene.g_primary = true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | async preloadImages (imagesData) |
| 80 | { |
| 81 | const promises = []; |
| 82 | const context = this; |
| 83 | |
| 84 | for (var imageData of imagesData) |
| 85 | { |
| 86 | if (context.images[imageData.name]) |
| 87 | { |
| 88 | console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)"); |
| 89 | } |
| 90 | |
| 91 | promises.push(new Promise((resolve, reject) => |
| 92 | { |
| 93 | var img = new Image(); |
| 94 | var imageContext = { name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow, loop: imageData.loop, chain: imageData.chain }; |
| 95 | |
| 96 | img.onload = function () |
| 97 | { |
| 98 | var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow, imageContext.loop, imageContext.chain); |
| 99 | finalImage.name = imageContext.name; |
| 100 | |
| 101 | context.images[imageContext.name] = finalImage; |
| 102 | |
| 103 | resolve(); |
| 104 | }; |
| 105 | |
| 106 | img.onerror = function () |
| 107 | { |
| 108 | reject(new Error("Failed to load image '" + imageContext.name + "' from " + imageData.url)); |
| 109 | }; |
| 110 | |
| 111 | img.src = imageData.url; |
| 112 | img.setAttribute("style", "display:none"); |
| 113 | })); |
| 114 | } |
| 115 | await Promise.all(promises); |
| 116 | |
| 117 | console.log("all images loaded ", this.images); |
| 118 | |
| 119 | return this.images; |
| 120 | } |
| 121 | |
| 122 | async preloadShaders (shadersData) |
| 123 | { |
| 124 | const context = this; |
| 125 | const promises = []; |
| 126 | |
| 127 | for (const shaderData of shadersData) |
| 128 | { |
| 129 | if (!shaderData || !shaderData.name) |
| 130 | { throw new Error("preloadShaders: each entry needs a name"); } |
| 131 | |
| 132 | if (!shaderData.frag) |
| 133 | { throw new Error("preloadShaders: '" + shaderData.name + "' needs a frag path"); } |
| 134 | |
| 135 | if (context.shaders[shaderData.name]) |
| 136 | { console.warn("Shader '" + shaderData.name + "' is already loaded! (overwriting anyway)"); } |
| 137 | |
| 138 | promises.push((async () => |
| 139 | { |
| 140 | let vertSource = Shader.DEFAULT_VERT; |
| 141 | |
| 142 | if (shaderData.vert) |
| 143 | { |
| 144 | const vertResponse = await fetch(shaderData.vert); |
| 145 | if (!vertResponse.ok) |
| 146 | { |
| 147 | throw new Error("Failed to load vertex shader '" + shaderData.name + "' from " + shaderData.vert); |
| 148 | } |
| 149 | |
| 150 | vertSource = await vertResponse.text(); |
| 151 | } |
| 152 | |
| 153 | const fragResponse = await fetch(shaderData.frag); |
| 154 | if (!fragResponse.ok) |
| 155 | { |
| 156 | throw new Error("Failed to load fragment shader '" + shaderData.name + "' from " + shaderData.frag); |
| 157 | } |
| 158 | |
| 159 | const fragSource = await fragResponse.text(); |
| 160 | const shader = new Shader(vertSource, fragSource); |
| 161 | shader.name = shaderData.name; |
| 162 | context.shaders[shaderData.name] = shader; |
| 163 | })()); |
| 164 | } |
| 165 | |
| 166 | await Promise.all(promises); |
| 167 | console.log("all shaders loaded ", this.shaders); |
| 168 | return this.shaders; |
| 169 | } |
| 170 | |
| 171 | loadScene (scene) |
| 172 | { |
| 173 | if (this.activeScenes.length == 0) |
| 174 | { scene.g_primary = true; } |
| 175 | |
| 176 | this.activeScenes.push(scene); |
| 177 | scene.game = this; |
| 178 | scene.onLoad(); |
| 179 | } |
| 180 | |
| 181 | removeScene (scene) |
| 182 | { |
| 183 | if (this.activeScenes.indexOf(scene) >= 0) |
| 184 | { |
| 185 | if (!this.pendingRemoval.contains(scene)) |
| 186 | { this.pendingRemoval.push(scene); } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | update () |
| 191 | { |
| 192 | this.previousStep = this.previousStep || 0; |
| 193 | const stepTime = Date.now(); |
| 194 | let stepTimeCurr = (stepTime - this.previousStep) * 0.001; |
| 195 | stepTimeCurr = Math.min(stepTimeCurr, 0.1); |
| 196 | if (isNaN(stepTimeCurr)) |
| 197 | { stepTimeCurr = 0; } |
| 198 | |
| 199 | GameInput.g_updateKeys(); |
| 200 | |
| 201 | for (let i = 0; i < this.activeScenes.length; i++) |
| 202 | { |
| 203 | this.activeScenes[i].update(stepTimeCurr); |
| 204 | this.activeScenes[i].draw(); |
| 205 | } |
| 206 | |
| 207 | this.cleanActiveScenes(); |
| 208 | |
| 209 | this.previousStep = stepTime; |
| 210 | |
| 211 | TWEEN.update(); |
| 212 | |
| 213 | if (this.isRunning) |
| 214 | { this.rafId = requestAnimationFrame(this.update); } |
| 215 | } |
| 216 | |
| 217 | cleanActiveScenes () |
| 218 | { |
| 219 | if (this.pendingRemoval.length > 0) |
| 220 | { |
| 221 | for (let i = 0; i < this.pendingRemoval.length; i++) |
| 222 | { |
| 223 | const scene = this.pendingRemoval[i]; |
| 224 | const index = this.activeScenes.indexOf(scene); |
| 225 | if (index >= 0 && scene.destroyed) |
| 226 | { this.activeScenes.removeAt(index); } |
| 227 | } |
| 228 | |
| 229 | this.pendingRemoval.length = 0; |
| 230 | } |
| 231 | } |
| 232 | } |