about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-04-13 10:07:41 -0400
committerelioat <elioat@tilde.institute>2024-04-13 10:07:41 -0400
commitee976c7842a387a7dd293ff62b4b2c0bb699e2a8 (patch)
tree90bddfd0e832dacb2f5d56e742c23146ce294dc5
parent679c9db5cf03299c45e6afe186b26c6a74b08d83 (diff)
downloadtour-ee976c7842a387a7dd293ff62b4b2c0bb699e2a8.tar.gz
player character to sand
-rw-r--r--js/sand/sand.js101
1 files changed, 86 insertions, 15 deletions
diff --git a/js/sand/sand.js b/js/sand/sand.js
index dfe6d80..32975e7 100644
--- a/js/sand/sand.js
+++ b/js/sand/sand.js
@@ -53,36 +53,107 @@ const grid = Array(gridHeight).fill().map(() => Array(gridWidth).fill(0));
         }
     }
 
-    const updateGrid = () => {
-        for (let y = gridHeight - 2; y >= 0; y--) {
+    let playerAdded = false;
+
+window.addEventListener('keydown', (e) => {
+    if (e.key === ' ' && !playerAdded) {
+        addPlayer();
+        playerAdded = true;
+    }
+});
+
+function addPlayer() {
+    for (let y = 0; y < gridHeight; y++) {
+        for (let x = 0; x < gridWidth; x++) {
+            if (grid[y][x] === 1) {
+                grid[Math.max(0, y - 10)][x] = 3;
+                return;
+            }
+        }
+    }
+}
+
+let keys = new Set();
+
+const jumpHeight = 5;
+
+window.addEventListener('keydown', (e) => {
+    if (e.key === 'r') {
+        playerAdded = false;
+        for (let y = 0; y < gridHeight; y++) {
             for (let x = 0; x < gridWidth; x++) {
-                if (grid[y][x] === 1) {
-                    if (grid[y + 1][x] === 0) {
-                        grid[y][x] = 0;
-                        grid[y + 1][x] = 1;
-                    } else if (x > 0 && grid[y + 1][x - 1] === 0) {
-                        grid[y][x] = 0;
-                        grid[y + 1][x - 1] = 1;
-                    } else if (x < gridWidth - 1 && grid[y + 1][x + 1] === 0) {
-                        grid[y][x] = 0;
-                        grid[y + 1][x + 1] = 1;
-                    }
+                grid[y][x] = 0; // Set each cell in the grid to be empty
+            }
+        }
+        return; // Exit the function after resetting the grid
+    }
+    
+    for (let y = 0; y < gridHeight; y++) {
+        for (let x = 0; x < gridWidth; x++) {
+            if (grid[y][x] === 3) {
+                if (e.key === 'ArrowLeft' && x > 0 && grid[y][x - 1] === 0) {
+                    grid[y][x] = 0;
+                    grid[y][x - 1] = 3;
+                    return; // Exit the function after moving the player
+                } else if (e.key === 'ArrowRight' && x < gridWidth - 1 && grid[y][x + 1] === 0) {
+                    grid[y][x] = 0;
+                    grid[y][x + 1] = 3;
+                    return; // Exit the function after moving the player
+                } else if ((e.key === ' ' || e.key === 'ArrowUp') && y >= jumpHeight && grid[y - 1][x] === 0 && grid[y - 2][x] === 0 && grid[y - 3][x] === 0) {
+                    grid[y][x] = 0;
+                    grid[y - jumpHeight][x] = 3;
+                    return; // Exit the function after moving the player
                 }
             }
         }
-    };
+    }
+});
+
+window.addEventListener('keyup', (e) => {
+    keys.delete(e.key);
+});
+
+const updateGrid = () => {
+    for (let y = gridHeight - 2; y >= 0; y--) {
+        for (let x = 0; x < gridWidth; x++) {
+            if (grid[y][x] === 1) {
+                if (grid[y + 1][x] === 0) {
+                    grid[y][x] = 0;
+                    grid[y + 1][x] = 1;
+                } else if (x > 0 && grid[y + 1][x - 1] === 0) {
+                    grid[y][x] = 0;
+                    grid[y + 1][x - 1] = 1;
+                } else if (x < gridWidth - 1 && grid[y + 1][x + 1] === 0) {
+                    grid[y][x] = 0;
+                    grid[y + 1][x + 1] = 1;
+                }
+            } else if (grid[y][x] === 3) {
+                if (y < gridHeight - 1 && grid[y + 1][x] === 0) {
+                    grid[y][x] = 0;
+                    grid[y + 1][x] = 3;
+                }
+            }
+
+            
+        }
+    }
+};
 
     const drawGrid = () => {
         // Clear the canvas
         ctx.fillStyle = 'beige';
         ctx.fillRect(0, 0, canvas.width, canvas.height);
 
-        // Draw the sand
+        // Draw the sand and player
         ctx.fillStyle = 'black';
         for (let y = 0; y < gridHeight; y++) {
             for (let x = 0; x < gridWidth; x++) {
                 if (grid[y][x] === 1) {
                     ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
+                } else if (grid[y][x] === 3) {
+                    ctx.fillStyle = 'red';
+                    ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
+                    ctx.fillStyle = 'black'; // Reset fill color to black after drawing the player
                 }
             }
         }