make gameState a class

This commit is contained in:
Thorsten Rausch 2021-06-16 10:44:21 +02:00
parent f022f17186
commit d60bbbcf7d

View File

@ -13,6 +13,7 @@ class Player {
this.position = 0; this.position = 0;
this.isAlive = true; this.isAlive = true;
} }
move(amount) { move(amount) {
this.position += amount; this.position += amount;
if (this.position === 15) { if (this.position === 15) {
@ -25,9 +26,11 @@ class Hunter {
constructor() { constructor() {
this.position = 0; this.position = 0;
} }
move(amount) { move(amount) {
this.position += amount; this.position += amount;
} }
hunt(playerArray) { hunt(playerArray) {
for (let i = 0; i < playerArray.length; i++) { for (let i = 0; i < playerArray.length; i++) {
if (playerArray[i].position <= this.position) { if (playerArray[i].position <= this.position) {
@ -37,33 +40,37 @@ class Hunter {
} }
} }
let gameState = { class Game {
players: [], constructor() {
whosNext: 0, this.players = [];
started: false, this.whosNext = 0;
round: 0, this.started = false;
hunter: new Hunter() this.round = 0;
}; this.hunter = new Hunter()
function finish_turn() {
// move on to next player; skip dead players
do {
gameState.whosNext++;
if (gameState.whosNext === gameState.players.length) {
gameState.whosNext = 0;
gameState.round++;
}
} while (!gameState.players[gameState.whosNext].isAlive);
// kill players with hunter
if (gameState.round >= 5) {
gameState.hunter.move(1);
gameState.hunter.hunt(gameState.players);
} }
// check if all players are dead
if (!gameState.players.some(player => player.isAlive === true)) { finish_turn() {
// todo: end game (all players are dead) // move on to next player; skip dead players
do {
this.whosNext++;
if (this.whosNext === this.players.length) {
this.whosNext = 0;
this.round++;
}
} while (!gameState.players[gameState.whosNext].isAlive);
// kill players with hunter
if (this.round >= 5) {
this.hunter.move(1);
this.hunter.hunt(this.players);
}
// check if all players are dead
if (!this.players.some(player => player.isAlive === true)) {
// todo: end game (all players are dead)
}
} }
} }
// todo: instantiate this for individual rooms
let gameState = new Game();
let port = 5000; let port = 5000;
server.listen(port, function () { server.listen(port, function () {
@ -149,7 +156,6 @@ io.on('connection', socket => {
generate_log_message(socket.room, socket.username, "LEFT", ""); generate_log_message(socket.room, socket.username, "LEFT", "");
}); });
// Game // Game
socket.on('roll dice', function () { socket.on('roll dice', function () {
@ -179,7 +185,7 @@ io.on('connection', socket => {
socket.on('card finished', function (difficulty, answerIsCorrect) { socket.on('card finished', function (difficulty, answerIsCorrect) {
if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty); if (answerIsCorrect) gameState.players[gameState.whosNext].move(difficulty);
io.in(socket.room).emit('card destroyed'); io.in(socket.room).emit('card destroyed');
finish_turn(); gameState.finish_turn();
}); });
}); });