Commits

Anonymous committed 838970a

Refactor (to reduce number of lines of JS in this repo).

  • Participants
  • Parent commits 0e123c9

Comments (0)

Files changed (5)

javascript/canvas2.html

 <head>
   <meta charset="utf-8">
   <title>HTML5 Canvas experiment #2</title>
+  <script src="playfield.js"></script>
   <script src="canvas2.js"></script>
   <style>
 #canvas { border: 1px solid blue; }

javascript/canvas2.js

-Playfield = function() {
-    this._store = {};
-    this.min_x = undefined;
-    this.min_y = undefined;
-    this.max_x = undefined;
-    this.max_y = undefined;
-
-    this.get = function(x, y) {
-        return this._store[x+','+y];
-    };
-
-    this.put = function(x, y, value) {
-        if (this.min_x === undefined || x < this.min_x) this.min_x = x;
-        if (this.max_x === undefined || x > this.max_x) this.max_x = x;
-        if (this.min_y === undefined || y < this.min_y) this.min_y = y;
-        if (this.max_y === undefined || y > this.max_y) this.max_y = y;
-        if (value === undefined) {
-            delete this._store[x+','+y];
-        }
-        this._store[x+','+y] = value;
-    };
-          
-    this.foreach = function(fun) {
-        for (var y = this.min_y; y <= this.max_y; y++) {
-            for (var x = this.min_x; x <= this.max_x; x++) {
-                var key = x+','+y;
-                var value = this._store[key];
-                if (value === undefined)
-                    continue;
-                var result = fun(x, y, value);
-                if (result !== undefined) {
-                    if (result === ' ') {
-                        result = undefined;
-                    }
-                    this.put(x, y, result);
-                }
-            }
-        }
-    };
-};
-
+// requires playfield.js
 CanvasExperiment2 = function() {
     var p;
     var canvas;

javascript/non-roguelike.html

 <head>
   <meta charset="utf-8">
   <title>HTML5 Canvas experiment #9</title>
+  <script src="playfield.js"></script>
   <script src="non-roguelike.js"></script>
   <style>
 #canvas { border: 1px solid blue; }

javascript/non-roguelike.js

-Playfield = function() {
-    this._store = {};
-    this.min_x = undefined;
-    this.min_y = undefined;
-    this.max_x = undefined;
-    this.max_y = undefined;
-
-    this.get = function(x, y) {
-        return this._store[x+','+y];
-    };
-
-    this.put = function(x, y, value) {
-        if (this.min_x === undefined || x < this.min_x) this.min_x = x;
-        if (this.max_x === undefined || x > this.max_x) this.max_x = x;
-        if (this.min_y === undefined || y < this.min_y) this.min_y = y;
-        if (this.max_y === undefined || y > this.max_y) this.max_y = y;
-        if (value === undefined) {
-            delete this._store[x+','+y];
-        }
-        this._store[x+','+y] = value;
-    };
-          
-    this.foreach = function(fun) {
-        for (var y = this.min_y; y <= this.max_y; y++) {
-            for (var x = this.min_x; x <= this.max_x; x++) {
-                var key = x+','+y;
-                var value = this._store[key];
-                if (value === undefined)
-                    continue;
-                var result = fun(x, y, value);
-                if (result !== undefined) {
-                    if (result === ' ') {
-                        result = undefined;
-                    }
-                    this.put(x, y, result);
-                }
-            }
-        }
-    };
-
-    this.load = function(x, y, map, string) {
-        var lx = x;
-        var ly = y;
-        for (var i = 0; i < string.length; i++) {
-            var c = string.charAt(i);
-            if (c === '\n') {
-                lx = x;
-                ly++;
-            } else if (c === ' ') {
-                this.put(lx, ly, undefined);
-                lx++;
-            } else if (c === '\r') {
-            } else {
-                this.put(lx, ly, map[c]);
-                lx++;
-            }
-        }
-    };
-};
-
+// requires playfield.js
 NonRoguelike = function() {
     var p;
     var canvas;

javascript/playfield.js

+Playfield = function() {
+    this._store = {};
+    this.min_x = undefined;
+    this.min_y = undefined;
+    this.max_x = undefined;
+    this.max_y = undefined;
+
+    this.get = function(x, y) {
+        return this._store[x+','+y];
+    };
+
+    this.put = function(x, y, value) {
+        if (this.min_x === undefined || x < this.min_x) this.min_x = x;
+        if (this.max_x === undefined || x > this.max_x) this.max_x = x;
+        if (this.min_y === undefined || y < this.min_y) this.min_y = y;
+        if (this.max_y === undefined || y > this.max_y) this.max_y = y;
+        if (value === undefined) {
+            delete this._store[x+','+y];
+        }
+        this._store[x+','+y] = value;
+    };
+          
+    this.foreach = function(fun) {
+        for (var y = this.min_y; y <= this.max_y; y++) {
+            for (var x = this.min_x; x <= this.max_x; x++) {
+                var key = x+','+y;
+                var value = this._store[key];
+                if (value === undefined)
+                    continue;
+                var result = fun(x, y, value);
+                if (result !== undefined) {
+                    if (result === ' ') {
+                        result = undefined;
+                    }
+                    this.put(x, y, result);
+                }
+            }
+        }
+    };
+
+    this.load = function(x, y, map, string) {
+        var lx = x;
+        var ly = y;
+        for (var i = 0; i < string.length; i++) {
+            var c = string.charAt(i);
+            if (c === '\n') {
+                lx = x;
+                ly++;
+            } else if (c === ' ') {
+                this.put(lx, ly, undefined);
+                lx++;
+            } else if (c === '\r') {
+            } else {
+                this.put(lx, ly, map[c]);
+                lx++;
+            }
+        }
+    };
+};