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

Game Border Code

parent 542dcc26
Branches
No related tags found
1 merge request!138 deadplayers
......@@ -4,6 +4,7 @@ const cellTypes = {
PLAYERBODY: 2,
FOOD: 3,
BANANA: 4,
BORDER: 5,
};
function createCell(x, y, type) {
......@@ -172,6 +173,17 @@ module.exports = {
continue;
}
//Handle collision with border
if (gameBoard[newY][newX].type == cellTypes.BORDER) {
console.log("Void");
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
continue;
}
//Snake head to new position by updating cell object
let newHeadCell = createCell(newX, newY, cellTypes.PLAYERHEAD);
newHeadCell.pid = cell.pid;
......@@ -210,6 +222,52 @@ module.exports = {
return updatedBoard;
},
applyBorders: (gameBoard, borderCounter) => {
let height = gameBoard.length;
let width = gameBoard[0].length;
//console.log(borderCounter);
//Return gameBoard when it is 2x2
if (borderCounter * 2 >= (Math.min(height, width)) - 2) {
return gameBoard;
}
//Top
for (let x = 0; x < width; x++) {
gameBoard[borderCounter][x].type = cellTypes.BORDER;
gameBoard[borderCounter][x].pid = 0;
gameBoard[borderCounter][x].direction = 0;
gameBoard[borderCounter][x].next = null;
}
//Bottom
for (let x = 0; x < width; x++) {
gameBoard[height - borderCounter - 1][x].type = cellTypes.BORDER;
gameBoard[height - borderCounter - 1][x].pid = 0;
gameBoard[height - borderCounter - 1][x].direction = 0;
gameBoard[height - borderCounter - 1][x].next = null;
}
//Left
for (let y = 0; y < height; y++) {
gameBoard[y][borderCounter].type = cellTypes.BORDER;
gameBoard[y][borderCounter].pid = 0;
gameBoard[y][borderCounter].direction = 0;
gameBoard[y][borderCounter].next = null;
}
//Right
for (let y = 0; y < height; y++) {
gameBoard[y][width - borderCounter - 1].type = cellTypes.BORDER;
gameBoard[y][width - borderCounter - 1].pid = 0;
gameBoard[y][width - borderCounter - 1].direction = 0;
gameBoard[y][width - borderCounter - 1].next = null;
}
return gameBoard;
},
getPlayerView: (gameBoard, pid) => {
let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell })));
// YANG: Find head with pid = pid
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment