-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (116 loc) · 3.7 KB
/
script.js
File metadata and controls
143 lines (116 loc) · 3.7 KB
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
import { setupGround, updateGround } from './ground.js';
import { getDinoRect, setDinoLose, setupDino, updateDino } from './dino.js';
import { getCactusRects, setupCactus, updateCactus } from './cactus.js';
const WORLD_WIDTH = 100;
const WORLD_HEIGHT = 30;
const SPEED_SCALE_INCREASE = 0.00001;
const checkpoint_sound = new Audio('assets/checkpoint.mp3');
const die_sound = new Audio('assets/die.mp3');
const worldElem = document.querySelector('[data-world]')
const scoreElem = document.querySelector('[data-score]')
const startScreenElem = document.querySelector('[data-start-screen]')
const highScoreElem = document.querySelector('[data-high-score]')
let lastTime;
let speedScale;
let highScore;
let score;
let prev_score;
setPixelToWorldScale();
updateHighScore();
window.addEventListener('resize', setPixelToWorldScale)
document.addEventListener('keydown', handleStart, { once: true })
function setPixelToWorldScale() {
let worldToPixelScale;
if (window.innerWidth / window.innerHeight < WORLD_WIDTH / WORLD_HEIGHT) {
worldToPixelScale = window.innerWidth / WORLD_WIDTH;
} else {
worldToPixelScale = window.innerHeight / WORLD_HEIGHT;
}
worldElem.style.width = `${WORLD_WIDTH * worldToPixelScale}px`;
worldElem.style.height = `${WORLD_HEIGHT * worldToPixelScale}px`;
}
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);
updateBackground();
if (checkLose()) {
die_sound.play();
return handleLose();
}
lastTime = time;
window.requestAnimationFrame(update);
}
function checkLose() {
const dinoRect = getDinoRect();
return getCactusRects().some(rect => isCollision(rect, dinoRect));
}
function isCollision(rect1, rect2) {
return rect1.left < rect2.right &&
rect1.top < rect2.bottom &&
rect1.right > rect2.left &&
rect1.bottom > rect2.top;
}
function updateSpeedScale(delta) {
speedScale += delta * SPEED_SCALE_INCREASE;
}
function updateScore(delta) {
score += delta * 0.01;
if (Number.parseInt(score) % 100 == 0 && Number.parseInt(score) != 0) {
checkpoint_sound.play();
}
scoreElem.textContent = Math.floor(score);
}
function updateHighScore() {
if (highScore === undefined) {
highScore = parseInt(localStorage.getItem('highScore'), 10) || 0;
}
if (score > highScore) {
highScore = Math.floor(score);
localStorage.setItem('highScore', highScore);
}
if (highScore > 0) {
highScoreElem.textContent = 'HI:' + highScore;
}
}
function toggleBackground() {
if (document.body.classList.contains("dark")) {
document.body.classList.remove("dark");
} else {
document.body.classList.add("dark");
}
}
function updateBackground() {
if (Number.parseInt(score) % 200 == 0 && Number.parseInt(score) !== prev_score && Number.parseInt(score) > 0) {
toggleBackground();
console.log(score);
prev_score = Number.parseInt(score);
}
}
function handleStart() {
lastTime = null;
speedScale = 1;
score = 0;
setupGround();
setupDino();
setupCactus();
startScreenElem.classList.add('hide');
window.requestAnimationFrame(update);
}
function handleLose() {
setDinoLose();
updateHighScore();
setTimeout(() => {
document.addEventListener('keydown', handleStart, { once: true });
startScreenElem.classList.remove('hide');
document.body.classList.remove('dark');
}, 100);
}