-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
241 lines (189 loc) · 5.83 KB
/
script.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 SPEED_SCALE = 0.00001;
const game = document.querySelector("#game");
const scoreDisplay = document.querySelector("#score");
const startMessage = document.querySelector("#start-message");
const gameoverMessage = document.querySelector("#gameover-message");
document.addEventListener("keydown", startGame, { once: true });
/* general variables */
let lastTime;
let speedScale;
let score;
/* frame update */
function update(time) {
if (lastTime == null) {
lastTime = time;
window.requestAnimationFrame(update);
return;
}
const delta = time - lastTime;
updateGround(delta, speedScale);
updateDino(delta, speedScale);
updateCactus(delta, speedScale);
updateSpeedScale(delta);
updateScore(delta);
if (checkGameOver()) return handleGameOver();
lastTime = time;
window.requestAnimationFrame(update);
}
function startGame() {
lastTime = null;
speedScale = 1;
score = 0;
setupGround();
setupDino();
setupCactus();
startMessage.classList.add("hide");
gameoverMessage.classList.add("hide");
window.requestAnimationFrame(update);
}
/* speeds up the game over time */
function updateSpeedScale(delta) {
speedScale += delta * SPEED_SCALE;
}
function updateScore(delta) {
score += delta * 0.01;
scoreDisplay.textContent = Math.floor(score);
}
/* collision conditions */
function checkCollision(rect1, rect2) {
return (
rect1.left < rect2.right &&
rect1.top < rect2.bottom &&
rect1.right > rect2.left &&
rect1.bottom > rect2.top
);
}
function checkGameOver() {
const dinoRect = getDinoRect();
return getCactusRects().some(rect => checkCollision(rect, dinoRect)); /* check collision with any of the cactus */
}
function handleGameOver() {
setDinoLose();
setTimeout(() => {
document.addEventListener("keydown", startGame, { once: true }); /* prevents accidental click */
gameoverMessage.classList.remove("hide");
}, 100);
}
/* HANDLING CSS PROPERTIES */
/* get property value */
function getCustomProperty(elem, prop) {
return parseFloat(getComputedStyle(elem).getPropertyValue(prop)) || 0;
}
/* set property value */
function setCustomProperty(elem, prop, value) {
elem.style.setProperty(prop, value);
}
/* increment the property value */
function incrementCustomProperty(elem, prop, inc) {
setCustomProperty(elem, prop, getCustomProperty(elem, prop) + inc);
}
/* GROUND MOVEMENT */
const GROUND_SPEED = 0.05;
const grounds = document.querySelectorAll(".ground");
function setupGround() {
setCustomProperty(grounds[0], "--left", 0);
setCustomProperty(grounds[1], "--left", 300);
}
function updateGround(delta, speedScale) {
grounds.forEach(ground => {
incrementCustomProperty(ground, "--left", delta * speedScale * GROUND_SPEED * -1); /* moves the ground according to game speed */
if (getCustomProperty(ground, "--left") <= -300) {
incrementCustomProperty(ground, "--left", 600); /* loop the elements */
}
});
}
/* DINOSAUR MOVEMENT */
const dino = document.querySelector("#dino");
const JUMP_SPEED = 0.45;
const GRAVITY = 0.0015;
const DINO_FRAME_COUNT = 2;
const FRAME_TIME = 100;
let isJumping;
let dinoFrame;
let currentFrameTime;
let yVelocity;
function setupDino() {
isJumping = false;
dinoFrame = 0;
currentFrameTime = 0;
yVelocity = 0;
setCustomProperty(dino, "--bottom", 0);
document.removeEventListener("keydown", onJump); /* reset the dinosaur if the player dies while jumping */
document.addEventListener("keydown", onJump);
}
function updateDino(delta, speedScale) {
handleRun(delta, speedScale);
handleJump(delta);
}
function getDinoRect() {
return dino.getBoundingClientRect(); /* get the dinosaur hitbox */
}
function setDinoLose() {
dino.src = "assets/dino-lose.png";
}
function handleRun(delta, speedScale) {
if (isJumping) {
dino.src = `assets/dino-stationary.png`;
return;
}
if (currentFrameTime >= FRAME_TIME) {
dinoFrame = (dinoFrame + 1) % DINO_FRAME_COUNT;
dino.src = `assets/dino-run-${dinoFrame}.png`; /* switch between images to simulate movement */
currentFrameTime -= FRAME_TIME;
}
currentFrameTime += delta * speedScale;
}
function handleJump(delta) {
if (!isJumping) return;
incrementCustomProperty(dino, "--bottom", yVelocity * delta);
if (getCustomProperty(dino, "--bottom") <= 0) {
setCustomProperty(dino, "--bottom", 0);
isJumping = false;
}
yVelocity -= GRAVITY * delta;
}
function onJump(e) {
if (e.code !== "Space" || isJumping) return;
yVelocity = JUMP_SPEED;
isJumping = true;
}
/* ADD CACTUS */
const CACTUS_SPEED = 0.05;
const CACTUS_INTERVAL_MIN = 500;
const CACTUS_INTERVAL_MAX = 2000;
let nextCactusTime;
function setupCactus() {
nextCactusTime = CACTUS_INTERVAL_MIN;
document.querySelectorAll(".cactus").forEach(cactus => {
cactus.remove(); /* remove cactus when game restart */
})
}
function updateCactus(delta, speedScale) {
document.querySelectorAll(".cactus").forEach(cactus => {
incrementCustomProperty(cactus, "--left", delta * speedScale * CACTUS_SPEED * -1);
if (getCustomProperty(cactus, "--left") <= -100) {
cactus.remove(); /* remove cactus off screen so it doesn't impair game performance */
}
})
if (nextCactusTime <= 0) {
createCactus();
nextCactusTime =
randomizer(CACTUS_INTERVAL_MIN, CACTUS_INTERVAL_MAX) / speedScale;
}
nextCactusTime -= delta;
}
function getCactusRects() {
return [...document.querySelectorAll(".cactus")].map(cactus => {
return cactus.getBoundingClientRect(); /* get the hitbox of all the cactus on the screen */
})
}
function createCactus() {
const cactus = document.createElement("img");
cactus.src = "assets/cactus.png";
cactus.classList.add("cactus");
setCustomProperty(cactus, "--left", 100);
game.append(cactus);
}
function randomizer(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min); /* choose a number between minimum and maximum */
}