-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
565 lines (518 loc) · 15.7 KB
/
script.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
const isMobile = navigator.userAgent.match(
/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/
)
? true
: false;
//console.log(`Mobile: ${isMobile}`);
//document.getElementById("mobile").innerText = isMobile;
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const LEADERBOARD = "leaderboard";
const DIFFICULTY = "difficulty";
const MUTED = "muted";
const VICTORY = "victory";
const color = ["red", "blue", "yellow", "green"];
const buttons = document.getElementsByClassName("simon-btn");
const startButton = document.getElementById("start");
const topInitials = document.getElementById("top-initials");
const topScore = document.getElementById("top-score");
const startAudio = new Audio("./audio/Shall-we-play-a-game.mp3");
const gameButtons = document.getElementsByClassName("simon-btn");
const gameOverAudio = new Audio("./audio/Pacman-death-sound.mp3");
const victoryAudio = new Audio("./audio/victory.mp3");
const scoreOutput = document.getElementById("score");
const modalClose = document.getElementsByClassName("close")[0];
const settingsModal = document.getElementById("settingsModal");
const hiScoreModal = document.getElementById("hiScoreModal");
const playerInitials = document.getElementById("initials");
const rankModal = document.getElementById("yourRank");
const fullLeaderboard = document.getElementById("full-Leader-Modal");
const maxLevelInput = document.getElementById("maxLevel");
const difficultyInput = document.getElementById("difficulty");
const muteControlImage = document.getElementById("mute");
const mutedIcon = "./images/icon_mute.png";
const unmutedIcon = "./images/icon_unmute.png";
const offOpacity = 0.5;
const onOpacity = 1;
const maxAudioChannels = 6;
const baseSpeed = 500;
// TO-DO add these to a settings menu
var difficultyLevel = localStorage.getItem(DIFFICULTY);
difficultyLevel ??= 1;
var currentLevel = 0;
const countdownSecondsToStart = 0;
const startingLevel = 1;
const tooSlowToPress = 5;
var victoryLevel = localStorage.getItem(VICTORY);
victoryLevel ??= 10;
var simonSpeed = 1;
//
maxLevelInput.value = victoryLevel;
difficultyInput.value = difficultyLevel;
const pressDuration = baseSpeed / 4;
const releaseDuration = pressDuration / 2;
var tooSlow;
var colorEnum;
var gameActive = false;
var replay = false;
var wait = false;
var playerScore = 0;
var leaderboardPosition = 6;
var currentPress = -1;
var simonSays = new Array();
var leaderboardArray = new Array();
var audio = [];
var audioChannels = [];
var currentAudioChannel = 0;
var timeouts = [];
const loadLocal = localStorage.getItem(LEADERBOARD);
audio[0] = "./audio/simonSound1.mp3";
audio[1] = "./audio/simonSound2.mp3";
audio[2] = "./audio/simonSound3.mp3";
audio[3] = "./audio/simonSound4.mp3";
startAudio.volume = 0.2;
gameOverAudio.volume = 0.2;
const str = localStorage.getItem(MUTED);
isMuted = str == "true" ? true : false;
muteControlImage.setAttribute("src", isMuted ? mutedIcon : unmutedIcon);
function initialize() {
updateSimonSpeed();
if (loadLocal === null || loadLocal === undefined) {
for (let a = 0; a < 5; a++) {
let obj = {
initials: "---",
score: 0,
};
leaderboardArray[a] = obj;
}
} else {
var obj = JSON.parse(loadLocal);
for (var i in obj) {
leaderboardArray.push(obj[i]);
}
// console.log(leaderboardArray);
}
updateLeaderboard();
// add listeners to the 4 game buttons
for (let i = 0; i < gameButtons.length; i++) {
preLoadButtonSound(i);
gameButtons[i].addEventListener("click", (clickEvent) => {
colorEnum = color.indexOf(clickEvent.target.id);
//console.log("Down: ", clickEvent.target.id);
if (gameActive && !wait) {
currentPress++;
//console.log(`Pressed: ${colorEnum} turn: ${currentPress}`);
clearTimeout(tooSlow);
tooSlowTimer();
checkforMatch(colorEnum);
buttonAction(colorEnum);
}
});
}
assignEventListeners();
initializeSettings();
}
function assignEventListeners() {
startButton.addEventListener("click", (clickEvent) => {
clearTimeout(tooSlow);
startSimon();
});
document.onclick = (clickEvent) => {
//console.log("CLICKED: ", clickEvent.target.id);
if (clickEvent.target.id == "settings") {
settingsModal.style.display =
settingsModal.style.display == "block" ? "none" : "flex";
}
if (clickEvent.target.id == "mute") {
isMuted = !isMuted;
muteControlImage.setAttribute("src", isMuted ? mutedIcon : unmutedIcon);
localStorage.setItem(MUTED, isMuted);
}
if (clickEvent.target.className == "close") {
settingsModal.style.display = "none";
}
if (clickEvent.target == settingsModal) {
settingsModal.style.display = "none";
}
if (clickEvent.target.id == "submit") {
closeLeaderModal();
}
if (clickEvent.target.id == "reset") clearLeaderboard();
if (clickEvent.target.id.includes("top-")) showFullLeaderboard();
if (clickEvent.target == fullLeaderboard) {
fullLeaderboard.style.display = "none";
}
};
document.onkeydown = function () {
if (window.event.keyCode == "13" && hiScoreModal.style.display == "flex") {
closeLeaderModal();
}
};
}
function buttonAction(num) {
buttonSound(num);
buttonEffect(num);
}
function startSimon() {
//+console.log(isMuted);
if (victoryAudio.currentTime > 0) {
victoryAudio.pause();
victoryAudio.currentTime = 0;
}
if (gameActive) {
gameActive = false;
if (startAudio.currentTime > 0) {
startAudio.pause();
startAudio.currentTime = 0;
}
scoreOutput.innerText = "RDY";
startButton.setAttribute("class", "hub-btn start-click");
startButton.innerText = "Start";
} else {
initializeButtonSounds();
updateDisplay(0);
playerScore = 0;
gameActive = true;
wait = true;
currentPress = -1;
currentLevel = 0;
currentLevel++;
simonSays = new Array();
// typically the player will start at level one, but this is configurable
for (let a = 0; a < startingLevel; a++) {
simonSays[a] = randomColor();
}
currentLevel = simonSays.length;
// plays the intro audio and will wait till it's complete to start the clock
console.log(isMuted);
startButton.setAttribute("class", "hub-btn stop-click");
startButton.innerText = "Stop";
startAudio.onended = function () {
postStartAudio();
};
if (isMuted) {
postStartAudio();
} else {
startAudio.play();
}
}
}
function postStartAudio() {
if (!gameActive) return;
startCountdown();
}
// this is simon's brain. Simon shows the player the sequence
function playback() {
clearTimeout(tooSlow);
if (!replay) updateDisplay(currentLevel);
for (let i = 0; i < simonSays.length; i++) {
setTimeout(() => {
//console.log(simonSays[i]);
buttonAction(simonSays[i]);
if (i == currentLevel - 1) {
//console.log("Playback Complete");
wait = false;
if (!replay) {
tooSlowTimer();
}
}
}, i * simonSpeed);
}
}
// checks to see if the player pressed the correct button at the right time
function checkforMatch(buttonIndex) {
if (simonSays[currentPress] == buttonIndex) {
// console.log("Correct");
if (currentPress == currentLevel - 1) {
// console.log("Success");
if (currentLevel >= victoryLevel) {
victoryMarch();
} else {
levelComplete();
}
}
} else {
// console.log("Wrong");
gameOver();
}
}
// victory march
function victoryMarch() {
playerScore = victoryLevel;
updateLeaderboard();
clearTimeout(tooSlow);
scoreOutput.innerText = "WIN";
replay = true;
wait = true;
if (!isMuted) victoryAudio.play();
setTimeout(() => {
playback();
setTimeout(() => {
openLeaderModal();
}, currentLevel * simonSpeed + 1000);
}, 1000);
}
// when the player clicks the wrong sequence start the gameover playback
function gameOver() {
updateLeaderboard();
scoreOutput.innerText = "LOSE";
replay = true;
wait = true;
gameOverAudio.onended = function () {
gameOverRecap();
};
if (isMuted) {
gameOverRecap();
} else {
gameOverAudio.play();
}
}
function gameOverRecap() {
playback();
setTimeout(() => {
openLeaderModal();
}, currentLevel * simonSpeed + 1000);
}
function openLeaderModal() {
replay = false;
if (leaderboardPosition <= 5) {
console.log("Position: ", leaderboardPosition);
yourRank.innerText = leaderboardPosition + 1;
initials.value = "";
hiScoreModal.style.display = "flex";
}
startSimon();
}
// after player matches simon add one more level and playback
function levelComplete() {
wait = true;
simonSays.push(randomColor());
currentPress = -1;
playerScore = currentLevel;
currentLevel++;
updateSimonSpeed();
// console.log(`current press ${currentPress} | Current level ${currentLevel}`);
setTimeout(() => {
playback();
}, 1000);
}
// update the digital display to show the number of points
function updateDisplay(num) {
scoreOutput.innerText = padNum(num);
}
// random color picker for Simon
function randomColor() {
return Math.floor(Math.random() * 4);
}
// Initial countdown after clicking start
function startCountdown() {
for (let i = countdownSecondsToStart; i >= 0; i--) {
setTimeout(() => {
//console.log("Countdown", i);
updateDisplay(i);
}, (countdownSecondsToStart - i) * 1000);
}
setTimeout(() => {
//console.log("Countdown complete");
playback();
}, (1 + countdownSecondsToStart) * 1000);
}
// #region button effects
async function initializeButtonSounds() {
if (isMuted) return;
//console.log(audioChannels);
for (a of audioChannels) {
playTrack(a);
buttonEffect(audioChannels.indexOf(a));
await sleep(250);
//console.log("Sleep Done");
}
}
function buttonSound(buttonIndex) {
playTrack(audioChannels[buttonIndex]);
}
// Add all your audio paths to an array
// then use a for loop to iterate through that array
// for (let i = 0; i < gameButtons.length; i++) {
// preLoadButtonSound(i);
// }
// once your files are pre-load, you will need initilize the sounds
// to work on iOS by trigging some automated sequence via userInput
async function preLoadButtonSound(buttonIndex) {
// the initializer will call load file.
// this async function means that it will not add the track
// until it has completed loading the track
audioChannels[buttonIndex] = await loadFile(audio[buttonIndex]);
}
// when loadFile is called this function will get the file
// and return it to preLoadButtonSound
// this loads the decoded sound file into memory
async function loadFile(filepath) {
const response = await fetch(filepath);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
return audioBuffer;
}
function playTrack(audioBuffer) {
const trackSource = audioContext.createBufferSource();
trackSource.buffer = audioBuffer;
trackSource.connect(audioContext.destination);
if (audioContext.currentTime != 0) {
audioContext.currentTime = 0;
}
if (!isMuted) trackSource.start();
}
function buttonEffect(buttonIndex) {
let opacity = offOpacity;
$(buttons[buttonIndex]).fadeTo(pressDuration, onOpacity, () => {
$(buttons[buttonIndex]).fadeTo(pressDuration, offOpacity);
});
}
//#endregion
// #region Leaderboard
function updateLeaderboard() {
leaderboardPosition = 6;
//console.log(`Score: ${playerScore}`);
for (let r = 4; r >= 0; r--) {
if (playerScore > leaderboardArray[r].score) {
leaderboardPosition = r;
}
}
//console.log("New Rank: ", leaderboardPosition);
if (leaderboardPosition != 6) {
leaderboardArray.insert(
leaderboardPosition,
leader(leaderboardPosition, playerInitials.value, playerScore)
);
leaderboardArray.pop();
}
saveLeaderboard();
updateTopScore();
}
function clearLeaderboard() {
for (let a = 0; a < 5; a++) {
let obj = {
initials: "---",
score: 0,
};
leaderboardArray[a] = obj;
}
saveLeaderboard();
updateTopScore();
}
function closeLeaderModal() {
leaderboardArray[leaderboardPosition].initials = playerInitials.value;
saveLeaderboard();
updateTopScore();
hiScoreModal.style.display = "none";
}
function updateTopScore() {
topInitials.innerText = leaderboardArray[0].initials;
topScore.innerText = padNum(leaderboardArray[0].score);
}
function leader(rank, initials, score) {
let newLeader = {
initials: initials,
score: score,
};
return newLeader;
}
function showFullLeaderboard() {
fullLeaderboard.style.display = "flex";
let leaderTable = document.getElementById("leader-table");
leaderTable.innerHTML = "";
for (let a = 0; a < 5; a++) {
let leftCol = document.createElement("td");
let middleCol = document.createElement("td");
let rightCol = document.createElement("td");
leftCol.innerText = a + 1;
middleCol.innerText = leaderboardArray[a].initials;
rightCol.innerText = padNum(leaderboardArray[a].score);
let row = document.createElement("tr");
row.appendChild(leftCol);
row.appendChild(middleCol);
row.appendChild(rightCol);
leaderTable.appendChild(row);
}
}
function saveLeaderboard() {
localStorage.setItem(LEADERBOARD, JSON.stringify(leaderboardArray));
}
// #endregion
// #region Settings
function initializeSettings() {
localStorage.setItem(VICTORY, victoryLevel);
localStorage.setItem(DIFFICULTY, difficultyLevel);
document.getElementById("maxLevel").addEventListener("change", (event) => {
victoryLevel = event.target.value;
});
const levelButtons = document.getElementsByClassName("spinner-btn");
Array.from(levelButtons).forEach((btn) =>
btn.addEventListener("click", (event) => {
victoryLevel = document.getElementById("maxLevel").value;
difficultyLevel = document.getElementById("difficulty").value;
console.log(`V: ${victoryLevel} | D: ${difficultyLevel}`);
localStorage.setItem(VICTORY, victoryLevel);
localStorage.setItem(DIFFICULTY, difficultyLevel);
})
);
}
// #endregion
// #region helpers
async function sleep(ms) {
return await new Promise((r) => setTimeout(r, ms));
}
function tooSlowTimer() {
clearTimeout(tooSlow);
tooSlow = setTimeout(() => {
gameOver();
}, tooSlowToPress * 1000);
}
function padNum(num) {
let numString = num.toString();
while (numString.length < 3) numString = "0" + numString;
return numString;
}
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item);
};
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
function updateSimonSpeed() {
simonSpeed = clamp(
-5 * Math.pow(currentLevel, 2) -
5 * currentLevel +
(1510 - 1000 * ((difficultyLevel - 1) / 5)),
250,
1750
);
//console.log("Speed: ", simonSpeed);
}
// #endregion
// #region Responsive View
window.addEventListener("DOMContentLoaded", () => {
computeSizing();
});
window.addEventListener("resize", () => {
computeSizing();
});
function computeSizing() {
let html = document.getElementsByTagName("html")[0];
let upper = document.getElementById("top-scoreboard");
let lower = document.getElementById("lower-section");
let housing = document.getElementById("housing-content");
let screenWidth = screen.width;
let maxSize =
Math.min(lower.clientHeight, lower.clientWidth) * (isMobile ? 0.94 : 0.98);
let strSize = Math.max(isMobile ? 200 : 350, maxSize) + "px";
let fontSize = Math.min(lower.clientHeight, lower.clientWidth) * 0.05;
let strFont = fontSize + "px";
//console.log(`Set Simon Size to : ${strSize}`);
//console.log(`Set Base Font ${fontSize}`);
housing.style.minHeight = strSize;
housing.style.minWidth = strSize;
housing.style.height = strSize;
housing.style.width = strSize;
html.style.setProperty("--font-size", strFont);
}
// #endregion
initialize();