Player movement #38
@ -1,26 +1,33 @@
|
||||
const Player = require('./Player');
|
||||
const Hunter = require("./Hunter");
|
||||
|
||||
class Game {
|
||||
constructor() {
|
||||
this.players = [];
|
||||
this.whosNext = 0;
|
||||
this.currentPlayerIndex = 0;
|
||||
this.started = false;
|
||||
this.round = 0;
|
||||
this.hunter = new Hunter();
|
||||
}
|
||||
|
||||
finish_turn() {
|
||||
let move_to_next_round = false;
|
||||
// move on to next player; skip dead players
|
||||
do {
|
||||
this.whosNext++;
|
||||
if (this.whosNext === this.players.length) {
|
||||
this.whosNext = 0;
|
||||
this.round++;
|
||||
this.currentPlayerIndex++;
|
||||
if (this.currentPlayerIndex >= this.players.length) {
|
||||
this.currentPlayerIndex = 0;
|
||||
move_to_next_round = true;
|
||||
}
|
||||
} while (!this.players[this.whosNext].isAlive);
|
||||
} while (!this.players[this.currentPlayerIndex].isAlive); // skip dead players
|
||||
this.finish_round();
|
||||
}
|
||||
|
||||
finish_round() {
|
||||
this.round++;
|
||||
// kill players with hunter
|
||||
if (this.round >= 5) {
|
||||
this.hunter.move(1);
|
||||
this.hunter.move_by(1);
|
||||
this.hunter.hunt(this.players);
|
||||
}
|
||||
// check if all players are dead
|
||||
@ -28,6 +35,29 @@ class Game {
|
||||
// todo: end game (all players are dead)
|
||||
}
|
||||
}
|
||||
|
||||
add_player(name) {
|
||||
this.players.push(new Player(name));
|
||||
}
|
||||
|
||||
remove_player(name) {
|
||||
let index = this.get_player_index(name);
|
||||
if (index !== -1) this.players.splice(index, 1);
|
||||
if (this.currentPlayerIndex === index) this.finish_turn(); // if current player leaves: move on to next
|
||||
}
|
||||
|
||||
current_player_is(name) {
|
||||
return this.players[this.currentPlayerIndex].name === name;
|
||||
}
|
||||
|
||||
get_player_index(name) {
|
||||
return this.players.findIndex(player => player.name === name);
|
||||
}
|
||||
|
||||
move_player(name, amount) {
|
||||
let index = this.get_player_index(name);
|
||||
this.players[index].move_by(amount);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Game;
|
@ -3,7 +3,7 @@ class Hunter {
|
||||
this.position = 0;
|
||||
}
|
||||
|
||||
move(amount) {
|
||||
move_by(amount) {
|
||||
this.position += amount;
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
class Player {
|
||||
constructor(socketUsername) {
|
||||
this.socketUsername = socketUsername;
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
this.position = 0;
|
||||
this.isAlive = true;
|
||||
}
|
||||
|
||||
move(amount) {
|
||||
move_by(amount) {
|
||||
this.position += amount;
|
||||
if (this.position === 15) {
|
||||
//todo: move by 1 only on the last 3 fields
|
||||
if (this.position >= 16) {
|
||||
// todo: win
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
const Player = require('./Player');
|
||||
const Game = require('./Game');
|
||||
|
||||
const express = require('express');
|
||||
@ -50,7 +49,7 @@ io.on('connection', socket => {
|
||||
}
|
||||
|
||||
if (gameState[socket.room].players.length < 4 && !gameState[socket.room].started) {
|
||||
gameState[socket.room].players.push(new Player(socket.username));
|
||||
gameState[socket.room].add_player(socket.username);
|
||||
addedUser = true;
|
||||
|
||||
socket.emit('login');
|
||||
@ -77,38 +76,21 @@ io.on('connection', socket => {
|
||||
|
||||
socket.on('disconnect', function () {
|
||||
if (gameState[socket.room] !== undefined && addedUser) {
|
||||
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
||||
let index = -1;
|
||||
for (let i = 0; i < gameState[socket.room].players.length; i++) {
|
||||
if (gameState[socket.room].players[i].socketUsername === socket.username) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index > -1) {
|
||||
gameState[socket.room].players.splice(index, 1);
|
||||
}
|
||||
gameState.remove_player(socket.username);
|
||||
|
||||
socket.leave(socket.room);
|
||||
|
||||
if (gameState[socket.room].players.length === 0) {
|
||||
delete gameState[socket.room];
|
||||
}
|
||||
if (gameState[socket.room].players.length === 0) delete gameState[socket.room];
|
||||
}
|
||||
|
||||
|
||||
generate_log_message(socket.room, socket.username, "LEFT", "");
|
||||
});
|
||||
|
||||
// Game
|
||||
socket.on('roll dice', function () {
|
||||
if (gameState[socket.room] !== undefined && addedUser) {
|
||||
if(gameState[socket.room].players[gameState[socket.room].whosNext] === undefined) {
|
||||
console.log(gameState[socket.room].players)
|
||||
console.log(gameState[socket.room].whosNext)
|
||||
}
|
||||
|
||||
if (gameState[socket.room].players[gameState[socket.room].whosNext].socketUsername === socket.username) {
|
||||
if (gameState[socket.room].current_player_is(socket.username)) {
|
||||
gameState[socket.room].started = true;
|
||||
let sides = 3;
|
||||
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
||||
@ -124,7 +106,7 @@ io.on('connection', socket => {
|
||||
|
||||
socket.on('get card', function (difficulty) {
|
||||
if (gameState[socket.room] !== undefined && addedUser) {
|
||||
if (gameState[socket.room].players[gameState[socket.room].whosNext].socketUsername === socket.username) {
|
||||
if (gameState[socket.room].current_player_is(socket.username)) {
|
||||
io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)});
|
||||
|
||||
generate_log_message(socket.room, socket.username, "CARD", difficulty);
|
||||
@ -136,7 +118,10 @@ io.on('connection', socket => {
|
||||
|
||||
socket.on('card finished', function (difficulty, answerIsCorrect) {
|
||||
if (gameState[socket.room] !== undefined && addedUser) {
|
||||
if (answerIsCorrect) gameState[socket.room].players[gameState[socket.room].whosNext].move(difficulty);
|
||||
if (answerIsCorrect) {
|
||||
gameState[socket.room].move_player(socket.username, difficulty);
|
||||
generate_log_message(socket.room, socket.username, "MOVE", difficulty);
|
||||
}
|
||||
io.in(socket.room).emit('card destroyed');
|
||||
gameState[socket.room].finish_turn();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user