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

Fix movement

parent b71af2f6
No related branches found
No related tags found
1 merge request!9Resolve "Define Object for Cell"
......@@ -68,16 +68,21 @@ module.exports = {
if (gameBoard[y][x].type === cellTypes.EMPTY) {
gameBoard[y][x].type = cellTypes.PLAYERHEAD;
gameBoard[y][x].pid = pid;
gameBoard[y][x].direction = 0;
let y2 = y - 1;
if (y2 >= 0 && gameBoard[y2][x].type === cellTypes.EMPTY) {
gameBoard[y2][x].type = cellTypes.PLAYERBODY;
gameBoard[y2][x].pid = pid;
gameBoard[y2][x].direction = 0;
gameBoard[y][x].next = gameBoard[y2][x];
let bodyPlaced = false;
if (y - 1 >= 0 && gameBoard[y - 1][x].type === cellTypes.EMPTY) {
// Place body segment
gameBoard[y - 1][x] = createCell(x, y - 1, cellTypes.PLAYERBODY);
gameBoard[y - 1][x].pid = pid;
gameBoard[y][x].next = gameBoard[y - 1][x];
bodyPlaced = true;
}
if (bodyPlaced) {
placedCell = true;
} else {
gameBoard[y][x] = createCell(x, y, cellTypes.EMPTY);
}
}
}
//console.log(gameBoard);
......@@ -95,6 +100,8 @@ module.exports = {
let newX = oldX;
let newY = oldY;
let tempVar = cell.direction;
//New position based on direction
if (cell.direction === 0) {
// Up
......@@ -118,17 +125,15 @@ module.exports = {
newY < gameBoard.length
) {
//Snake head to new position by updating cell object
gameBoard[newY][newX] = {
...cell,
x: newX,
y: newY,
};
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].type = cellTypes.EMPTY;
gameBoard[oldY][oldX].pid = 0;
gameBoard[oldY][oldX].direction = 0;
gameBoard[oldY][oldX].next = null;
gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
//Snake body to previous locations
let currentBody = gameBoard[newY][newX].next;
......@@ -149,10 +154,7 @@ module.exports = {
}
//Remove previous body position
gameBoard[oldY][oldX].type = cellTypes.EMPTY;
gameBoard[oldY][oldX].pid = 0;
gameBoard[oldY][oldX].direction = 0;
gameBoard[oldY][oldX].next = null;
gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
console.log(gameBoard);
return gameBoard;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment