class Game { activeScenes = []; pendingRemoval = []; previousStep = 0; //used for calculating deltaTime isRunning = false; rafId = null; images = {}; shaders = {}; constructor () { this.previousStep = 0; this.activeScenes = []; this.isRunning = false; this.rafId = null; this.shaders = {}; this.update = this.update.bind(this); } startGame () { GameInput.g_init(); if (this.isRunning) { return; } this.isRunning = true; this.previousStep = Date.now(); this.update(); } stopGame () { this.isRunning = false; if (this.rafId != null) { cancelAnimationFrame(this.rafId); this.rafId = null; } } preloadImagesThenStart (imageData, onComplete, onError) { const cont = async () => { try { await this.preloadImages( imageData ); this.startGame(); onComplete && onComplete(); } catch (err) { console.error("GAME: Image preload failed.", err); onError && onError(err); } }; cont(); } setPrimaryScene (scene) { if (this.activeScenes.contains(scene)) { for (let i = 0; i < this.activeScenes.length; i++) { this.activeScenes[i].g_primary = false; } scene.g_primary = true; } } async preloadImages (imagesData) { const promises = []; const context = this; for (var imageData of imagesData) { if (context.images[imageData.name]) { console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)"); } promises.push(new Promise((resolve, reject) => { var img = new Image(); var imageContext = { name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow, loop: imageData.loop, chain: imageData.chain }; img.onload = function () { var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow, imageContext.loop, imageContext.chain); finalImage.name = imageContext.name; context.images[imageContext.name] = finalImage; resolve(); }; img.onerror = function () { reject(new Error("Failed to load image '" + imageContext.name + "' from " + imageData.url)); }; img.src = imageData.url; img.setAttribute("style", "display:none"); })); } await Promise.all(promises); console.log("all images loaded ", this.images); return this.images; } async preloadShaders (shadersData) { const context = this; const promises = []; for (const shaderData of shadersData) { if (!shaderData || !shaderData.name) { throw new Error("preloadShaders: each entry needs a name"); } if (!shaderData.frag) { throw new Error("preloadShaders: '" + shaderData.name + "' needs a frag path"); } if (context.shaders[shaderData.name]) { console.warn("Shader '" + shaderData.name + "' is already loaded! (overwriting anyway)"); } promises.push((async () => { let vertSource = shaderData.screen ? Shader.SCREEN_VERT : Shader.DEFAULT_VERT; if (shaderData.vert) { const vertResponse = await fetch(shaderData.vert); if (!vertResponse.ok) { throw new Error("Failed to load vertex shader '" + shaderData.name + "' from " + shaderData.vert); } vertSource = await vertResponse.text(); } const fragResponse = await fetch(shaderData.frag); if (!fragResponse.ok) { throw new Error("Failed to load fragment shader '" + shaderData.name + "' from " + shaderData.frag); } const fragSource = await fragResponse.text(); const shader = new Shader(vertSource, fragSource); shader.name = shaderData.name; shader.screen = !!shaderData.screen || vertSource === Shader.SCREEN_VERT; context.shaders[shaderData.name] = shader; })()); } await Promise.all(promises); console.log("all shaders loaded ", this.shaders); return this.shaders; } loadScene (scene) { if (this.activeScenes.length == 0) { scene.g_primary = true; } this.activeScenes.push(scene); scene.game = this; scene.onLoad(); } removeScene (scene) { if (this.activeScenes.indexOf(scene) >= 0) { if (!this.pendingRemoval.contains(scene)) { this.pendingRemoval.push(scene); } } } update () { this.previousStep = this.previousStep || 0; const stepTime = Date.now(); let stepTimeCurr = (stepTime - this.previousStep) * 0.001; stepTimeCurr = Math.min(stepTimeCurr, 0.1); if (isNaN(stepTimeCurr)) { stepTimeCurr = 0; } GameInput.g_updateKeys(); for (let i = 0; i < this.activeScenes.length; i++) { this.activeScenes[i].update(stepTimeCurr); this.activeScenes[i].draw(); } this.cleanActiveScenes(); this.previousStep = stepTime; TWEEN.update(); if (this.isRunning) { this.rafId = requestAnimationFrame(this.update); } } cleanActiveScenes () { if (this.pendingRemoval.length > 0) { for (let i = 0; i < this.pendingRemoval.length; i++) { const scene = this.pendingRemoval[i]; const index = this.activeScenes.indexOf(scene); if (index >= 0 && scene.destroyed) { this.activeScenes.removeAt(index); } } this.pendingRemoval.length = 0; } } }