about summary refs log tree commit diff stats
path: root/html/broughlike/broughlike.js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-11-01 21:04:36 -0400
committerelioat <elioat@tilde.institute>2024-11-01 21:04:36 -0400
commit5951c7d3d477c6498ee4a55006f92a84ff7e37f2 (patch)
tree6493061db4a499645146d0b30b8f14cb48b8b662 /html/broughlike/broughlike.js
parent3b607e7e3c2364613f6d1f1b41ad4ca16d0c98fb (diff)
downloadtour-5951c7d3d477c6498ee4a55006f92a84ff7e37f2.tar.gz
*
Diffstat (limited to 'html/broughlike/broughlike.js')
-rw-r--r--html/broughlike/broughlike.js39
1 files changed, 14 insertions, 25 deletions
diff --git a/html/broughlike/broughlike.js b/html/broughlike/broughlike.js
index 8933e31..0397f9b 100644
--- a/html/broughlike/broughlike.js
+++ b/html/broughlike/broughlike.js
@@ -1,10 +1,14 @@
 import { CONFIG, COLORS } from './config.js';
 
-// FIXME: canvas, ctx, and tileSize are all globally available and regularly accessed. 
-// I'd like to refactor this to be more modular so that these can all be contained to the CONFIG object.
-const canvas = document.getElementById('gameCanvas');
-const ctx = canvas.getContext('2d');
-let tileSize = canvas.width / CONFIG.GRID_SIZE;
+// FIXME: canvas, ctx, and tileSize are all regularly accessed. 
+// I'd like to refactor this to be more modular so that these can all be contained to the CONFIG object or something similar.
+let { ctx, canvas, tileSize } = initializeCanvasContext();
+function initializeCanvasContext() {
+    const canvas = document.getElementById('gameCanvas');
+    const ctx = canvas.getContext('2d');
+    let tileSize = canvas.width / CONFIG.GRID_SIZE;
+    return { ctx, canvas, tileSize };
+}
 
 let highScore = localStorage.getItem('highScore') || 0;
 const player = {
@@ -112,7 +116,7 @@ function generateWallsNaive() {
         }
     }
 
-    if (!isPassable()) {
+    if (!isReachable(player.x, player.y, exit.x, exit.y)) {
         generateWallsNaive();
     }
 }
@@ -144,7 +148,7 @@ function generateWallsDrunkardsWalk() {
     }
     }
 
-    if (!isPassable()) {
+    if (!isReachable(player.x, player.y, exit.x, exit.y)) {
         generateWallsDrunkardsWalk();
     }
 }
@@ -199,7 +203,7 @@ function generateWallsCellularAutomata() {
         }
     }
 
-    if (!isPassable()) {
+    if (!isReachable(player.x, player.y, exit.x, exit.y)) {
         generateWallsCellularAutomata();
     }
 }
@@ -270,7 +274,7 @@ function generateWallsRSP() {
         }
     }});
 
-    if (!isPassable()) {
+    if (!isReachable(player.x, player.y, exit.x, exit.y)) {
         generateWallsRSP();
     }
 }
@@ -309,6 +313,7 @@ function generateItems() {
     }
 }
 
+// Checks to see if there's a path between any two points on the level
 function isReachable(startX, startY, targetX, targetY) {
     const visited = Array(CONFIG.GRID_SIZE).fill().map(() => Array(CONFIG.GRID_SIZE).fill(false)); // Initialize a 2D array of false values
     function dfs(x, y) {
@@ -322,22 +327,6 @@ function isReachable(startX, startY, targetX, targetY) {
     return dfs(startX, startY);
 }
 
-// This function is used to check if the player can reach the exit after generating the level
-// It is almost the same as isReachable, but it checks for the exit instead of an arbitrary target
-// I could defo use isReachable for this, but I...am not doing that right now.
-function isPassable() {
-    const visited = Array(CONFIG.GRID_SIZE).fill().map(() => Array(CONFIG.GRID_SIZE).fill(false)); // Initialize a 2D array of false values
-    function dfs(x, y) {
-        if (x < 0 || x >= CONFIG.GRID_SIZE || y < 0 || y >= CONFIG.GRID_SIZE) return false; // Are the coordinates in bounds?
-        if (visited[x][y]) return false;                                                    // Have we already visited this cell?
-        if (walls.some(wall => wall.x === x && wall.y === y)) return false;                 // Is there a wall here?
-        visited[x][y] = true;                                                               // Mark this cell as visited
-        if (x === exit.x && y === exit.y) return true;                                      // Have we reached the exit?
-        return dfs(x + 1, y) || dfs(x - 1, y) || dfs(x, y + 1) || dfs(x, y - 1);            // Recursively check neighbors
-    }
-    return dfs(player.x, player.y);
-}
-
 function drawGrid() {
     ctx.clearRect(0, 0, canvas.width, canvas.height);
     ctx.lineWidth = 2;