- Added multiplayer functions

- Only active user can roll the dice
- Only active user can select answers
- Card will be destroyed if active user selects answer
This commit is contained in:
2021-06-10 14:05:01 +02:00
parent d47ca1be66
commit c4a8c84db2
4 changed files with 78 additions and 33 deletions

View File

@@ -56,13 +56,6 @@ function Button(default_color, hover_color, select_color, width, height, x, y, t
this.graphics.buttonMode = true;
this.graphics.defaultCursor = 'pointer';
this.graphics.on('click', function () {
if (_this.button_is_answer) {
if (_this.selected === true) {
_this.unSelectButton();
} else {
_this.selectButton();
}
}
click();
});
this.graphics.on('mouseover', function () {

View File

@@ -1,4 +1,4 @@
function Card(game_board_size, s, a1, a2, a3, a4, d) {
function Card(game_board_size, s, a1, a2, a3, a4, d, your_turn) {
this.card = new PIXI.Graphics();
this.s = s;
this.a1 = a1;
@@ -10,6 +10,7 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
if (a3.status) this.right_answer = this.a3.text;
if (a4.status) this.right_answer = this.a4.text;
this.d = d;
this.your_turn = your_turn;
this.card_x = game_board_size * 0.25 + 2.5;
this.card_y = game_board_size / 2 - game_board_size * 0.72 / 2 + 2.5;
this.card_height = game_board_size * 0.72;
@@ -49,19 +50,27 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
// Answers
this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 4, this.a1.text, this.a1.status, true, function () {
select_answer(0, _this.a1.text);
if (this.your_turn) {
select_answer(0, _this.a1.text);
}
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 3, this.a2.text, this.a2.status, true, function () {
select_answer(1, _this.a2.text);
if (this.your_turn) {
select_answer(1, _this.a2.text);
}
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 2, this.a3.text, this.a3.status, true, function () {
select_answer(2, _this.a3.text);
if (this.your_turn) {
select_answer(2, _this.a3.text);
}
}));
this.buttons.push(new Button(0xffffff, 0xcccccc, 0x4169E1, this.card_width - 40, 150, this.card_x + 20, this.card_y + this.card_height - 120 - 170 * 1, this.a4.text, this.a4.status, true, function () {
select_answer(3, _this.a4.text);
if (this.your_turn) {
select_answer(3, _this.a4.text);
}
}));
this.buttons.forEach(button => this.card.addChild(button.getButton()));
@@ -81,7 +90,7 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
rolled_number = null;
rolled_number_text.destroy();
border_card_stack.clear();
_this.card.destroy();
socket.emit('card finished', d);
} else {
alert("Bitte wähle eine Antwortmöglichkeit aus");
}
@@ -90,6 +99,11 @@ function Card(game_board_size, s, a1, a2, a3, a4, d) {
app.stage.addChild(this.card);
};
this.destroyCard = function () {
if (this.card !== null) {
this.card.destroy();
}
};
function select_answer(id, text) {
_this.buttons.forEach(button => button.unSelectButton());

View File

@@ -9,7 +9,7 @@ let curr_player = 1;
let player_array = [1, 1, 1, 1];
let player_color = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00];
let player_sprite_array = [];
let card;
let answer = null;
let show_card = false;
let diced = false;
@@ -156,17 +156,22 @@ function start_game() {
rolled_number_text.x = sprite_size * 7 - sprite_size * 0.2 + dice.width / 2 - rolled_number_text.width / 2;
rolled_number_text.y = sprite_size * 6 - sprite_size * 0.2;
app.stage.addChild(rolled_number_text);
});
socket.on('card', function (data) {
let q = data.question;
let a = data.answers;
let d = data.difficulty;
new Card(game_board_size, q, a[0], a[1], a[2], a[3], d).showCard();
let u = data.username;
let q = data.card.question;
let a = data.card.answers;
let d = data.card.difficulty;
card = new Card(game_board_size, q, a[0], a[1], a[2], a[3], d, u === username);
card.showCard();
show_card = true;
});
socket.on('card destroyed', function () {
card.destroyCard();
});
resize();
}