Skip to content
Snippets Groups Projects
Commit 36a43ce3 authored by Jake Dreher's avatar Jake Dreher
Browse files

Implement snake collision

parent f9a5fcba
Branches
No related tags found
1 merge request!138 deadplayers
......@@ -76,6 +76,10 @@ function checkCollisions(newY, newX, gameBoard) {
console.log(`Player has eaten the food!`);
return COLLISIONTYPES.FOOD;
}
if (gameBoard[newY][newX].type === cellTypes.PLAYERHEAD) {
console.log(`Player has collided with another player!`);
return COLLISIONTYPES.SNAKE;
}
//Handle collision with banana
if (gameBoard[newY][newX].type == cellTypes.BANANA) {
console.log("Slip");
......@@ -179,6 +183,23 @@ module.exports = {
) {
updatedBoard = addFood(updatedBoard);
}
if (collision === COLLISIONTYPES.SNAKE) {
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
deadPlayers.push(cell.pid);
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === gameBoard[newY][newX].pid
? createCell(c.x, c.y, cellTypes.EMPTY)
: c,
),
);
deadPlayers.push(gameBoard[newY][newX].pid);
continue;
}
if (collision > COLLISIONTYPES.FOOD) {
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment