-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (66 loc) · 2.09 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
import characterData from './data.js'
import Character from './Character.js'
let monstersArray = ["orc", "demon", "goblin"]
let isWaiting = false
function getNewMonster() {
const nextMonsterData = characterData[monstersArray.shift()]
return nextMonsterData ? new Character(nextMonsterData) : {}
}
/*
Challenge
1. Disable the user's ability to attack when a monster dies.
2. Reneable the user's ability to attack when a new monster
loads.
3. When the game is over, disable the user's ability to attack.
**hint.md for help!!**
*/
function attack() {
if(!isWaiting){
wizard.getDiceHtml()
monster.getDiceHtml()
wizard.takeDamage(monster.currentDiceScore)
monster.takeDamage(wizard.currentDiceScore)
render()
if(wizard.dead){
endGame()
}
else if(monster.dead){
isWaiting = true
if(monstersArray.length > 0){
setTimeout(()=>{
monster = getNewMonster()
render()
isWaiting = false
},1500)
}
else{
endGame()
}
}
}
}
function endGame() {
isWaiting = true
const endMessage = wizard.health === 0 && monster.health === 0 ?
"No victors - all creatures are dead" :
wizard.health > 0 ? "The Wizard Wins" :
"The Orc is Victorious"
const endEmoji = wizard.health > 0 ? "🔮" : "☠️"
setTimeout(()=>{
document.body.innerHTML = `
<div class="end-game">
<h2>Game Over</h2>
<h3>${endMessage}</h3>
<p class="end-emoji">${endEmoji}</p>
</div>
`
}, 1500)
}
document.getElementById("attack-button").addEventListener('click', attack)
function render() {
document.getElementById('hero').innerHTML = wizard.getCharacterHtml()
document.getElementById('monster').innerHTML = monster.getCharacterHtml()
}
const wizard = new Character(characterData.hero)
let monster = getNewMonster()
render()