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