make gameState a class
This commit is contained in:
parent
f022f17186
commit
d60bbbcf7d
@ -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() {
|
finish_turn() {
|
||||||
// move on to next player; skip dead players
|
// move on to next player; skip dead players
|
||||||
do {
|
do {
|
||||||
gameState.whosNext++;
|
this.whosNext++;
|
||||||
if (gameState.whosNext === gameState.players.length) {
|
if (this.whosNext === this.players.length) {
|
||||||
gameState.whosNext = 0;
|
this.whosNext = 0;
|
||||||
gameState.round++;
|
this.round++;
|
||||||
}
|
}
|
||||||
} while (!gameState.players[gameState.whosNext].isAlive);
|
} while (!gameState.players[gameState.whosNext].isAlive);
|
||||||
// kill players with hunter
|
// kill players with hunter
|
||||||
if (gameState.round >= 5) {
|
if (this.round >= 5) {
|
||||||
gameState.hunter.move(1);
|
this.hunter.move(1);
|
||||||
gameState.hunter.hunt(gameState.players);
|
this.hunter.hunt(this.players);
|
||||||
}
|
}
|
||||||
// check if all players are dead
|
// check if all players are dead
|
||||||
if (!gameState.players.some(player => player.isAlive === true)) {
|
if (!this.players.some(player => player.isAlive === true)) {
|
||||||
// todo: end game (all players are dead)
|
// 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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user