diff --git a/src/game/game.js b/src/game/game.js index b06b15aa6f86ccb314e0ec8b6f53a26165e8abe0..49e0a5e70d24f04ff37a910ecd9c04fcbbdb7964 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -300,49 +300,52 @@ module.exports = { return gameBoard; }, - getPlayerView: (gameBoard, pid) => { - let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); - - let width = 11; - let height = 11; - - 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; - }, -}; + getPlayerView: (gameBoard, pid) => { + let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); + + let width = 11; + let height = 11; + let head = null; + let bodySize = 0; + + 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) { + head = cell; + } + if (cell.type === cellTypes.PLAYERBODY && cell.pid === pid) { + bodySize++; + } + } + } + + 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 the view along with head position and body size + return { + view: playerView, + headPosition: { x: head.x, y: head.y }, + bodySize: bodySize + 1 // Include head in body size + }; + } +}; \ No newline at end of file