diff --git a/src/game/game.js b/src/game/game.js index 4210b6efa7cff5c325eabbee82393e995ebd94e5..b14a5da13a0517135dff80b53dc7dfd47c7b8bc8 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -90,7 +90,7 @@ module.exports = { }, moveOneStep: (gameBoard) => { - //Loop through board until we find a PLAYERHEAD + // 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++) { let cell = gameBoard[i][j]; @@ -99,10 +99,10 @@ module.exports = { let oldY = cell.y; let newX = oldX; let newY = oldY; - - let tempVar = cell.direction; - //New position based on direction + let tempVar = cell.direction; + + // New position based on direction if (cell.direction === 0) { // Up newY -= 1; @@ -117,48 +117,67 @@ module.exports = { newX -= 1; } - //Continue if within bounds + // Check for collisions with walls if ( - newX >= 0 && - newX < gameBoard[0].length && - newY >= 0 && - newY < gameBoard.length + newX < 0 || + newX >= gameBoard[0].length || + newY < 0 || + newY >= gameBoard.length ) { - //Snake head to new position by updating cell object - let newHeadCell = createCell(newX, newY, cellTypes.PLAYERHEAD); - newHeadCell.pid = cell.pid; - gameBoard[newY][newX] = newHeadCell; - - newHeadCell.direction = tempVar; - gameBoard[newY][newX] = newHeadCell; - - //Remove previous head position - gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); + console.log(`Player ${cell.pid} has collided with the wall!`); + // Remove the player from the game (indicating death) + return gameBoard.map(row => + row.map(c => (c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c)) + ); + } - //Snake body to previous locations - let currentBody = gameBoard[newY][newX].next; - while (currentBody) { - let bodyOldX = currentBody.x; - let bodyOldY = currentBody.y; - currentBody.x = oldX; - currentBody.y = oldY; - - //Update cell body position - gameBoard[oldY][oldX] = { - ...currentBody, - type: cellTypes.PLAYERBODY, - }; - oldX = bodyOldX; - oldY = bodyOldY; - currentBody = currentBody.next; - } - - //Remove previous body position - gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); + // Handle collision with food + if (gameBoard[newY][newX].type === cellTypes.FOOD) { + console.log(`Player ${cell.pid} has eaten the food!`); + // Add new body segment at the current head's position + let newBodySegment = createCell(oldX, oldY, cellTypes.PLAYERBODY); + newBodySegment.pid = cell.pid; + newBodySegment.next = cell.next; + cell.next = newBodySegment; + // Place new food somewhere else + gameBoard = addFood(gameBoard); + } - console.log(gameBoard); - return gameBoard; + // 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; + + // Remove previous head position + gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); + + // Move the body segments + let currentBody = newHeadCell.next; + while (currentBody) { + let bodyOldX = currentBody.x; + let bodyOldY = currentBody.y; + currentBody.x = oldX; + currentBody.y = oldY; + + // Update cell body position + gameBoard[oldY][oldX] = { + ...currentBody, + type: cellTypes.PLAYERBODY, + }; + oldX = bodyOldX; + oldY = bodyOldY; + currentBody = currentBody.next; } + + // 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); + } + + console.log(gameBoard); + return gameBoard; } } }