From: Michael D. Lowis Date: Thu, 15 Nov 2018 15:59:12 +0000 (-0500) Subject: changed map to be a closure instead of an object X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=653196f75c0a7f9b870b0aaadaa46b42c944e1e0;p=proto%2Fjstoys.git changed map to be a closure instead of an object --- diff --git a/raycast.html b/raycast.html index 90c3782..30c5d88 100644 --- a/raycast.html +++ b/raycast.html @@ -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); }