diff --git a/app.js b/app.js index 373acfe968e1789379205d30f75d449793096858..4d2cfa6fb9d3e54d444758f14d6bc101dd5f9630 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,6 @@ const express = require("express"); const { WebSocketServer } = require("ws"); +const gameModule = require("./src/game/game.js"); const webserver = express() .use((_, res) => res.sendFile("/index.html", { root: `${__dirname}/public` })) @@ -11,6 +12,14 @@ const sockserver = new WebSocketServer({ port: 3001 }); // This will eventually map connections to lobby or game instances let connections = []; +let games = {}; + +// TO BE REWRITTEN +games["game1"] = { + gameBoard: gameModule.createGameBoard(100, 100), + players: [], +}; + let gameBoard = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], diff --git a/src/game/game.js b/src/game/game.js new file mode 100644 index 0000000000000000000000000000000000000000..d33f57ae86740e116d4e933033660d01b51906a5 --- /dev/null +++ b/src/game/game.js @@ -0,0 +1,162 @@ +const cellTypes = { + EMPTY: 0, + PLAYERHEAD: 1, + PLAYERBODY: 2, + FOOD: 3, +}; + +function createCell(x, y, type) { + return { + x: x, + y: y, + type: type, + pid: 0, + direction: 0, + next: null, + }; +} + +function getRandomInt(max) { + return Math.floor(Math.random() * max); +} + +function addFood(gameBoard) { + //Find all the empty spaces on the grid + let emptySpaces = []; + for (let row of gameBoard) { + for (let cell of row) { + if (cell.type === cellTypes.EMPTY) { + emptySpaces.push(cell); + } + } + } + + //Create food cells randomly + if (emptySpaces.length > 0) { + let randomCell = emptySpaces[getRandomInt(emptySpaces.length)]; + randomCell.type = cellTypes.FOOD; + } + + return gameBoard; +} + +module.exports = { + cellTypes, + + createGameBoard: (width, height) => { + let gameBoard = []; + for (var i = 0; i < height; i++) { + let row = []; + for (var j = 0; j < width; j++) { + row.push(createCell(j, i, cellTypes.EMPTY)); + } + gameBoard.push(row); + } + for (var i = 0; i < 10; i++) { + gameBoard = addFood(gameBoard); + } + //console.log(gameBoard); + return gameBoard; + }, + + addPlayer: (gameBoard, pid) => { + let placedCell = false; + while (!placedCell) { + let x = getRandomInt(gameBoard[0].length); + let y = getRandomInt(gameBoard.length); + + 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]; + } + placedCell = true; + } + } + //console.log(gameBoard); + return gameBoard; + }, + + moveOneStep: (gameBoard) => { + //Loop through board until we find a PLAYERHEAD + for (var i = 0; i < gameBoard.length; i++) { + for (var j = 0; j < gameBoard[i].length; j++) { + let cell = gameBoard[i][j]; + if (cell.type === cellTypes.PLAYERHEAD) { + let oldX = cell.x; + let oldY = cell.y; + let newX = oldX; + let newY = oldY; + + //New position based on direction + if (cell.direction === 0) { // Up + newY -= 1; + } else if (cell.direction === 1) { // Right + newX += 1; + } else if (cell.direction === 2) { // Down + newY += 1; + } else if (cell.direction === 3) { // Left + newX -= 1; + } + + //Continue if within bounds + if (newX >= 0 && newX < gameBoard[0].length && newY >= 0 && newY < gameBoard.length) { + + //Snake head to new position by updating cell object + gameBoard[newY][newX] = { + ...cell, + x: newX, + y: newY, + }; + + //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; + + //Snake body to previous locations + let currentBody = gameBoard[newY][newX].next; + while (currentBody) { + let bodyOldX = currentBody.x; + let bodyOldY = currentBody.y; + currentBody.x = oldX; + currentBody.y = oldY; + + //Update cell body position + gameBoard[oldY][oldX] = { + ...currentBody, + type: cellTypes.PLAYERBODY, + }; + oldX = bodyOldX; + oldY = bodyOldY; + currentBody = currentBody.next; + } + + //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; + + //console.log(gameBoard); + return gameBoard; + } + } + } + } + return gameBoard; + }, + + checkCollisions: (gameBoard) => { + // returns a list of players that have collided, + // and a new gameBoard with the players removed + }, +};