-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
105 lines (87 loc) · 2.31 KB
/
util.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
95
96
97
98
99
100
101
102
103
104
105
function randomIntFromRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
var leaderBoard = ['first','second','third'];
for (let i=0; i<3;i++){
if ( null === localStorage.getItem(leaderBoard[i])){
localStorage.setItem(leaderBoard[i],0);
}
}
function updateLeader(){
for (let i=0; i<3;i++){
document.getElementById(`${leaderBoard[i]}`).innerHTML = localStorage.getItem(leaderBoard[i]);
}
}
function playerScore( sessionScore ){
scoreList = []
for (let i=0; i<3;i++){
scoreList.push(Number(localStorage.getItem(leaderBoard[i])));
}
scoreList.push(sessionScore);
scoreList.sort(compareNumbers);
scoreList.reverse();
for (let i=0; i<3;i++){
localStorage.setItem(leaderBoard[i],scoreList[i]);
}
updateLeader();
}
function heart_maker() {
new_heart = new spawning_lives();
heart_list.push(new_heart);
new_heart.draw();
}
function banana_maker() {
new_banana = new spawning_banana();
banana_list.push(new_banana);
new_banana.draw();
}
function boost_maker() {
new_boost = new spawning_boost();
boost_list.push(new_boost);
new_boost.draw();
}
function powerup_caller() {
picker = randomIntFromRange(1,3);
if (picker === 1) { heart_maker();}
else if (picker === 2) { banana_maker();}
else { boost_maker();}
}
function collision( r1, r2 ) {
return (
r1.x + r1.height >= r2.x &&
r1.x <= r2.x + r2.height &&
r1.y + r1.height >= r2.y &&
r1.y <= r2.y + r2.height
)
}
function end(){
hearts -= 1;
unanimate();
window.removeEventListener("keydown",movement);
clearInterval(platform_generator);
clearInterval(powerup_generator);
score_but.innerHTML = score;
if (hearts === 0 ){
play_audio("end");
playing = false;
playerScore(score);
play_but.innerHTML = "Restart";
play_but.addEventListener("click",start);
}
else {
replay = setTimeout( function () {
start()
}, 1000);
}
}
document.addEventListener('visibilitychange', function (event) {
active = document.hidden;
});
function play_audio(id){
let audio = document.querySelector(`#${id}`);
audio.currentTime = 0;
audio.play();
}
function compareNumbers(a, b) {
return a - b;
}