]> git.mdlowis.com Git - proto/jstoys.git/commitdiff
changed map to be a closure instead of an object master
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 15 Nov 2018 15:59:12 +0000 (10:59 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 15 Nov 2018 15:59:12 +0000 (10:59 -0500)
raycast.html

index 90c3782b18d7298e331ce81c6ba470bdd681cab1..30c5d8849fbaf442d14e98dbbf884a7099698a9d 100644 (file)
@@ -43,14 +43,19 @@ const Controls = ((self = {})=>{
     return self;
 })();
 
+const mapGet = ((grid)=>{
+    grid = new Uint8Array(MAP_SIZE ** 2).map((_,i)=>(Math.random() < 0.3 ? 1 : 0));
+    return (x, y)=>(grid[Math.floor(y) * MAP_SIZE + Math.floor(x)] || -1);
+})();
+
 const Player = ((self = START_POS)=>{
     const rotate = (angle)=>(self.direction = (self.direction + angle + TAU) % TAU);
 
     const walk = (distance)=>{
         const dx = Math.cos(self.direction) * distance;
         const dy = Math.sin(self.direction) * distance;
-        if (Map.get(self.x + dx, self.y) <= 0) self.x += dx;
-        if (Map.get(self.x, self.y + dy) <= 0) self.y += dy;
+        if (mapGet(self.x + dx, self.y) <= 0) self.x += dx;
+        if (mapGet(self.x, self.y + dy) <= 0) self.y += dy;
     };
 
     self.update = (seconds)=>{
@@ -63,11 +68,6 @@ const Player = ((self = START_POS)=>{
     return self;
 })();
 
-const Map = ((grid = new Uint8Array(MAP_SIZE ** 2))=>{
-    grid = grid.map((i) => (Math.random() < 0.3 ? 1 : 0));
-    return { get: (x, y)=>(grid[Math.floor(y) * MAP_SIZE + Math.floor(x)] || -1) };
-})();
-
 const Camera = ((self = {}, columns = [], ceiling = null, floor = null)=>{
     const resizeView = ()=>{
         if (self.width != window.innerWidth || self.height != window.innerHeight) {
@@ -142,7 +142,7 @@ const Camera = ((self = {}, columns = [], ceiling = null, floor = null)=>{
                 stepX.x = origin.x + dx;
                 stepX.y = origin.y + dy;
                 stepX.length2 = dx * dx + dy * dy;
-                stepX.height = Map.get(stepX.x - (data.cos < 0 ? 1 : 0), stepX.y);
+                stepX.height = mapGet(stepX.x - (data.cos < 0 ? 1 : 0), stepX.y);
                 stepX.distance = origin.distance + Math.sqrt(stepX.length2);
             }
 
@@ -151,7 +151,7 @@ const Camera = ((self = {}, columns = [], ceiling = null, floor = null)=>{
                 stepY.x = origin.x + dy;
                 stepY.y = origin.y + dx;
                 stepY.length2 = dx * dx + dy * dy;
-                stepY.height = Map.get(stepY.x, stepY.y - (data.sin < 0 ? 1 : 0));
+                stepY.height = mapGet(stepY.x, stepY.y - (data.sin < 0 ? 1 : 0));
                 stepY.distance = origin.distance + Math.sqrt(stepY.length2);
             }