diff --git a/mar/editor.js b/mar/editor.js index 9c5b0c9..2ef473b 100644 --- a/mar/editor.js +++ b/mar/editor.js @@ -404,4 +404,34 @@ function editorClick() { document.getElementById("gameBtns").setAttribute("style", "display: none"); } +/* + im using a closure because i don't like putting everything in global scope + feel free to disagree +*/ +var lazySave = (function() { + if(typeof window.localStorage === 'undefined') { + return function() {}; // if browser does not support local storage make it a no op (no idea what browsers) + } + + // store a reference to the timeout + var DELAY = 3000; + var timeout = null; + + // Basicly on every change set a timeout that will wait DELAY milliseconds before storing the code. + // This will prevent unnececary saving on every change + return function(event) { + // if another timeout was waiting clear that one + if(timeout !== null) { + clearTimeout(timeout); + } + // and set a new one + timeout = setTimeout(function() { + window.localStorage.setItem('editorCodeContents', ace.edit("editor").getValue()); + timeout = null; // clear the timeout cached variable after its function has run + }, DELAY); + }; +})(); + editor.on("change", parse); +// You can add multiple handlers on events +editor.on("change", lazySave); diff --git a/mar/phaser/mar.js b/mar/phaser/mar.js index fc453ea..974e23a 100644 --- a/mar/phaser/mar.js +++ b/mar/phaser/mar.js @@ -791,11 +791,8 @@ function manhanttanDistance(x1, y1, x2, y2) { } function codeListener(message) { - if (message.t === "code") { - ace.edit("editor").setValue(message.code); - } } @@ -1035,7 +1032,15 @@ var GameClient = function (callback) { }; - self.reloadCode(); + // check if we can load code locally first + try { + var value = window.localStorage.getItem("editorCodeContents"); + if(value === null) throw new TypeError("no code stored locally under key: 'editorCodeContents'") + ace.edit("editor").setValue(value); + + } catch(e) { + self.reloadCode(); + } if (callback !== undefined) { @@ -1152,11 +1157,11 @@ BasicGame.Boot.prototype = { game.camera.x = 280; game.camera.y = 90; game.stage.disableVisibilityChange = true; - + this.scale.scaleMode = Phaser.ScaleManager.RESIZE; this.scale.pageAlignHorizontally = true; this.scale.pageAlignVertically = true; - + }, create: function () { @@ -1179,7 +1184,7 @@ BasicGame.Boot.prototype = { }, update: function () { game.scale.setShowAll(); - game.scale.refresh(); + game.scale.refresh(); // Update the cursor position. // It's important to understand that screen-to-isometric projection means you have to specify a z position manually, as this cannot be easily // determined from the 2D pointer position without extra trickery. By default, the z position is 0 if not set.