-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhP.js
94 lines (60 loc) · 1.57 KB
/
hP.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
var gunShot = new Audio('sounds/gunfinal.m4a');
var zombieRoar = new Audio('sounds/Zombiefinal.m4a');
zombieRoar.volume = 0.6;
var healthPoints = 100;
function updateHealthPoints(points) {
healthPoints = points;
var healthBar = document.querySelector("#healthBar");
healthBar.style.width = points + "%";
if(healthPoints < 1) {
alert("Game over!");
window.location.reload();
}
}
function newGame() {
randomEnemyAttacks();
document.querySelector("button").style.display = "none";
}
function livingEnemies() {
return document.querySelectorAll(".enemy:not(.dead)");
}
function iShoot(enemy) {
enemy.classList.remove("shooting");
enemy.classList.remove("showing");
enemy.classList.add("dead");
if(!livingEnemies().length) {
window.location.href =
"level2/main.html";
}
}
function enemyAttacksMe(enemy) {
if(healthPoints > 0) {
enemy.classList.add("showing");
zombieRoar.play();
setTimeout(()=> {
enemyhitsMe(enemy);
}, 2000);
setTimeout(()=> {
enemy.classList.remove("showing");
}, 2000);
}
}
function enemyhitsMe(enemy) {
if(!enemy.classList.contains("dead")) {
enemy.classList.add("shooting");
updateHealthPoints(healthPoints - 20);
setTimeout(()=> {
enemy.classList.remove("shooting");
}, 200);
}
}
function randomEnemyAttacks() {
var randomEnemyNo = Math.random() * livingEnemies().length;
randomEnemyNo = Math.floor(randomEnemyNo);
var enemy = livingEnemies()[randomEnemyNo];
var randomDelay = Math.random() * 2000 + 1000;
setTimeout( ()=> {
enemyAttacksMe(enemy);
randomEnemyAttacks();
}, randomDelay);
}