From 5b57fd2e6418931a3be5274bd4ee20f5ebfefa3d Mon Sep 17 00:00:00 2001 From: elioat Date: Sat, 13 Apr 2024 10:50:35 -0400 Subject: MAGMA --- js/sand/sand.js | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'js') diff --git a/js/sand/sand.js b/js/sand/sand.js index 32975e7..b2dbb03 100644 --- a/js/sand/sand.js +++ b/js/sand/sand.js @@ -29,8 +29,8 @@ const grid = Array(gridHeight).fill().map(() => Array(gridWidth).fill(0)); canvas.addEventListener('touchstart', (e) => { mouseDown = true; - e.preventDefault(); // Prevent scrolling when touching the canvas - addSand(e.touches[0]); // Use the first touch point + e.preventDefault(); + addSand(e.touches[0]); }); canvas.addEventListener('touchend', () => { @@ -39,8 +39,8 @@ const grid = Array(gridHeight).fill().map(() => Array(gridWidth).fill(0)); canvas.addEventListener('touchmove', (e) => { if (mouseDown) { - e.preventDefault(); // Prevent scrolling when touching the canvas - addSand(e.touches[0]); // Use the first touch point + e.preventDefault(); + addSand(e.touches[0]); } }); @@ -75,34 +75,45 @@ function addPlayer() { let keys = new Set(); -const jumpHeight = 5; +const jumpHeight = 10; window.addEventListener('keydown', (e) => { + + // reset the canvas if (e.key === 'r') { playerAdded = false; for (let y = 0; y < gridHeight; y++) { for (let x = 0; x < gridWidth; x++) { - grid[y][x] = 0; // Set each cell in the grid to be empty + grid[y][x] = 0; } } - return; // Exit the function after resetting the grid + return; } + // move the character around 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) { + if ((e.key === 'ArrowLeft' || e.key === 'a') && 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) { + return; + } else if ((e.key === 'ArrowRight' || e.key === 'd') && 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 + return; + } else if ((e.key === ' ' || e.key === 'ArrowUp' || e.key === 'w') && (y === gridHeight - 1 || grid[y + 1][x] !== 0)) { + let jumpCount = 0; + function jump() { + if (jumpCount < jumpHeight && y - jumpCount >= 0 && grid[y - jumpCount - 1][x] === 0) { + grid[y - jumpCount][x] = 0; + grid[y - jumpCount - 1][x] = 3; + jumpCount++; + setTimeout(jump, 100); // delay in milliseconds + } + } + jump(); + return; } } } @@ -140,11 +151,11 @@ const updateGrid = () => { }; const drawGrid = () => { - // Clear the canvas + // clear the canvas ctx.fillStyle = 'beige'; ctx.fillRect(0, 0, canvas.width, canvas.height); - // Draw the sand and player + // draw the sand and player ctx.fillStyle = 'black'; for (let y = 0; y < gridHeight; y++) { for (let x = 0; x < gridWidth; x++) { @@ -159,7 +170,7 @@ const updateGrid = () => { } }; - // Draw the sand + // draw the sand ctx.fillStyle = 'black'; for (let y = 0; y < gridHeight; y++) { for (let x = 0; x < gridWidth; x++) { -- cgit 1.4.1-2-gfad0