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

Implement zoomed view

parent 36a43ce3
Branches
No related tags found
1 merge request!138 deadplayers
...@@ -302,7 +302,42 @@ module.exports = { ...@@ -302,7 +302,42 @@ module.exports = {
getPlayerView: (gameBoard, pid) => { getPlayerView: (gameBoard, pid) => {
let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell })));
// YANG: Find head with pid = pid
// return [x][y] around head let width = 5;
let height = 5;
let head = null;
for (var i = 0; i < updatedBoard.length; i++) {
for (var j = 0; j < updatedBoard[i].length; j++) {
let cell = updatedBoard[i][j];
if (cell.type === cellTypes.PLAYERHEAD && cell.pid === pid) {
console.log("MY Player head found at: ", cell.x, cell.y);
head = cell;
}
}
}
if (head === null) {
return [];
}
let playerView = [];
for (var i = 0; i <= height; i ++) {
let row = [];
for (var j = 0; j <= width; j ++) {
let x = head.x + j - Math.floor(width/2);
let y = head.y + i - Math.floor(height/2);
if (x < 0 || x >= updatedBoard[0].length || y < 0 || y >= updatedBoard.length) {
row.push(cellTypes.BORDER);
} else {
row.push(updatedBoard[y][x]);
}
}
playerView.push(row);
}
return playerView;
}, },
}; };
...@@ -27,7 +27,7 @@ class WebSocketModel { ...@@ -27,7 +27,7 @@ class WebSocketModel {
if (deadPlayers.length > 0) { if (deadPlayers.length > 0) {
for (let conn of this.games[game].players) { for (let conn of this.games[game].players) {
conn.send( conn.connection.send(
JSON.stringify({ JSON.stringify({
type: "deadPlayers", type: "deadPlayers",
data: deadPlayers, data: deadPlayers,
...@@ -37,14 +37,15 @@ class WebSocketModel { ...@@ -37,14 +37,15 @@ class WebSocketModel {
} }
for (let conn of this.games[game].players) { for (let conn of this.games[game].players) {
conn.send( let playerView = gameModule.getPlayerView(this.games[game].gameBoard, conn.pid);
conn.connection.send(
JSON.stringify({ JSON.stringify({
type: "gameBoard", type: "gameBoard",
data: this.games[game].gameBoard, // YANG: gameModule.getPlayerView()... data: playerView, // YANG: gameModule.getPlayerView()...
}), }),
); );
} }
}, 1000); }, 500);
} }
constructor() { constructor() {
this.connections = []; this.connections = [];
...@@ -148,13 +149,13 @@ class WebSocketModel { ...@@ -148,13 +149,13 @@ class WebSocketModel {
let roomId = connection.protocol; let roomId = connection.protocol;
if (!this.games[roomId].started) { if (!this.games[roomId].started) {
this.games[roomId].players.push(connection); this.games[roomId].players.push({connection, pid: this.games[roomId].players.length+1});
this.games[roomId].gameBoard = gameModule.addPlayer( this.games[roomId].gameBoard = gameModule.addPlayer(
this.games[roomId].gameBoard, this.games[roomId].gameBoard,
this.games[roomId].players.length, this.games[roomId].players.length,
); );
} else { } else {
this.games[roomId].players.push(connection); this.games[roomId].players.push({connection, pid: this.games[roomId].players.length+1});
} }
connection.send( connection.send(
...@@ -165,6 +166,7 @@ class WebSocketModel { ...@@ -165,6 +166,7 @@ class WebSocketModel {
); );
connection.on("close", () => { connection.on("close", () => {
// Need to fix this
this.games[roomId].players = this.games[roomId].players.filter( this.games[roomId].players = this.games[roomId].players.filter(
(curr) => curr !== this.games[roomId].players, (curr) => curr !== this.games[roomId].players,
); );
...@@ -182,7 +184,7 @@ class WebSocketModel { ...@@ -182,7 +184,7 @@ class WebSocketModel {
} else if (message.type === "chat") { } else if (message.type === "chat") {
console.log("Received chat: " + message.data); console.log("Received chat: " + message.data);
for (let conn of this.games[roomId].players) { for (let conn of this.games[roomId].players) {
conn.send( conn.connection.send(
JSON.stringify({ JSON.stringify({
type: "chat", type: "chat",
data: message.data, data: message.data,
...@@ -193,7 +195,7 @@ class WebSocketModel { ...@@ -193,7 +195,7 @@ class WebSocketModel {
} else if (message.type === "join") { } else if (message.type === "join") {
console.log("Received join: " + message.data); console.log("Received join: " + message.data);
for (let conn of this.games[roomId].players) { for (let conn of this.games[roomId].players) {
conn.send( conn.connection.send(
JSON.stringify({ JSON.stringify({
type: "playerJoined", type: "playerJoined",
data: message.data, data: message.data,
...@@ -207,8 +209,8 @@ class WebSocketModel { ...@@ -207,8 +209,8 @@ class WebSocketModel {
} else if (message.type === "start") { } else if (message.type === "start") {
console.log("Received start: " + message.data); console.log("Received start: " + message.data);
for (let conn of this.games[roomId].players) { for (let conn of this.games[roomId].players) {
if (conn !== connection) if (conn.connection !== connection)
conn.send( conn.connection.send(
JSON.stringify({ type: "playerReady", data: message.data }), JSON.stringify({ type: "playerReady", data: message.data }),
); );
} }
...@@ -218,7 +220,7 @@ class WebSocketModel { ...@@ -218,7 +220,7 @@ class WebSocketModel {
this.games[roomId].readyPlayers this.games[roomId].readyPlayers
) { ) {
for (let conn of this.games[roomId].players) { for (let conn of this.games[roomId].players) {
conn.send( conn.connection.send(
JSON.stringify({ type: "start", data: message.data }), JSON.stringify({ type: "start", data: message.data }),
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment