implement player class with player movement
This commit is contained in:
parent
2ac1733251
commit
c243138b87
@ -7,9 +7,37 @@ 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'));
|
||||||
|
|
||||||
|
class Player {
|
||||||
|
constructor(socketUsername) {
|
||||||
|
this.socketUsername = socketUsername;
|
||||||
|
this.position = 0;
|
||||||
|
this.alive = true;
|
||||||
|
}
|
||||||
|
move(amount) {
|
||||||
|
this.position += amount;
|
||||||
|
if (this.position === 15) {
|
||||||
|
// todo: win
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Hunter {
|
||||||
|
constructor() {
|
||||||
|
this.position = 0;
|
||||||
|
}
|
||||||
|
move(amount) {
|
||||||
|
this.position += amount;
|
||||||
|
}
|
||||||
|
hunt(players) {
|
||||||
|
for (let i = 0; i < players.length; i++) {
|
||||||
|
if (players[i].position <= this.position) {
|
||||||
|
players[i].alive = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let gameState = {
|
let gameState = {
|
||||||
players: [],
|
players: [],
|
||||||
positions: [],
|
|
||||||
whosNext: 0,
|
whosNext: 0,
|
||||||
started: false
|
started: false
|
||||||
};
|
};
|
||||||
@ -49,8 +77,7 @@ io.on('connection', socket => {
|
|||||||
socket.username = data.username;
|
socket.username = data.username;
|
||||||
socket.room = data.room_name;
|
socket.room = data.room_name;
|
||||||
|
|
||||||
gameState['players'].push(socket.username);
|
gameState['players'].push(new Player(socket.username));
|
||||||
gameState['positions'].push(1);
|
|
||||||
addedUser = true;
|
addedUser = true;
|
||||||
|
|
||||||
socket.emit('login');
|
socket.emit('login');
|
||||||
@ -76,18 +103,22 @@ io.on('connection', socket => {
|
|||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', function () {
|
||||||
if (addedUser) {
|
if (addedUser) {
|
||||||
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
socket.broadcast.to(socket.room).emit('user left', socket.username);
|
||||||
let index = gameState['players'].indexOf(socket.username);
|
let index = -1;
|
||||||
|
for (let i = 0; i < gameState['players'].length; i++) {
|
||||||
|
if (gameState['players'][i].socketUsername === socket.username) {
|
||||||
|
index = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
gameState['players'].splice(index, 1);
|
gameState['players'].splice(index, 1);
|
||||||
gameState['positions'].splice(index, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
|
|
||||||
if (gameState['players'].length === 0) {
|
if (gameState['players'].length === 0) {
|
||||||
gameState['players'] = [];
|
gameState['players'] = [];
|
||||||
gameState['positions'] = [];
|
|
||||||
gameState['whosNext'] = 0;
|
gameState['whosNext'] = 0;
|
||||||
gameState['started'] = false;
|
gameState['started'] = false;
|
||||||
}
|
}
|
||||||
@ -99,7 +130,8 @@ io.on('connection', socket => {
|
|||||||
|
|
||||||
// Game
|
// Game
|
||||||
socket.on('roll dice', function () {
|
socket.on('roll dice', function () {
|
||||||
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
|
|
||||||
|
if (gameState['players'][gameState['whosNext']].socketUsername === socket.username) {
|
||||||
gameState['started'] = true;
|
gameState['started'] = true;
|
||||||
let sides = 3;
|
let sides = 3;
|
||||||
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
let randomNumber = Math.floor(Math.random() * sides) + 1;
|
||||||
@ -113,7 +145,7 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('get card', function (difficulty) {
|
socket.on('get card', function (difficulty) {
|
||||||
if (gameState['whosNext'] === gameState['players'].indexOf(socket.username)) {
|
if (gameState['players'][gameState['whosNext']].socketUsername === 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);
|
||||||
@ -123,7 +155,7 @@ io.on('connection', socket => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('card finished', function (difficulty) {
|
socket.on('card finished', function (difficulty) {
|
||||||
gameState['positions'][gameState['players'].indexOf(socket.username)] += difficulty;
|
gameState['players'][gameState['whosNext']].move(difficulty); //TODO: stop players from moving when answer is wrong
|
||||||
gameState['whosNext'] += 1;
|
gameState['whosNext'] += 1;
|
||||||
if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0;
|
if(gameState['whosNext'] === gameState['players'].length) gameState['whosNext'] = 0;
|
||||||
io.in(socket.room).emit('card destroyed');
|
io.in(socket.room).emit('card destroyed');
|
||||||
|
@ -59,7 +59,6 @@
|
|||||||
<script src="js/Button.js"></script>
|
<script src="js/Button.js"></script>
|
||||||
<script src="js/Sprite.js"></script>
|
<script src="js/Sprite.js"></script>
|
||||||
<script src="js/chat.js"></script>
|
<script src="js/chat.js"></script>
|
||||||
<script src="js/player.js"></script>
|
|
||||||
<script src="js/game.js"></script>
|
<script src="js/game.js"></script>
|
||||||
<script src="js/index.js"></script>
|
<script src="js/index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
@ -5,12 +5,6 @@
|
|||||||
dice.svg: https://www.svgrepo.com/download/198836/gambler-casino.svg
|
dice.svg: https://www.svgrepo.com/download/198836/gambler-casino.svg
|
||||||
sprite.jpg: https://media.istockphoto.com/photos/gray-granite-stone-texture-seamless-square-background-tile-ready-picture-id1096464726
|
sprite.jpg: https://media.istockphoto.com/photos/gray-granite-stone-texture-seamless-square-background-tile-ready-picture-id1096464726
|
||||||
*/
|
*/
|
||||||
let curr_player = 1;
|
|
||||||
let player_array = createPlayers(4);
|
|
||||||
let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
|
|
||||||
let player_sprite_array = [];
|
|
||||||
let hunter = new Hunter();
|
|
||||||
|
|
||||||
let card;
|
let card;
|
||||||
let answer = null;
|
let answer = null;
|
||||||
let show_card = false;
|
let show_card = false;
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
function createPlayers(amount) {
|
|
||||||
let players = new Array(amount);
|
|
||||||
for (let i = 0; i < amount; i++) {
|
|
||||||
players[i] = new Player();
|
|
||||||
}
|
|
||||||
return players;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Player {
|
|
||||||
constructor() {
|
|
||||||
this.position = 0;
|
|
||||||
this.alive = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
move(amount) {
|
|
||||||
this.position += amount;
|
|
||||||
if (this.position === 15) {
|
|
||||||
// todo: win
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Hunter {
|
|
||||||
constructor() {
|
|
||||||
this.position = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
move(amount) {
|
|
||||||
this.position += amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
hunt(players) {
|
|
||||||
for (let i = 0; i < players.length; i++) {
|
|
||||||
if (players[i].position <= this.position) {
|
|
||||||
players[i].alive = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user