-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (91 loc) · 2.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import characterData from "./data.js";
import Character from "./Character.js";
let loseSound = document.getElementById("loseSound");
let winSound = document.getElementById("winSound");
let monsterArray = ["orc", "demon", "goblin"];
let isWaiting = false;
function getNewMonster() {
const nextMonsterData = characterData[monsterArray.shift()];
return nextMonsterData ? new Character(nextMonsterData) : {};
}
function attack() {
if (!isWaiting) {
wizard.setDiceHtml();
monster.setDiceHtml();
wizard.takeDamage(monster.currentDiceScore);
monster.takeDamage(wizard.currentDiceScore);
render();
if (wizard.dead) {
isWaiting = true;
endGame();
} else if (monster.dead) {
isWaiting = true;
if (monsterArray.length) {
setTimeout(function () {
monster = getNewMonster();
render();
isWaiting = false;
}, 1000);
} else {
isWaiting = true;
endGame();
}
}
}
}
function endGame() {
wizard.health === 0 && monster.health === 0 ? loseSound.play() : wizard.health > 0 ? winSound.play() : loseSound.play();
const endEmoji = wizard.health === 0 && monster.health === 0 ? "😭" : wizard.health > 0 ? "🔮" : "☠️";
const endMessage =
wizard.health === 0 && monster.health === 0
? "No victors - all creatures are dead"
: wizard.health === 0
? `The ${monster.name} is Victorious`
: "The Wizard Wins";
setTimeout(function () {
document.querySelector("body").innerHTML = `<div class="end-game">
<h2>Game Over</h2>
<h3>${endMessage}</h3>
<p class="end-emoji">${endEmoji}</p>
</div>`;
}, 2700);
}
function render() {
document.getElementById("hero").innerHTML = wizard.getCharacterHtml();
document.getElementById("monster").innerHTML = monster.getCharacterHtml();
}
document.getElementById("attack-button").addEventListener("click", attack);
const wizard = new Character(characterData.hero);
let monster = getNewMonster();
render();
// //NEW CODE
// const DIED_STATES = {
// ORC: {
// emoji: "☠️",
// message: "The Orc is Victorious",
// },
// WIZARD: {
// emoji: "🔮",
// message: "The Wizard Wins",
// },
// BOTH: {
// emoji: "😭",
// message: "No victors - all creatures are dead",
// },
// };
// //NEW CODE
// function whoDied() {
// if (wizard.dead && orc.dead) return "BOTH";
// if (wizard.dead) return "ORC";
// return "WIZARD";
// }
// //NEW CODE
// function endMessageV2() {
// const died = whoDied();
// const { emoji, message } = DIED_STATES[died];
// document.querySelector("body").innerHTML = `<div class="end-game">
// <h2>Game Over</h2>
// <h3>${message}</h3>
// <p class="end-emoji">${emoji}</p>
// </div>`;
// }