-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
305 lines (261 loc) · 7.79 KB
/
main.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
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
// Settings. If you change the tilesize (256px) or the gridSize (25600px) you
// also have to change those values in the CSS.
const numRocks = 600;
const gridSize = 25600 / 256;
const bugDanceDuration = 2000;
const bugAgitateDuration = 1000;
// One of these is randomly played whenever a rock is flung.
let rockSounds = [];
// One of these is randomly selected for each rock on the map.
let rockImages = [];
// One of these is randomly played whenever you click on a bug or bug-equivalent
// creature.
let bugClickSounds = [];
// These are the files that will be loaded into the arrays above.
const rockSoundsFiles = [
"rock1.m4a",
"rock2.m4a",
"rock3.m4a",
"rock4.m4a",
"rock5.m4a",
];
const rockImagesFiles = [
"rock1.png",
"rock2.png",
"rock3.png",
"rock4.png",
"rock5.png",
];
const bugClickSoundsFiles = [
"clickme1.m4a",
"clickme2.m4a",
"clickme3.m4a",
"clickme4.m4a",
"clickme5.m4a",
];
// A list of all the types of bugs or bug-like creatures. For each one of these,
// it's expected that there is an image for the bug in
// ./assets/image/{bugType}.png, and an "discovery sound" in
// ./assets/sound/{bugType}.m4a.
const bugTypes = [
"unicorn",
"ant",
"bee",
"beetle",
"butterfly",
"cake",
"cockroach",
"dinosaur",
"dragonfly",
"flower",
"fly",
"grasshopper",
"icecream",
"ladybug",
"mantis",
"mosquito",
"moth",
"pizza",
"rainbow",
"scorpion",
"snowman",
"termite",
"unicorn",
];
// A list of all the bug "prototypes" that can be included as a bug component. Each proto is like this:
// {
// type: "unicorn",
// image: "unicorn.png",
// sound: "unicorn.m4a", // This is the sound that is played when the bug is revealed.
// }
let bugProtos = [];
/**
* Picks a random element from a given array.
* @param {Array} array - The array to pick from.
* @returns The randomly selected element from the array.
*/
function choose(array) {
if (array.length === 0) {
return undefined;
}
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
}
/**
* Preload all the assets and initialize the game. This will probably fail if
* any of the assets fails to load, but Nora needs to learn to deal with
* computers failing for no discernible reason.
*/
async function initializeGame() {
// Preload the CSS background texture.
await getImage("forest-tile-512x512.jpg");
// Preload all the "game assets."
rockSounds = await Promise.all(rockSoundsFiles.map(getSound));
rockImages = await Promise.all(rockImagesFiles.map(getImage));
bugClickSounds = await Promise.all(bugClickSoundsFiles.map(getSound));
bugProtos = await Promise.all(bugTypes.map(getBugProto));
}
/**
* Loads the assets for a bug prototype and returns the proto.
*
* @param {string} bugType - The type of bug to load
* @returns A promise wrapping the bug proto.
*/
async function getBugProto(bugType) {
return {
type: bugType,
image: await getImage(`${bugType}.png`),
sound: await getSound(`${bugType}.m4a`),
};
}
/**
* Loads an image from the assets folder.
* @param {string} fileName - The name of the file to load.
* @returns A promise wrapping the image.
*/
function getImage(fileName) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = `./assets/img/${fileName}`;
});
}
/**
* Loads a sound from the assets folder.
* @param {string} fileName - The name of the file to load.
* @returns A promise wrapping the sound.
*/
function getSound(fileName) {
return new Promise((resolve, reject) => {
const audio = new Audio();
audio.oncanplaythrough = resolve(audio);
audio.onerror = reject;
audio.src = `./assets/sound/${fileName}`;
});
}
/**
* Creates an event handler that:
* - throws the rock to reveal the lovely prize beneath. (Will it be a bug, or * something else?)
* - Plays the 'discovery sound' associated with the bug.
*/
function makeFlingRockHandler(discoverySound) {
return () => {
const rock = event.currentTarget;
const rockContainer = rock.closest(".rock-container");
const bug = rockContainer.querySelector(".bug");
const rockFlingDuration = 500 + Math.random() * 500;
animateRock(rock, rockFlingDuration);
rockSounds[Math.floor(Math.random() * rockSounds.length)].play();
setTimeout(() => {
rock.remove();
happyDance(bug);
discoverySound.play();
}, rockFlingDuration);
};
}
/**
* Animate the rock fling
* @param {HTMLElement} rock - The rock element to animate
* @param {number} duration - The duration of the animation in milliseconds
*/
function animateRock(rock, duration) {
const angle = Math.random() * Math.PI * 2;
const distance = 1000 + Math.random() * 1000;
rock.style.transform = `translate(${Math.cos(angle) * distance}px, ${
Math.sin(angle) * distance
}px) rotate(${Math.random() * 720 - 360}deg)`;
rock.style.opacity = "0";
rock.style.transition = `all ${duration}ms cubic-bezier(0.25, 0.1, 0.25, 1)`;
}
/**
* Make the bug -- or bug-like equivalent -- dance.
* @param {HTMLElement} bug - The bug element to animate
*/
function happyDance(bug) {
bug.classList.add("pulsing");
setTimeout(() => {
bug.classList.remove("pulsing");
}, bugDanceDuration);
}
function agitateBug(event) {
choose(bugClickSounds).play();
const bug = event.currentTarget;
if (bug.classList.contains("pulsing")) {
return;
}
bug.classList.add("pulsing");
setTimeout(() => {
bug.classList.remove("pulsing");
}, bugAgitateDuration);
}
/**
* Starts the game after DOMContentLoaded.
*/
async function startGame() {
const splashScreen = document.getElementById("splash-screen");
const scrollableArea = document.getElementById("scrollable-area");
const rockTemplate = document.getElementById("rock-template");
function hideSplashScreen() {
splashScreen.style.display = "none";
scrollableArea.style.display = "grid";
}
function createRockComponent(location) {
const rockContainer = rockTemplate.content
.cloneNode(true)
.querySelector(".rock-container");
rockContainer.style.gridRow = location.x;
rockContainer.style.gridColumn = location.y;
// Choose a random bug to put under the rock.
const randomBugProto = choose(bugProtos);
const rockElement = rockContainer.querySelector(".rock");
rockElement.addEventListener(
"click",
makeFlingRockHandler(randomBugProto.sound)
);
// Select a random rock image
const randomRockImage = choose(rockImages);
// Set the src attribute of the img element inside the rock div
const rockImg = rockElement.querySelector("img");
rockImg.src = randomRockImage.src;
// Make the bug agitate when clicked.
const bug = rockContainer.querySelector(".bug");
bug.addEventListener("click", agitateBug);
// Set the src attribute of the img element inside the bug div
const bugImg = rockContainer.querySelector(".bug img");
bugImg.src = randomBugProto.image.src;
scrollableArea.appendChild(rockContainer);
}
function createRocks() {
const rockLocations = [];
while (rockLocations.length < numRocks) {
const newRockLocation = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize),
};
if (
rockLocations.find(
(l) => l.x === newRockLocation.x && l.y === newRockLocation.y
)
) {
continue;
}
rockLocations.push(newRockLocation);
}
for (const location of rockLocations) {
createRockComponent(location);
}
}
try {
await initializeGame();
} catch (error) {
console.error("Error preloading assets:", error);
alert("Sorry Nora. You broke it.");
return;
}
createRocks();
hideSplashScreen();
}
document.addEventListener("DOMContentLoaded", () => {
startGame();
});