From 5224f69de2ceedc904df6d399c4f092866f61bc0 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 14 Apr 2016 14:54:56 -0600 Subject: [PATCH] Improved script loading, added loading progress bar. --- Oversimplified.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Oversimplified.js b/Oversimplified.js index 7710f6a..a1165ae 100644 --- a/Oversimplified.js +++ b/Oversimplified.js @@ -10,6 +10,7 @@ Oversimplified.canvas = null; Oversimplified.context = null; Oversimplified.nextID = 0; Oversimplified.loadingScripts = []; +Oversimplified.loadedScripts = []; Oversimplified.emptyImage = new Image(); Oversimplified.emptyImage.src = "data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA="; Oversimplified.emptyImage.width = 1; @@ -18,6 +19,7 @@ Oversimplified.emptyImage.height = 1; // Settings Namespace Oversimplified.Settings = { defaultStep : 1/30, + numberOfScriptsToLoad : 0, soundVolume : 0.75, musicVolume : 0.75, preventRightClick : true @@ -32,7 +34,7 @@ Oversimplified.timestamp = function() { Oversimplified.now = null; Oversimplified.dateTime = 0; Oversimplified.lastFrame = Oversimplified.timestamp(); -Oversimplified.step = Oversimplified.Settings.defaultStep; //seconds per frame +Oversimplified.step = Oversimplified.Settings.defaultStep; //seconds per frame, i.e. one 30th of a second passes each frame. // Camera Object Oversimplified.camera = { @@ -1585,7 +1587,9 @@ Oversimplified.SetCanvasToCameraSize = function () { // Defines the order of operations for the Frame. Oversimplified.Frame = function () { - if (Oversimplified.loadingScripts.length == 0) { + if ((Oversimplified.Settings.numberOfScriptsToLoad > 0 && Oversimplified.loadedScripts.length == Oversimplified.Settings.numberOfScriptsToLoad) || + (Oversimplified.Settings.numberOfScriptsToLoad <= 0 &&Oversimplified.loadingScripts.length == 0)) + { Oversimplified.now = Oversimplified.timestamp(); Oversimplified.dateTime = Oversimplified.dateTime + Math.min(1, (Oversimplified.now - Oversimplified.lastFrame) / 1000); while (Oversimplified.dateTime > Oversimplified.step) { @@ -1596,7 +1600,34 @@ Oversimplified.Frame = function () { } Oversimplified.lastFrame = Oversimplified.now; } else { - if (Oversimplified.DEBUG.showMessages) console.log("Loading scripts: " + Oversimplified.loadingScripts.toString()); + if (Oversimplified.DEBUG.showMessages) { + var debugMessage = "Loaded " + Oversimplified.loadedScripts.length.toString(); + debugMessage += (Oversimplified.Settings.numberOfScriptsToLoad > 0) ? " of " + Oversimplified.Settings.numberOfScriptsToLoad.toString() : ""; + debugMessage += " scripts:\n" + Oversimplified.loadedScripts.toString() + ".\nWaiting for:\n" + Oversimplified.loadingScripts.toString(); + console.log(debugMessage); + } + + if (Oversimplified.Settings.numberOfScriptsToLoad > 0) { + var percentage = Oversimplified.loadedScripts.length / Oversimplified.Settings.numberOfScriptsToLoad; + var barHeight = 32; + var maxBarWidth = Math.round(Oversimplified.camera.width * 0.6); + var barWidth = Math.round(maxBarWidth * percentage); + var barX = Math.round(Oversimplified.camera.width * 0.2); + var barY = Math.round(Oversimplified.camera.height * 0.5) - Math.round(barHeight / 2); + + var saveFillStyle = OS.context.fillStyle; + var saveStrokeStyle = OS.context.strokeStyle; + + OS.context.fillStyle = "#DD5511"; + OS.context.fillRect(barX, barY, barWidth, barHeight); + + OS.context.strokeStyle= "#882200"; + OS.context.lineWidth=5; + OS.context.strokeRect(barX, barY, maxBarWidth, barHeight); + + OS.context.fillStyle = saveFillStyle; + OS.context.strokeStyle = saveStrokeStyle; + } } requestAnimationFrame(Oversimplified.Frame); @@ -1726,6 +1757,7 @@ Oversimplified.AddScript = function (pathToScript, mainFunction) { } } + Oversimplified.loadedScripts.push(pathToScript); Oversimplified.loadingScripts.splice(Oversimplified.loadingScripts.indexOf(pathToScript), 1); }; document.body.appendChild(script); @@ -1733,7 +1765,9 @@ Oversimplified.AddScript = function (pathToScript, mainFunction) { // Callback function that prevents any added scripts from executing until all scripts are loaded. Oversimplified.WaitForScriptsToLoad = function (Function) { - if (Oversimplified.loadingScripts.length > 0) { + if (Oversimplified.DEBUG.showMessages && Oversimplified.Settings.numberOfScriptsToLoad > 0) console.log("Waiting for " + (Oversimplified.Settings.numberOfScriptsToLoad - Oversimplified.loadedScripts.length).toString() + " scripts to load"); + if (Oversimplified.loadingScripts.length > 0) + { setTimeout(function(){Oversimplified.WaitForScriptsToLoad(Function)}, 0.1); } else { Function();