-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
227 lines (185 loc) · 5.35 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
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
"use strict";
const gameScreen = document.querySelector(".main-game");
const character = document.querySelector("#cat");
const overlay = document.querySelector(".overlay");
const highScoreTextEle = document.querySelector(".highscore-text");
const currentScoreEle = document.querySelector(".currentscore");
const highScoreEle = document.querySelector(".highscore");
const announcementEle = document.querySelector(".announcement");
let obstacles = [];
let obstacleCounter = 0;
let [highScore, currentScore] = [0, 0];
let level = 0;
let playingGame = false;
let timeoutObstacle, updateGame;
const levelInfo = [
{
speed: 2,
min: 1.2,
max: 4,
},
{
speed: 1.8,
min: 0.9,
max: 3,
},
{
speed: 1.5,
min: 0.8,
max: 2,
},
];
const getSpeed = (level) => {
return `${levelInfo[level].speed}s`;
};
const initGame = () => {
setScoreBoard();
setObstacle();
updateGame = setInterval(() => {
checkForCollision();
updateScore();
}, 100);
};
const setObstacle = () => {
const lvl = levelInfo[level];
const waitTime = Math.random() * (lvl.max - lvl.min) + lvl.min;
timeoutObstacle = setTimeout(() => {
generateObstacles();
setObstacle();
}, waitTime * 1000);
};
const resetGameElements = function () {
obstacles.forEach((obstacle) => {
obstacle.remove();
});
character.removeAttribute("style");
obstacles = [];
currentScore = 0;
level = 0;
};
const pauseGameElements = () => {
character.style.animationPlayState = "paused";
obstacles.forEach((obstacle) => {
obstacle.style.animationPlayState = "paused";
});
};
const showAnnouncement = () => {
announcementEle.classList.remove("hidden");
setTimeout(() => {
announcementEle.classList.add("hidden");
}, 1500);
};
const setScoreBoard = () => {
setScore(highScoreEle, highScore);
setScore(currentScoreEle, currentScore);
highScoreTextEle.innerText = "Highscore";
};
const setScore = (screenEle, type) => {
screenEle.innerText = String(type).padStart(5, "0");
};
const updateScore = () => {
currentScore++;
if ((currentScore % 100 === 0) & (level < levelInfo.length - 1)) {
level++;
showAnnouncement();
}
setScore(currentScoreEle, currentScore);
};
const updateHighScore = () => {
if (currentScore <= highScore) return;
highScore = currentScore;
};
const jump = () => {
if (character.classList !== "jump") {
character.classList.add("jump");
}
character.addEventListener("animationend", function () {
this.classList.remove("jump");
});
};
const clearIntervals = () => {
clearInterval(updateGame);
clearTimeout(timeoutObstacle);
};
const clearObstacle = function () {
this.remove();
obstacles.shift();
};
const generateObstacles = () => {
const randomImgNum = Math.ceil(Math.random() * 3);
const obstacle = document.createElement("div");
obstacle.classList.add("obstacle");
obstacleCounter++;
if (obstacleCounter % 8 === 0) {
obstacle.dataset.notObstacle = "true";
obstacle.style.backgroundImage = "url(assets/fish.png)";
} else {
obstacle.style.backgroundImage = `url(assets/water_0${randomImgNum}.png)`;
}
obstacle.style.animationDuration = getSpeed(level);
console.log(`Speed set at level ${level}`);
obstacles.push(obstacle);
gameScreen.appendChild(obstacle);
obstacle.addEventListener("animationend", clearObstacle);
};
const updateOverlayContent = () => {
// const title = overlay.querySelector(".overlay__title");
const content = overlay.querySelector(".overlay__text");
// title.innerText = "Game Over";
content.innerHTML =
"Game Over! </br></br><small>* <span class='spacebar'>space</span> to try again * </small>";
};
const getPosition = (element, side) => {
const parentPosition = gameScreen.getBoundingClientRect()[side];
const elementPosition = element.getBoundingClientRect()[side];
return side === "left"
? elementPosition - parentPosition
: parentPosition - elementPosition;
};
const checkForCollision = () => {
if (obstacles.length === 0) return;
const obstacle = obstacles[0];
const characterInfo = character.getBoundingClientRect();
const obstacleLeft = getPosition(obstacle, "left");
const characterBottom = getPosition(character, "bottom");
// console.log(`left: ${obstacleLeft}`);
// console.log(`bottom: ${characterBottom}`);
if (
characterBottom < 48 &&
obstacleLeft < characterInfo.width &&
obstacleLeft > 0
) {
if (obstacle.hasAttribute("data-not-obstacle")) {
if (obstacle.hasAttribute("data-points-added")) return;
currentScore += 30;
obstacle.dataset.pointsAdded = "true";
const addToScore = character.querySelector(".add-to-score");
addToScore.classList.add("animation");
addToScore.addEventListener("animationend", (e) => {
e.stopPropagation();
addToScore.classList.remove("animation");
});
obstacle.style.opacity = "0";
} else {
clearIntervals();
pauseGameElements();
overlay.classList.remove("hidden");
character.style.backgroundImage = "url(assets/cat_gameover.png)";
playingGame = false;
}
}
};
document.addEventListener("keydown", function (e) {
if (e.code === "Space") {
if (!playingGame) {
overlay.classList.add("hidden");
playingGame = true;
updateOverlayContent();
updateHighScore();
resetGameElements();
initGame();
} else {
jump();
}
}
});