-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
347 lines (292 loc) · 10.2 KB
/
Copy pathindex.js
File metadata and controls
347 lines (292 loc) · 10.2 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Graphics and UI
const playArea = document.getElementsByTagName("main")[0]; // the primary container in the body
const instructions = document.getElementById("instructions");
const catImages = [];
const frownEmoji = "\uD83D\uDE41";
const heartEmoji = "\u2764\uFE0F";
let emojiCounter = 0;
const maxEmojis = 4000;
const interval = 150; // Adjust the interval (in milliseconds) for adding emojis to the window at the end
const fadeIn = "fadeIn 0.5s ease-in forwards";
const fadeOut = "fadeOut 0.5s ease-out forwards";
const fadeInOut = "fadeInOut 2s ease-in-out forwards";
const fadeInOut4s = "fadeInOut 4s ease-in-out forwards";
// Audio
var currentCatSFX = 1;
// Statistics
let clickCounter = 0;
// let timer = 2;
let timer = 9999;
// const debugAllCatsFound = false;
const debugAllCatsFound = true;
// let timer = 3;
var catsFound = 0;
var totalCats = 3;
// Areas
let clickedAreas = [];
let catLocations = [];
let allAreaNodes = [];
let arrayFromClickables = Array.from(allAreaNodes); // Convert NodeList to Array (for easier manipulation)
let areaNames = [
"that cupboard",
"that drawer",
"the sink",
"the curtain",
"the closet",
];
// Interactables
let goToBedroomButton =
document.getElementById("goToBedroomButton");
let goToKitchenButton =
document.getElementById("goToKitchenButton");
// When DOM is fully loaded
document.addEventListener("DOMContentLoaded", function () {
let allAreaNodes = document.querySelectorAll('[id^="area_"]'); // Into allAreaNodes NodeList, place all elements with id starting with "area_"
arrayFromClickables = Array.from(allAreaNodes); // Convert NodeList to Array (for easier manipulation)
allAreaNodes.forEach((e) => {
e.addEventListener("click", addAreaToClickedAreas);
});
});
function addAreaToClickedAreas(e) {
if (!clickedAreas.includes(e.target.id)) {
clickedAreas.push(e.target.id);
checkForCat(e, debugAllCatsFound);
} else {
checkForCat(e, debugAllCatsFound);
}
}
// Checks if the area clicked is one of the randomized cat locations
// Triggers visuals if an unfound cat was found or visually notifies if it was not
function checkForCat(hidingPlace, debugAllCatsFound) {
if (catLocations.includes(hidingPlace.target.id)) {
if (debugAllCatsFound) {
catsFound = totalCats;
} else {
catsFound++;
}
document.getElementById("foundCounterValue").innerHTML =
catsFound;
hidingPlace.target.children[0].style.animation = fadeIn;
showNotification(
"❤️",
hidingPlace.clientX,
hidingPlace.clientY
);
playNextCatSFX();
} else {
showNotification(
"Darn, no cat here... 😟",
hidingPlace.clientX,
hidingPlace.clientY,
"catNotFound"
);
}
if (catsFound >= totalCats) {
gameOver(true);
}
}
function playNextCatSFX() {
var audio = new Audio(`./sfx/meow_${currentCatSFX}.mp3`);
audio.play();
currentCatSFX++;
}
function startGame() {
var audio = new Audio('./sfx/meow_3.mp3');
audio.play();
placeCatsRandomly();
setTimeout(function () {
goToBedroomButton.style.display = "block";
goToBedroomButton.style.opacity = 1;
}, 500);
document.getElementById("playArea").style.animation = fadeIn;
document.getElementById("playArea").style.display = "flex";
goToKitchenButton.style.display = "none";
document.getElementById("splashPage").remove();
runTimer();
}
// Randomizes the available clickable areas and adds the first 3 from the
// randomized array into the catLocations var
function placeCatsRandomly() {
const shuffledArray = arrayFromClickables.sort(
() => Math.random() - 0.5
);
const selectedAreas = shuffledArray.slice(0, totalCats);
console.log(selectedAreas);
selectedAreas.forEach((area, index, array) => {
catLocations.push(area.id);
var container = document.getElementById(area.id);
container.innerHTML = `<img class='catGif' id='cat${index}' src='./img/cat${index}.gif'>`;
});
}
// Shows a notification modal at the specified coordinates
function showNotification(message, x, y, type) {
const notificationModal = document.createElement("div");
notificationModal.className = "notificationModal";
if (type === "catNotFound") {
notificationModal.classList.add("catNotFoundDialog");
}
notificationModal.style.top = y + "px";
notificationModal.style.left = x + "px";
notificationModal.style.zIndex = 7;
notificationModal.innerHTML = message;
document.body.appendChild(notificationModal);
// Triggers the fadeInOut animation after a short delay
setTimeout(() => {
notificationModal.style.animation = fadeInOut;
notificationModal.style.position = "fixed"; // Ensure the modal stays fixed relative to the viewport
notificationModal.style.width = "auto";
notificationModal.style.padding = "10px";
notificationModal.style.transform =
"translate(-50%, -50%) scale(1.5)"; // Center the modal
// Removes the modal after the fade-out animation completes
setTimeout(() => {
notificationModal.remove();
}, 2200);
}, 10);
}
// Changes the 'location' the user is in, including animated crossfade and
// updating the clickable areas to match the new background room image.
function goToLocation(location) {
playArea.style.animation = fadeOut;
if (location === "bedroom") {
goToBedroom();
setTimeout(function () {
document.getElementById("locationImage").src =
"./img/bedroom.jpg";
playArea.style.animation = fadeIn;
}, 10);
} else if (location === "kitchen") {
goToKitchen();
} else {
console.error(
"Failed to goToLocation() with an invalid parameter."
);
}
}
function goToKitchen() {
goToKitchenButton.style.display = "none";
// Hide the existing Bedroom rows
document.querySelector(".bedroom").classList.add("hidden");
// Show the Kitchen rows
document.querySelector(".kitchen").classList.remove("hidden");
goToBedroomButton.style.display = "block";
// Change the image source after a delay to synchronize with the fade-out animation
setTimeout(function () {
// Change the image source after fade-out is complete
changeImage("kitchen");
// Apply fade-in animation to the image container
playArea.style.animation = fadeIn;
}, 10);
}
function goToBedroom() {
// Hide the existing goToKitchenButton
goToBedroomButton.style.display = "none";
// Hide the existing Kitchen rows
document.querySelector(".kitchen").classList.add("hidden");
//Show the Bedroom rows
document.querySelector(".bedroom").classList.remove("hidden");
goToKitchenButton.style.display = "block";
// Change the image source after a delay to synchronize with the fade-out animation
setTimeout(function () {
changeImage("bedroom");
// Apply fade-in animation to the image container
playArea.style.animation = fadeIn;
}, 10);
}
function changeImage(location) {
if (location == "bedroom") {
document.getElementById("locationImage").src =
"./img/bedroom.jpg";
} else if (location == "kitchen") {
document.getElementById("locationImage").src =
"./img/kitchen.jpg";
}
}
function showInstructions() {
instructions.style.display = "block";
let letsGoButton = document.getElementById("letsGoButton");
letsGoButton.remove();
if (
Math.max(
document.documentElement.clientWidth || 0,
window.innerWidth || 0
) <= 500
) {
splashTitle.remove();
}
}
//
function addEmojiRandomly(message, x, y) {
const emojiModal = document.createElement("div");
emojiModal.className = "emojiModal";
emojiModal.style.top = y + "px";
emojiModal.style.left = x + "px";
emojiModal.innerHTML = message;
document.getElementById("playArea").appendChild(emojiModal);
setTimeout(() => {
setTimeout(() => {
emojiModal.remove();
}, 40000);
}, 10);
}
function addEmojiRandomLocation(emoji) {
const emojiWidth = 50;
const emojiHeight = 50;
const playAreaWidth = playArea.offsetWidth - emojiWidth;
const playAreaHeight = playArea.offsetHeight - emojiHeight;
let randomX = Math.floor(Math.random() * playAreaWidth);
let randomY = Math.floor(Math.random() * (playAreaHeight - 20));
emojiCounter++;
if (emojiCounter >= maxEmojis) {
clearInterval(emojiInterval);
}
addEmojiRandomly(emoji, randomX, randomY);
}
// Begins the timer countdown and directs the game to the appropriate end state
function runTimer() {
var interval = window.setInterval(function () {
if (timer <= 0) {
clearInterval(interval);
gameOver(false);
} else if (catsFound >= totalCats) {
clearInterval(interval);
} else {
timer--;
document.getElementById("timerValue").innerHTML = timer;
}
}, 1000);
}
function successful() {
var successMessage = document.getElementById("successMessage");
successMessage.style.display = "block";
console.log("Won at the game");
// TODO: Make emojis slowly float up
// Adds a heart emoji at a random location
const emojiInterval = setInterval(() => {
addEmojiRandomLocation(heartEmoji);
}, interval);
}
function failure() {
var failureMessage = document.getElementById("failureMessage");
failureMessage.style.display = "block";
const emojiInterval = setInterval(() => {
addEmojiRandomLocation(frownEmoji);
}, interval);
}
// Removes all clickable areas from the the screen on fail state, then displays the appropriate modal
function gameOver(outcome) {
let goToLocationButtons = Array.from(
document.getElementsByClassName("goToLocationButton")
);
goToLocationButtons.forEach((e) => {
e.style.display = "none";
});
arrayFromClickables.forEach((e) => {
e.style.display = "none";
});
if (outcome) {
successful();
} else {
failure();
}
}