Skip to content
Snippets Groups Projects
Commit b57b2cda authored by zm343's avatar zm343
Browse files

Fixed multiplayer movement + Added banana collision

parent 09f0b9b7
No related branches found
No related tags found
1 merge request!10Update game.js for collision & growing features
......@@ -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,7 +152,18 @@ 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
......@@ -148,10 +171,10 @@ module.exports = {
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) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment