implemented hunter

This commit is contained in:
fabianthome 2021-07-12 13:18:31 +02:00
parent 6c14f8619a
commit 58e9cd7115
5 changed files with 48 additions and 29 deletions

View File

@ -1,5 +1,5 @@
const Player = require('./Player'); const Player = require('./Player');
const Hunter = require("./Hunter"); const Hunter = require('./Hunter');
class Game { class Game {
@ -93,15 +93,15 @@ class Game {
} }
} }
getPlayerNames(){ getPlayerNames() {
return this.playerNames; return this.playerNames;
} }
addPlayerName(playerName){ addPlayerName(playerName) {
this.playerNames.push(playerName); this.playerNames.push(playerName);
} }
removePlayerName(playerName){ removePlayerName(playerName) {
this.playerNames.splice(this.playerNames.indexOf(playerName), 1) this.playerNames.splice(this.playerNames.indexOf(playerName), 1)
} }
} }

View File

@ -4,13 +4,17 @@ class Hunter {
this.isAlive = false; this.isAlive = false;
} }
getPosition() {
return this.position;
}
move_by(amount) { move_by(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 - 1) {
playerArray[i].isAlive = false; playerArray[i].isAlive = false;
} }
} }

View File

@ -2,10 +2,10 @@ const Game = require('./Game');
const express = require('express'); const express = require('express');
const fs = require('fs'); const fs = require('fs');
const {instrument} = require("@socket.io/admin-ui"); const { instrument } = require("@socket.io/admin-ui");
const app = express(); const app = express();
const server = require('http').createServer(app); const server = require('http').createServer(app);
const {Server} = require("socket.io"); const { Server } = require("socket.io");
const io = new Server(server); const io = new Server(server);
let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json')); let cards = JSON.parse(fs.readFileSync(__dirname + '/../data/fragen_10_06_21_final_new_format.json'));
@ -111,7 +111,6 @@ io.on('connection', socket => {
} else { } else {
io.to(socket.id).emit('error', 'It\'s not your turn'); io.to(socket.id).emit('error', 'It\'s not your turn');
} }
}); });
socket.on('get card', function (difficulty) { socket.on('get card', function (difficulty) {
@ -119,7 +118,7 @@ io.on('connection', socket => {
if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return; if (game[socket.room].currentStatus !== Game.STATUS.ONGOING) return;
if (game[socket.room].current_player_is(socket.username)) { if (game[socket.room].current_player_is(socket.username)) {
io.in(socket.room).emit('card', {'username': socket.username, 'card': getRandomCard(difficulty)}); io.in(socket.room).emit('card', { 'username': socket.username, 'card': getRandomCard(difficulty) });
generate_log_message(socket.room, socket.username, "CARD", difficulty); generate_log_message(socket.room, socket.username, "CARD", difficulty);
} else { } else {
@ -147,16 +146,18 @@ io.on('connection', socket => {
io.in(socket.room).emit('player moved', { io.in(socket.room).emit('player moved', {
"next_player": game[socket.room].players[game[socket.room].currentPlayerIndex].name, "next_player": game[socket.room].players[game[socket.room].currentPlayerIndex].name,
"player": index, "player": index,
"position": position "position": position,
"state": game[socket.room].currentStatus,
}); });
io.in(socket.room).emit('update Hunter', game[socket.room].hunter.getPosition());
switch (game[socket.room].currentStatus) { switch (game[socket.room].currentStatus) {
case Game.STATUS.IS_WON: case Game.STATUS.IS_WON:
//TODO show clients the winner console.log("FINISHED");
//game[socket.room].winnerIndex
break; break;
case Game.STATUS.IS_DRAW: case Game.STATUS.IS_DRAW:
//TODO show clients that nobody wins console.log("DRAW");
break; break;
default: default:
break; break;

View File

@ -1,4 +1,4 @@
function Card(game_board_size, question, answer_1, answer_2, answer_3, answer_4, difficulty, your_turn) { function Card(game_board_size, question, answer_1, answer_2, answer_3, answer_4, difficulty, your_turn, game_state) {
this.card = new PIXI.Graphics(); this.card = new PIXI.Graphics();
this.question = question; this.question = question;
this.answer_1 = answer_1; this.answer_1 = answer_1;
@ -68,8 +68,13 @@ function Card(game_board_size, question, answer_1, answer_2, answer_3, answer_4,
this.card.addChild(header); this.card.addChild(header);
if (difficulty === 0) { if (difficulty === 0) {
for (let i = 0; i < positions.length; i++) { if (game_state == 3) {
if (positions[i] > 15) this.question = "Gewinner: " + playerNames[i]; for (let i = 0; i < positions.length; i++) {
if (positions[i] > 15) this.question = "Gewinner: " + playerNames[i];
}
}
else if (game_state == 2) {
this.question = "Unentschieden.";
} }
} }
@ -123,7 +128,7 @@ function Card(game_board_size, question, answer_1, answer_2, answer_3, answer_4,
// OK-Button // OK-Button
if (difficulty === 0 && (positions[0] > 15 || positions[1] > 15 || positions[2] > 15 || positions[3] > 15)) { if (game_state == 2 || game_state == 3) {
this.card.addChild(new Button(0xffffff, 0xcccccc, 0xffffff, this.card_width - 40, 100, this.card_x + 20, this.card_y + this.card_height - 120, "New Game", null, function () { this.card.addChild(new Button(0xffffff, 0xcccccc, 0xffffff, this.card_width - 40, 100, this.card_x + 20, this.card_y + this.card_height - 120, "New Game", null, function () {
window.location.reload(); window.location.reload();
}).getButton()); }).getButton());

View File

@ -57,7 +57,7 @@ function start_game() {
sprites.forEach(sprite => app.stage.addChild(sprite.getSprite())); sprites.forEach(sprite => app.stage.addChild(sprite.getSprite()));
// Red border // Red border
let red_border = generate_red_border(new PIXI.Graphics()); let red_border = generate_red_border(new PIXI.Graphics(), 1, 9);
app.stage.addChild(red_border); app.stage.addChild(red_border);
@ -174,9 +174,9 @@ function start_game() {
score_button.defaultCursor = 'pointer'; score_button.defaultCursor = 'pointer';
score_button.on('pointerdown', function () { score_button.on('pointerdown', function () {
card = new Card(game_board_size, "", card = new Card(game_board_size, "",
{"text": playerNames[0] ? playerNames[0] + ": " + positions[0] : ("Kein Spieler"), "status": false}, { "text": playerNames[0] ? playerNames[0] + ": " + positions[0] : ("Kein Spieler"), "status": false },
{"text": playerNames[1] ? playerNames[1] + ": " + positions[1] : ("Kein Spieler"), "status": false}, { "text": playerNames[1] ? playerNames[1] + ": " + positions[1] : ("Kein Spieler"), "status": false },
{"text": playerNames[2] ? playerNames[2] + ": " + positions[2] : ("Kein Spieler"), "status": false}, { "text": playerNames[2] ? playerNames[2] + ": " + positions[2] : ("Kein Spieler"), "status": false },
{ {
"text": playerNames[3] ? playerNames[3] + ": " + positions[3] : ("Kein Spieler"), "text": playerNames[3] ? playerNames[3] + ": " + positions[3] : ("Kein Spieler"),
"status": false "status": false
@ -268,13 +268,14 @@ function start_game() {
break; break;
} }
if (x === 1 && y === 9 && data.position !== 0) { if (data.state == 2 || data.state == 3) {
card = new Card(game_board_size, "", card = new Card(game_board_size, "",
{"text": playerNames[0] ? playerNames[0] + ": " + positions[0] : ("Kein Spieler"), "status": false}, { "text": playerNames[0] ? playerNames[0] + ": " + positions[0] : ("Kein Spieler"), "status": false },
{"text": playerNames[1] ? playerNames[1] + ": " + positions[1] : ("Kein Spieler"), "status": false}, { "text": playerNames[1] ? playerNames[1] + ": " + positions[1] : ("Kein Spieler"), "status": false },
{"text": playerNames[2] ? playerNames[2] + ": " + positions[2] : ("Kein Spieler"), "status": false}, { "text": playerNames[2] ? playerNames[2] + ": " + positions[2] : ("Kein Spieler"), "status": false },
{"text": playerNames[3] ? playerNames[3] + ": " + positions[3] : ("Kein Spieler"), "status": false}, { "text": playerNames[3] ? playerNames[3] + ": " + positions[3] : ("Kein Spieler"), "status": false },
0, false); 0, false, data.state);
red_border.clear();
card.showCard(); card.showCard();
show_card = true; show_card = true;
} }
@ -282,6 +283,14 @@ function start_game() {
if (next_player === username) my_turn.text = "Your Turn"; if (next_player === username) my_turn.text = "Your Turn";
}); });
socket.on('update Hunter', function (position) {
x = sprites[position].coord_x;
y = sprites[position].coord_y;
red_border.clear();
red_border = generate_red_border(new PIXI.Graphics(), x, y);
app.stage.addChild(red_border);
});
resize(); resize();
} }
@ -297,9 +306,9 @@ function generate_card_stack(sprite, x, y, onclick) {
return sprite; return sprite;
} }
function generate_red_border(graphics) { function generate_red_border(graphics, x, y) {
graphics.lineStyle(sprite_size * 0.10, 0x862323, 1); graphics.lineStyle(sprite_size * 0.10, 0x862323, 1);
graphics.drawRect(sprite_size * 1 - sprite_size * 0.2, sprite_size * 9 - sprite_size * 0.2, sprite_size * 1.5, sprite_size * 1.5); graphics.drawRect(sprite_size * x - sprite_size * 0.2, sprite_size * y - sprite_size * 0.2, sprite_size * 1.5, sprite_size * 1.5);
return graphics; return graphics;
} }