-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (69 loc) · 2.11 KB
/
app.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
import characterData from "./data.js";
import Character from "./Character.js";
let villansArray = ["mihawk", "crocodile", "buggy"];
let isWaiting = false;
function getNewMonster() {
const nextVillainData = characterData[villansArray.shift()];
return nextVillainData ? new Character(nextVillainData) : {};
}
function attack() {
if (!isWaiting) {
shanks.setDiceHtml();
villains.setDiceHtml();
shanks.takeDamage(villains.currentDiceScore);
villains.takeDamage(shanks.currentDiceScore);
render();
if (shanks.dead) {
endGame();
} else if (villains.dead) {
isWaiting = true;
if (villansArray.length > 0) {
setTimeout(() => {
villains = getNewMonster();
render();
isWaiting = false;
}, 1500);
} else {
endGame();
}
}
}
}
function endGame() {
isWaiting = true;
const endMessage =
shanks.health === 0 && villains.health === 0
? "No victors - all Pirates are Dead"
: shanks.health > 0
? "Red-Haired Shanks won the battle"
: "Cross-Guild is Victorious";
const endEmoji =
shanks.health === 0 && villains.health === 0
? "./images/tie.png"
: shanks.health > 0
? "./images/win.png"
: "./images/lose.png";
setTimeout(() => {
document.body.innerHTML = `
<div class="end-game">
<button id="attack-button" class="restart-btn">Restart Game</button>
<h2>Game Over</h2>
<h3>${endMessage}</h3>
<img class="end-img" src=${endEmoji} />
<br>
</div>`;
}, 1500);
setTimeout(() => {
document
.querySelector(".restart-btn")
.addEventListener("click", () => window.location.reload());
}, 1500);
}
document.getElementById("attack-button").addEventListener("click", attack);
function render() {
document.getElementById("hero").innerHTML = shanks.getCharacterHtml();
document.getElementById("villain").innerHTML = villains.getCharacterHtml();
}
const shanks = new Character(characterData.hero);
let villains = getNewMonster();
render();