From b57b2cda1810cc8ad9a8462b52683bbcd5eed0a4 Mon Sep 17 00:00:00 2001 From: zm343 <2639-zm343@users.noreply.gitlab.cci.drexel.edu> Date: Sat, 24 Aug 2024 16:33:19 +0000 Subject: [PATCH] Fixed multiplayer movement + Added banana collision --- src/game/game.js | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/game/game.js b/src/game/game.js index b14a5da..1087a46 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -3,6 +3,7 @@ const cellTypes = { PLAYERHEAD: 1, PLAYERBODY: 2, FOOD: 3, + BANANA: 4 }; function createCell(x, y, type) { @@ -31,12 +32,19 @@ function addFood(gameBoard) { } } + let bananaExists = gameBoard.some(row => row.some(cell => cell.type === cellTypes.BANANA)); + //Create food cells randomly if (emptySpaces.length > 0) { let randomCell = emptySpaces[getRandomInt(emptySpaces.length)]; randomCell.type = cellTypes.FOOD; } + if (!bananaExists && emptySpaces.length > 0) { + let randomCell = emptySpaces[getRandomInt(emptySpaces.length)]; + randomCell.type = cellTypes.BANANA; + } + return gameBoard; } @@ -90,6 +98,9 @@ module.exports = { }, moveOneStep: (gameBoard) => { + // Save board state to allow multiple snake movements + let updatedBoard = gameBoard.map(row => row.map(cell => ({ ...cell }))); + // Loop through board until we find a PLAYERHEAD for (var i = 0; i < gameBoard.length; i++) { for (var j = 0; j < gameBoard[i].length; j++) { @@ -126,9 +137,10 @@ module.exports = { ) { console.log(`Player ${cell.pid} has collided with the wall!`); // Remove the player from the game (indicating death) - return gameBoard.map(row => + updatedBoard = updatedBoard.map(row => row.map(c => (c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c)) ); + continue; } // Handle collision with food @@ -140,18 +152,29 @@ module.exports = { newBodySegment.next = cell.next; cell.next = newBodySegment; // Place new food somewhere else - gameBoard = addFood(gameBoard); + updatedBoard = addFood(updatedBoard); } + // Handle collision with banana + if (gameBoard[newY][newX].type == cellTypes.BANANA) { + console.log('Slip'); + updatedBoard[newY][newX] = createCell(newX, newY, cellTypes.EMPTY); + updatedBoard = updatedBoard.map(row => + row.map(c => (c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c)) + ); + updatedBoard = addFood(updatedBoard); + continue; + } + // Snake head to new position by updating cell object let newHeadCell = createCell(newX, newY, cellTypes.PLAYERHEAD); newHeadCell.pid = cell.pid; newHeadCell.direction = tempVar; newHeadCell.next = cell.next; - gameBoard[newY][newX] = newHeadCell; + updatedBoard[newY][newX] = newHeadCell; // Remove previous head position - gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); + updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); // Move the body segments let currentBody = newHeadCell.next; @@ -162,7 +185,7 @@ module.exports = { currentBody.y = oldY; // Update cell body position - gameBoard[oldY][oldX] = { + updatedBoard[oldY][oldX] = { ...currentBody, type: cellTypes.PLAYERBODY, }; @@ -173,15 +196,12 @@ module.exports = { // Remove previous body position if it's no longer part of the snake if (oldX !== newHeadCell.x || oldY !== newHeadCell.y) { - gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); + updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); } - - console.log(gameBoard); - return gameBoard; } } } - return gameBoard; + return updatedBoard; }, checkCollisions: (gameBoard) => { -- GitLab