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

Fix movement

parent cf55278e
Branches
No related tags found
1 merge request!9Resolve "Define Object for Cell"
......@@ -67,39 +67,31 @@ let moveHandler = (connection, message) => {
console.log("Received move: " + message.data);
let movement = message.data;
let pid = message.pid;
let gameBoard = games[connection.protocol].gameBoard;
console.log("pid: ", pid);
for (var i = 0; i < gameBoard.length; i++) {
for (var j = 0; j < gameBoard[0].length; j++) {
if (movement === "up" && gameBoard[i][j] === pid && i > 0) {
gameBoard[i][j] = 0;
gameBoard[i - 1][j] = pid;
return;
}
if (
movement === "down" &&
gameBoard[i][j] === pid &&
i < gameBoard.length - 1
) {
gameBoard[i][j] = 0;
gameBoard[i + 1][j] = pid;
return;
if (gameBoard[i][j].type === 1 && gameBoard[i][j].pid === pid) {
console.log("Updating direction to: " + movement);
switch (movement) {
case "up":
gameBoard[i][j].direction = 0;
break;
case "right":
console.log("1");
console.log(gameBoard[i][j]);
gameBoard[i][j].direction = 1;
cell.direction = 1;
break;
case "down":
gameBoard[i][j].direction = 2;
break;
case "left":
gameBoard[i][j].direction = 3;
break;
}
if (movement === "left" && gameBoard[i][j] === pid && j > 0) {
gameBoard[i][j] = 0;
gameBoard[i][j - 1] = pid;
return;
}
if (
movement === "right" &&
gameBoard[i][j] === pid &&
j < gameBoard[0].length - 1
) {
gameBoard[i][j] = 0;
gameBoard[i][j + 1] = pid;
console.log("New direction set to: " + gameBoard[i][j].direction);
return;
}
}
......@@ -136,6 +128,7 @@ sockserver.on("connection", (connection) => {
// All messages are expected to have a type
if (message.type === "move") {
console.log("Received move: " + message.data);
moveHandler(connection, message);
} else if (message.type === "chat") {
console.log("Received chat: " + message.data);
} else if (message.type === "join") {
......@@ -146,7 +139,7 @@ sockserver.on("connection", (connection) => {
console.log("Received: " + message);
}
} catch (e) {
console.log("Error parsing JSON: " + message);
console.log("Error parsing JSON: " + e.message);
}
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment