-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (51 loc) · 1.54 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
let chosenMaxLife = 100;
const ATTACK_VALUE = 10;
const STRONG_ATTACK_VALUE = 20;
const MONSTER_ATTACK_VALUE = 14;
const HEAL_VALUE = 20;
let currentMonsterHealth = chosenMaxLife;
let currentPlayerHealth = chosenMaxLife;
adjustHealthBars(chosenMaxLife);
function attackHandler() {
attackMonster('ATTACK');
}
function attackMonster(mode) {
let maxDamage;
if (mode === 'ATTACK') {
maxDamage = ATTACK_VALUE;
} else {
maxDamage = STRONG_ATTACK_VALUE;
}
const damage = dealMonsterDamage(maxDamage);
currentMonsterHealth -= damage;
endRound();
}
function endRound() {
const playerDamage = dealPlayerDamage(MONSTER_ATTACK_VALUE);
currentPlayerHealth -= playerDamage;
if (currentMonsterHealth <= 0) {
alert('You won');
} else if (currentPlayerHealth <= 0 && currentMonsterHealth > 0) {
alert('You lost');
} else if (currentPlayerHealth <= 0 && currentMonsterHealth <= 0) {
alert('Draw');
}
}
function strongAttackHandler() {
attackMonster('STRONG_ATTACK');
}
function healPlayerHandler() {
let healValue;
if (currentPlayerHealth >= chosenMaxLife - HEAL_VALUE) {
alert("You can't heal more than your max health");
healValue = chosenMaxLife - currentPlayerHealth;
} else {
healValue = HEAL_VALUE;
}
increasePlayerHealth(healValue);
currentPlayerHealth += healValue;
endRound();
}
attackBtn.addEventListener('click', attackHandler);
strongAttackBtn.addEventListener('click', strongAttackHandler);
healBtn.addEventListener('click', healPlayerHandler);