Projektmanagement-Game/public/js/Card.js

178 lines
7.0 KiB
JavaScript
Raw Normal View History

2021-07-12 11:18:31 +00:00
function Card(game_board_size, question, answer_1, answer_2, answer_3, answer_4, difficulty, your_turn, game_state) {
2021-06-08 13:38:43 +00:00
this.card = new PIXI.Graphics();
2021-07-05 13:57:59 +00:00
this.question = question;
this.answer_1 = answer_1;
this.answer_2 = answer_2;
this.answer_3 = answer_3;
this.answer_4 = answer_4;
if (answer_1.status) this.right_answer = this.answer_1.text;
if (answer_2.status) this.right_answer = this.answer_2.text;
if (answer_3.status) this.right_answer = this.answer_3.text;
if (answer_4.status) this.right_answer = this.answer_4.text;
this.difficulty = difficulty;
this.your_turn = your_turn;
2021-06-08 13:38:43 +00:00
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;
this.card_width = game_board_size * 0.5;
this.buttons = [];
let _this = this;
this.showCard = function () {
const style = new PIXI.TextStyle({
fontFamily: 'Arial',
2021-06-14 11:14:16 +00:00
fontSize: 50,
2021-06-08 13:38:43 +00:00
wordWrap: true,
wordWrapWidth: game_board_size * 0.5 - 20,
});
const header_style = new PIXI.TextStyle({
fontFamily: 'Arial',
fontSize: 70,
wordWrap: true,
wordWrapWidth: game_board_size * 0.5 - 20,
});
this.card.beginFill(0xffffff);
2021-07-05 13:57:59 +00:00
switch (difficulty) {
2021-06-24 08:52:08 +00:00
case 1:
this.card.lineStyle(30, 0x6C9A8B, 1);
break;
case 2:
this.card.lineStyle(30, 0xFFDDA1, 1);
break;
case 3:
this.card.lineStyle(30, 0xF47A93, 1);
break;
}
this.card.beginFill(0xFFFFFF);
this.card.moveTo(this.card_x + 50, this.card_y);
this.card.lineTo(this.card_x + 0, this.card_y + 50);
this.card.lineTo(this.card_x + 0, this.card_y + this.card_height);
this.card.lineTo(this.card_x + 50, this.card_y + this.card_height + 50);
this.card.lineTo(this.card_x + this.card_width - 50, this.card_y + this.card_height + 50);
this.card.lineTo(this.card_x + this.card_width, this.card_y + this.card_height);
this.card.lineTo(this.card_x + this.card_width, this.card_y + 50);
this.card.lineTo(this.card_x + this.card_width - 50, this.card_y + 0);
this.card.closePath();
2021-06-08 13:38:43 +00:00
this.card.endFill();
2021-06-21 20:45:58 +00:00
let header;
2021-07-05 13:57:59 +00:00
if (this.difficulty === 0) {
2021-06-21 20:45:58 +00:00
header = new PIXI.Text("Scoreboard", header_style);
} else {
2021-07-05 13:57:59 +00:00
header = new PIXI.Text("Schwierigkeit " + this.difficulty, header_style);
2021-06-21 20:45:58 +00:00
}
2021-06-08 13:38:43 +00:00
header.x = this.card_x + 20 + this.card.width / 2 - header.width / 2 - 2.5 - 20;
header.y = this.card_y + 20;
this.card.addChild(header);
2021-07-05 13:57:59 +00:00
if (difficulty === 0) {
2021-07-12 11:18:31 +00:00
if (game_state == 3) {
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.";
2021-07-05 13:57:59 +00:00
}
}
const basicText = new PIXI.Text(this.question, style);
2021-06-08 13:38:43 +00:00
basicText.x = this.card_x + 20;
basicText.y = this.card_y + 50 + header.height;
this.card.addChild(basicText);
// Answers
2021-06-21 20:45:58 +00:00
let color = 0xffffff;
2021-07-05 13:57:59 +00:00
if (this.difficulty === 0) {
2021-06-21 20:45:58 +00:00
color = 0xFFDDA1;
}
2021-07-05 13:57:59 +00:00
this.buttons.push(new Button(color, 0xcccccc, 0x4169E1, this.card_width - 40, 200, this.card_x + 20, this.card_y + this.card_height - 120 - 220 * 4, this.answer_1.text, this.answer_1.status, function () {
2021-06-14 10:06:49 +00:00
if (_this.your_turn) {
2021-07-05 13:57:59 +00:00
select_answer(0, _this.answer_1.text);
}
2021-06-08 13:38:43 +00:00
}));
2021-06-21 20:45:58 +00:00
color = 0xffffff;
2021-07-05 13:57:59 +00:00
if (this.difficulty === 0) {
2021-06-21 20:45:58 +00:00
color = 0x4169E1;
}
2021-07-05 13:57:59 +00:00
this.buttons.push(new Button(color, 0xcccccc, 0x4169E1, this.card_width - 40, 200, this.card_x + 20, this.card_y + this.card_height - 120 - 220 * 3, this.answer_2.text, this.answer_2.status, function () {
2021-06-14 10:06:49 +00:00
if (_this.your_turn) {
2021-07-05 13:57:59 +00:00
select_answer(1, _this.answer_2.text);
}
2021-06-08 13:38:43 +00:00
}));
2021-06-21 20:45:58 +00:00
color = 0xffffff;
2021-07-05 13:57:59 +00:00
if (this.difficulty === 0) {
2021-06-21 20:45:58 +00:00
color = 0x6C9A8B;
}
2021-07-05 13:57:59 +00:00
this.buttons.push(new Button(color, 0xcccccc, 0x4169E1, this.card_width - 40, 200, this.card_x + 20, this.card_y + this.card_height - 120 - 220 * 2, this.answer_3.text, this.answer_3.status, function () {
2021-06-14 10:06:49 +00:00
if (_this.your_turn) {
2021-07-05 13:57:59 +00:00
select_answer(2, _this.answer_3.text);
}
2021-06-08 13:38:43 +00:00
}));
2021-06-21 20:45:58 +00:00
color = 0xffffff;
2021-07-05 13:57:59 +00:00
if (this.difficulty === 0) {
2021-06-21 20:45:58 +00:00
color = 0xF47A93;
}
2021-07-05 13:57:59 +00:00
this.buttons.push(new Button(color, 0xcccccc, 0x4169E1, this.card_width - 40, 200, this.card_x + 20, this.card_y + this.card_height - 120 - 220 * 1, this.answer_4.text, this.answer_4.status, function () {
2021-06-14 10:06:49 +00:00
if (_this.your_turn) {
2021-07-05 13:57:59 +00:00
select_answer(3, _this.answer_4.text);
}
2021-06-08 13:38:43 +00:00
}));
this.buttons.forEach(button => this.card.addChild(button.getButton()));
// OK-Button
2021-07-12 11:18:31 +00:00
if (game_state == 2 || game_state == 3) {
2021-07-05 13:57:59 +00:00
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();
}).getButton());
} else {
this.card.addChild(new Button(0xffffff, 0xcccccc, 0xffffff, this.card_width - 40, 100, this.card_x + 20, this.card_y + this.card_height - 120, "OK", null, function () {
if (answer !== null) {
if (_this.right_answer === answer) { //TODO: do this in backend instead to prevent cheating
console.log("Richtig");
socket.emit('card finished', difficulty, true);
} else {
console.log("Falsch");
socket.emit('card finished', difficulty, false);
}
2021-06-21 20:45:58 +00:00
show_card = false;
answer = null;
diced = false;
rolled_number = null;
2021-07-05 13:57:59 +00:00
} else {
if (your_turn === true) {
alert("Bitte wähle eine Antwortmöglichkeit aus");
} else {
show_card = false;
answer = null;
diced = false;
rolled_number = null;
card.destroyCard();
border_card_stack.clear();
}
2021-06-21 20:45:58 +00:00
}
2021-07-05 13:57:59 +00:00
}).getButton());
}
2021-06-08 13:38:43 +00:00
app.stage.addChild(this.card);
2021-06-09 12:11:47 +00:00
};
2021-06-08 13:38:43 +00:00
this.destroyCard = function () {
if (this.card !== null) {
this.card.destroy();
}
};
2021-06-08 13:38:43 +00:00
function select_answer(id, text) {
2021-06-08 13:38:43 +00:00
_this.buttons.forEach(button => button.unSelectButton());
_this.buttons[id].selectButton();
answer = text;
2021-06-08 13:38:43 +00:00
}
}