-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.82 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
import characterData from "./data.js"
import Character from "./character.js"
document.getElementById("attack-button").addEventListener("click", attack)
let monstersArray = ["orc", "demon", "goblin"];
function attack() {
if (wizard.dead || monster.dead) {
// nothing happens - attack button effectively disabled
} else {
wizard.setDiceHtml()
monster.setDiceHtml()
wizard.takeDamage(monster.currentDiceScore)
monster.takeDamage(wizard.currentDiceScore)
render()
if (wizard.dead) {
endGame()
} else if (monster.dead) {
if (monstersArray.length === 0) {
endGame()
} else {
setTimeout (function() {
monster = getNewMonster()
render()
}, 1500)
}
}
}
}
function endGame() {
const endMessage = wizard.health === 0 && monster.health === 0 ? "No victors - all creatures are dead" :
wizard.health > 0 ? "The Wizard Wins" :
`The ${monster.name} is Victorious`;
const endEmoji = wizard.health === 0 ? `☠️` : `🔮`
setTimeout(function() {
document.body.innerHTML =
`<div class="end-game">
<h2>Game Over</h2>
<h3>${endMessage}</h3>
<p class="end-emoji">${endEmoji}</p>
</div>`;
}, 1500)
}
function render() {
document.getElementById("hero").innerHTML = wizard.getCharacterHtml()
document.getElementById("monster").innerHTML = monster.getCharacterHtml()
}
function getNewMonster() {
const nextMonsterData = characterData[monstersArray.shift()]
return nextMonsterData ? new Character(nextMonsterData) : {}
}
const wizard = new Character(characterData.hero)
let monster = getNewMonster()
render()