-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
241 lines (217 loc) · 6.77 KB
/
game.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const timeElement = document.getElementById("time");
const scoreElement = document.getElementById("score");
let time = 0;
let score = 0;
let invincible = true;
let gameFinished = false;
let scoreUpdateInterval;
let speedMultiplicator = ((2 / 3) * window.innerHeight) / 515 + 0.2;
const obstacleAmount = Math.floor(window.innerWidth / 120);
function triggerVincible() {
invincible = false;
player.style.animation = "";
}
function updateScore() {
time++;
timeElement.textContent = (time / 10).toFixed();
if (!invincible) {
score += 1;
scoreElement.textContent = score;
}
}
const player = document.getElementById("player");
function updatePlayerPosition(event) {
const playerBoundingRect = player.getBoundingClientRect();
const x = event.clientX;
const y = event.clientY;
player.style.transform = `translate(${x - playerBoundingRect.width / 2}px, ${y - playerBoundingRect.height / 2}px)`;
}
let obstacles = [];
function createObstacles() {
for (let i = 0; i < obstacleAmount; i++) {
const obstacle = document.createElement("div");
obstacle.className = "obstacle";
const x = Math.random() * window.innerWidth;
const y = Math.random() * -200;
obstacle.style.transform = "translate(" + x + "px" + y + "px)";
document.body.append(obstacle);
obstacles.push([obstacle, x, y]);
}
}
let fallAnimation;
function animationStep() {
const playerBoundingRect = player.getBoundingClientRect();
for (const obstacle of obstacles) {
const obstacleBoundingRect = obstacle[0].getBoundingClientRect();
const element = obstacle[0];
obstacle[2] += (2 + time / 2000) * speedMultiplicator;
element.style.transform =
"translate(" + obstacle[1] + "px, " + obstacle[2] + "px)";
// check if the rectangle has trespassed the bottom of the window
if (obstacle[2] > window.innerHeight) {
obstacle[2] = Math.random() * -200;
obstacle[1] = Math.random() * window.innerWidth;
}
// check if the player was hit
const playerLeft = playerBoundingRect.x;
const playerRight = playerBoundingRect.x + playerBoundingRect.width;
const playerTop = playerBoundingRect.y;
const playerBottom = playerBoundingRect.y + playerBoundingRect.height;
const obstacleLeft = obstacleBoundingRect.x;
const obstacleRight = obstacleBoundingRect.x + obstacleBoundingRect.width;
const obstacleTop = obstacleBoundingRect.y;
const obstacleBottom = obstacleBoundingRect.y + obstacleBoundingRect.height;
if (
playerRight > obstacleLeft &&
playerLeft < obstacleRight &&
playerBottom > obstacleTop &&
playerTop < obstacleBottom &&
!invincible
) {
GameOver();
}
}
}
function magnet() {
const playerBoundingRect = player.getBoundingClientRect();
for (const obstacle of obstacles) {
const x = obstacle[1];
const y = obstacle[2];
if (x > playerBoundingRect.x) {
obstacle[1] -= speedMultiplicator;
} else if (x < playerBoundingRect.x) {
obstacle[1] += speedMultiplicator;
}
if (y > playerBoundingRect.y) {
obstacle[2] -= speedMultiplicator;
} else if (y < playerBoundingRect.y) {
obstacle[2] += speedMultiplicator;
}
}
}
function blink() {
for (const obstacle of obstacles) {
obstacle[0].style.opacity = "0";
setTimeout(() => (obstacle[0].style.opacity = "1"), 500);
}
}
const filter = document.getElementById("filter");
const effects = [
() => {
filter.style.backdropFilter = "blur(8px)";
filter.style.webkitBackdropFilter = "blur(8px)";
vignette.classList.add("vignette-black");
setTimeout(() => vignette.classList.remove("vignette-black"));
return () => {
filter.style.backdropFilter = "";
filter.style.webkitBackdropFilter = "";
};
},
() => {
filter.style.backdropFilter = "grayscale(100%)";
filter.style.webkitBackdropFilter = "grayscale(100%)";
vignette.classList.add("vignette-red");
return () => {
filter.style.backdropFilter = "";
filter.style.webkitBackdropFilter = "";
vignette.classList.remove("vignette-red");
};
},
() => {
player.style.width = "1rem";
player.style.height = "1rem";
filter.style.animation = "rotateColours 1s infinite";
for (const obstacle of obstacles) {
obstacle[0].style.width = "7rem";
obstacle[0].style.height = "7rem";
}
return () => {
player.style.width = "5rem";
player.style.height = "5rem";
for (const obstacle of obstacles) {
obstacle[0].style.width = "5rem";
obstacle[0].style.height = "5rem";
}
filter.style.animation = "";
};
},
() => {
const magnetInterval = setInterval(magnet, 120);
return () => clearInterval(magnetInterval);
},
() => {
const blinkInterval = setInterval(blink, 1000);
return () => {
clearInterval(blinkInterval);
for (const obstacle of obstacles) {
obstacle[0].style.opacity = "1";
}
};
},
];
let effectTimeout;
let randomEffectInterval;
let cleanup;
const vignette = document.getElementById("vignette");
function randomEffect() {
effectTimeout = setTimeout(
() => {
const effectIdx = Math.floor(Math.random() * effects.length);
const effect = effects[effectIdx];
cleanup = effect();
setTimeout(
() => {
if (typeof cleanup === "function") cleanup();
},
(8 + Math.random() * 4) * 1000,
);
},
(Math.random() * 10 + 5) * 1000,
);
}
function initialise() {
gameFinished = false;
invincible = true;
for (const obstacle of obstacles) {
obstacle[0].remove();
}
score = 0;
scoreElement.textContent = "0";
time = 0;
obstacles = [];
createObstacles();
scoreUpdateInterval = setInterval(updateScore, 100);
fallAnimation = setInterval(animationStep, 1);
setTimeout(triggerVincible, 5000);
document.addEventListener("mousemove", updatePlayerPosition);
const gameover = document.getElementById("gameover");
gameover.style.opacity = "0";
document.documentElement.style.cursor = "none";
player.style.animation = ".5s linear 0s 10 running invincible";
randomEffectInterval = setInterval(randomEffect, 27000);
randomEffect();
}
initialise();
function GameOver() {
clearInterval(randomEffectInterval);
clearTimeout(effectTimeout);
document.documentElement.style.cursor = "";
clearInterval(fallAnimation);
clearInterval(scoreUpdateInterval);
document.removeEventListener("mousemove", updatePlayerPosition);
const gameover = document.getElementById("gameover");
gameover.style.opacity = "1";
const finalScore = document.getElementById("finalScore");
finalScore.textContent = score;
gameFinished = true;
if (typeof cleanup === "function") {
cleanup();
cleanup = undefined;
}
}
function RestartGame() {
if (gameFinished) {
initialise();
}
}
window.addEventListener("click", RestartGame);