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

Write initial api for game code

parent 296fffd6
No related branches found
No related tags found
1 merge request!8game.js update (addFood & moveOneStep)
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],
......
module.exports = {
cellTypes: {
EMPTY: 0,
PLAYERHEAD: 1,
PLAYERBODY: 2,
FOOD: 3,
},
createGameBoard: (width, height) => {
let gameBoard = [];
for (var i = 0; i < height; i++) {
row = [];
for (var j = 0; j < width; j++) {
row.push(createCell(j, i, cellTypes.EMPTY));
}
gameBoard.push(row);
}
return gameBoard;
},
addPlayer: (gameBoard, pid) => {
let x = getRandomInt(gameBoard[0].length);
let y = getRandomInt(gameBoard.length);
gameBoard[y][x].type = cellTypes.PLAYERHEAD;
gameBoard[y][x].pid = pid;
gameBoard[y][x].direction = 0;
let y2 = y - 1;
gameBoard[y2][x].type = cellTypes.PLAYERBODY;
gameBoard[y2][x].pid = pid;
gameBoard[y2][x].direction = 0;
gameBoard[y][x].next = gameBoard[y2][x];
return gameBoard;
},
moveOneStep: (gameBoard) => {
// Loop through board until we find a PLAYERHEAD
// Move the head in the direction it is facing
// if the head has a next, move the next to the head's old position
// if the next has a next, move the next's next to the next's old position
// and so on
// return the new gameBoard
},
checkColissions: (gameBoard) => {
// returns a list of players that have collided,
// and a new gameBoard with the players removed
},
};
let 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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment