-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (141 loc) · 4.25 KB
/
app.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
var RootElem = document.querySelector(":root");
var GameElem = document.querySelector("#game");
var DinoElem = GameElem.querySelector(".dino");
var ScoreElem = GameElem.querySelector(".score");
var GroundElem = GameElem.querySelector(".ground");
var CactusElem = GroundElem.querySelector(".cactus");
var CenterElem = DinoElem.querySelector(".center");
var GameSpeed = 4000;
var JumpSpeed = (GameSpeed / 10) * 2;
var MaxJump = 250;
var SpeedScale = 1;
var Score = 0;
var GameStarted = false;
var GameOver = false;
var Jumping = false;
var SelfPlayMode = false;
function setCustomProperty(elem, prop, value) {
elem.style.setProperty(prop, value);
}
function handleJump(e) {
if (e.code !== "Space") return;
var audio = document.querySelector(".audio-jump");
audio.play();
Jumping = true;
DinoElem.classList.add("jump");
DinoElem.addEventListener("animationend", function () {
Jumping = false;
DinoElem.classList.remove("jump");
});
}
function shouldJump() {
var minGap = 250;
var cactusXPos = CactusElem.getBoundingClientRect().x;
// Validations
if (cactusXPos <= 0 || Jumping) return false;
if (cactusXPos < minGap) {
return true;
}
return false;
}
function startGame() {
GameStarted = true;
GameElem.classList.add("game-started");
document.addEventListener("keydown", handleJump);
window.requestAnimationFrame(updateGame);
}
function endGame() {
var audio = document.querySelector(".audio-die");
audio.play();
GameOver = true;
GameElem.classList.add("game-over");
document.removeEventListener("keydown", handleJump);
}
// As long as the game is not over, this function is called every frame
function updateGame() {
setCustomProperty(RootElem, "--game-speed", GameSpeed);
setCustomProperty(RootElem, "--jump-speed", JumpSpeed);
setCustomProperty(RootElem, "--max-jump", MaxJump);
setCustomProperty(RootElem, "--speed-scale", SpeedScale);
if (SelfPlayMode) {
if (shouldJump()) {
handleJump({ code: "Space" }); // Simulate a jump
}
}
// Update the score
updateScore();
// Update the cactus
updateCactus();
// Check if game over
if (checkGameOver()) {
endGame();
return; // Won't recurse
}
window.requestAnimationFrame(updateGame);
}
function isCollision(dinoRect, cactusRect) {
// AABB - Axis-aligned bounding box
return (
dinoRect.x < cactusRect.x + cactusRect.width &&
dinoRect.x + dinoRect.width > cactusRect.x &&
dinoRect.y < cactusRect.y + cactusRect.height &&
dinoRect.y + dinoRect.height > cactusRect.y
);
}
function checkGameOver() {
if (GameOver) return true;
var dinoRect = CenterElem.getBoundingClientRect();
var cactusRect = CactusElem.getBoundingClientRect();
if (isCollision(dinoRect, cactusRect)) {
return true;
}
return false;
}
var scoreInterval = 10;
var currentScoreInterval = 0;
function updateScore() {
currentScoreInterval += 1;
if (currentScoreInterval % scoreInterval !== 0) {
return;
}
Score += 1;
if (Score === 0) return;
if (Score % 100 === 0) {
var audio = document.querySelector(".audio-point");
audio.play();
GameSpeed -= SpeedScale;
JumpSpeed = (GameSpeed / 10) * 2;
}
var currentScoreElem = ScoreElem.querySelector(".current-score");
currentScoreElem.innerText = Score.toString().padStart(5, "0");
}
function updateCactus() {
var cactusXPos = CactusElem.getBoundingClientRect().x;
var isOffScreen = cactusXPos > window.innerWidth;
if (isOffScreen === false) return;
var cacti = ["cactus-small-1", "cactus-small-2", "cactus-small-3"];
var randomNum = Math.floor(Math.random() * cacti.length);
var cactus = cacti[randomNum];
CactusElem.classList.remove(
"cactus-small-1",
"cactus-small-2",
"cactus-small-3"
);
CactusElem.classList.add(cactus);
}
function fitScreen() {
var width = window.innerWidth;
var height = window.innerHeight / 2;
GameElem.style.width = width + "px";
GameElem.style.height = height + "px";
GameElem.style.zoom = 1.5;
}
window.addEventListener("load", function () {
fitScreen();
window.addEventListener("resize", fitScreen);
var selfPlayElem = document.querySelector("#selfplay");
selfPlayElem.addEventListener("change", function () {
SelfPlayMode = selfPlayElem.checked;
});
document.addEventListener("keydown", startGame, { once: true });
});