/
app.js
406 lines (333 loc) · 10.9 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
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
$(() => {
// console.log('loaded bro');
/********************************
// SAY HELLO, STEVEN
********************************/
$('#steven-says').typewrite({
'delay': 100
});
/********************************
// SOME GLOBAL VARIABLES MY DUDE
********************************/
let started = false;
let hero = false; //mode is 'normal' by default
let currentGame = {
"round": 1,
"userGuess": 0
}
/****************
// PLAY SOUND
****************/
const playSound = (sound) => {
if (sound === "top"){
$('#top-sound')[0].play();
}
else if (sound === "right"){
$('#right-sound')[0].play();
}
else if (sound === "left"){
$('#left-sound')[0].play();
}
else if (sound === "bottom"){
$('#bottom-sound')[0].play();
}
}
/*************************************************
// DIAMOND CLASS AND METHODS FOR SOUNDS AND GLOW
*************************************************/
class Diamond {
constructor(diamond, sound) {
this.diamond = document.getElementById(diamond);
this.chime = () => {
playSound(sound);
};
}
}
Diamond.prototype.glowOff = function(){ // inherited by ALL diamond instances
this.diamond.classList.remove("active");
}
Diamond.prototype.glow = function(){ // inherited by ALL diamond instances;
this.glowOff(); // make sure it's not glowing first, this causes it to not glow on future instances
this.diamond.classList.add("active");
var glowing = this; // variable so we can pass to the timeout function
setTimeout(function() {
glowing.glowOff()
}, 1500); // remove the glowing class after a lil bit
};
//hi ho hi ho
const topDiamond = new Diamond("top-diamond", "top");
const rightDiamond = new Diamond("right-diamond", "right");
const leftDiamond = new Diamond("left-diamond", "left");
const bottomDiamond = new Diamond("bottom-diamond", "bottom");
const allDiamonds = [topDiamond, rightDiamond, leftDiamond, rightDiamond];
/***************
// GAME OBJECT
***************/
const gameElements = {
"round" : document.getElementById("round"),
"diamonds" : document.getElementById("diamonds"),
"normal" : document.getElementById("normal"),
"hero" : document.getElementById("hero")
}
/*************************
// SIMON SEQUENCE OBJECT
*************************/
let simonSequence = {
"current" : [],
"playing" : false,
"index" : 0,
"play" : function(){
this.playing = true; // currently playing (user cannot guess during this time)
currentGame.userGuess = 0; // set the user's input back to zero
let tempSequenceVariable = this; // so we can pass to timeout function
setTimeout(function() {// wait and then play sequence
let currentDiamond = tempSequenceVariable.current[tempSequenceVariable.index];
currentDiamond.glow();
currentDiamond.chime();
tempSequenceVariable.index++; // increment the counter
if (tempSequenceVariable.index < currentGame.round) { // if the counter < the current round, call the loop function
tempSequenceVariable.play(); // again which will trigger another sequence play
} else {
tempSequenceVariable.playing = false; // when sequence isn't playing, the user can now guess
}
}, 2200) // speed of the timeout
},
"generate": function(){ // Generate a random sequence
this.index = 0; // Play sequence from the beginning
this.current = [];// Empty the previous sequence
let currentSequence = this.current; // in order to pass through for loop
for (let g = 0; g < 5; g++) { //resetting to 20 levels, good luck!
let randomGem = allDiamonds[Math.floor(Math.random() * allDiamonds.length)]
//create a new array of these 20 random gems
currentSequence.push(randomGem);
console.log(currentSequence[g].diamond); // so we can cheat :)
}
}
};
/****************
// UPDATE ROUND
****************/
function updateRound() { // display current round on page
gameElements.round.innerHTML = currentGame.round;
}
/****************
// RESTART GAME
****************/
const restartGame = () => { // new game
if(started === false){ // if this is the first game played set started to true
started = true;
} // else it's been started
currentGame = { // reset values
"round": 1,
"userGuess": 0
}
updateRound(); // update round
allGlow();
simonSequence.generate(); // new sequence
simonSequence.play(); // play sequence
}
/*******************
// CHANGE GAME MODE
*******************/
function setMode(mode){ // Sets the game mode to the mode passed (Either crystal gem or normal)
if (mode === '#hero'){
hero = true; //game mode is now crystal gem hero mode
}
else if (mode === '#normal') {
hero = false; // game mode is now in normal mode
}
};
/***********************
// SPIN ME RIGHT ROUND
***********************/
const spinMoves = () => {
$('#diamonds').velocity({rotateZ: "+=405" }); //because it is already rotated +45deg
}
/********************
// CHECK FOR WINNER
********************/
const checkWin = () => {
if(currentGame.userGuess >= simonSequence.current.length){
allGlow();
spinMoves();
youWin(); //call the winning modal
setTimeout(function() {
restartGame();
}, 5000); // Wait a bit, then restart the game
} else if(currentGame.userGuess >= currentGame.round){ // player cleared round, but not completed all levels
currentGame.round++; // move to next round
updateRound();
simonSequence.index = 0; // reset the sequence index to the beginning
simonSequence.play(); // play the sequence
}
}
/***************
// PLAYER GUESS
***************/
const guess = (diamond) => { // checks a users guess (diamond clicked on)
if(simonSequence.current.length === 0){
diamond.chime(); //ding dong ding
}
if(simonSequence.playing || !started){
return; // don't do anything if sequence is currently playing
}
if(simonSequence.current[currentGame.userGuess] === diamond){ // incorrect guess
currentGame.userGuess++; // increase input
diamond.chime();
diamond.glow();
checkWin(); // did they win?
}
else { // they guessed incorrectly
if(hero === true){
youLose();
setTimeout(() => {
$('#loseModal').fadeOut();
restartGame();
}, 4000); // wait a bit for modal to be read, then restart the game
}
else {
currentGame.userGuess = 0; // reset player's guess input
simonSequence.index = 0; // restart current sequence
$('#oops-sound')[0].play();
$('#steven-says').html('Ooops! Try again.').typewrite({
'delay': 100
});
setTimeout(() => { // wait for the error sound to finish, then replay sequence
$('#steven-says').html('You can do it!').typewrite({
'delay': 100
});
simonSequence.playing = false;
simonSequence.play();
}, 1800);
}
}
}
/***********************************
// ALL GLOW FOR FUTURE WIN SEQUENCE
***********************************/
const allGlow = () => {
topDiamond.glow();
leftDiamond.glow();
rightDiamond.glow();
bottomDiamond.glow();
}
/********************************
// INSTRUCTIONS BUTTON AND MODAL
********************************/
const $instructionsModal = $('#instructionsModal');
const $button = $('#instructions');
const $close = $('.close');
// when the user clicks on the button, open the modal
// added button sound via help from stack overflow
$button.on('click', () => {
$instructionsModal.fadeIn();
let $audioElement = $('<audio>');
$audioElement.attr('src', 'https://s3-us-west-2.amazonaws.com/simonuniversesounds/220173__gameaudio__spacey-1up-power-up.wav');
$button.append($audioElement);
$audioElement[0].play();
});
// when the user clicks on <span> (x), close the modal
$close.on('click', () => {
$instructionsModal.fadeOut();
});
/********************************
// WIN MODAL
********************************/
const youWin = () => {
// when the user wins, open the modal
$('#winModal').fadeIn();
$('#win-sound')[0].play();
}
// when the user clicks on <span> (x), close the modal
$close.on('click', () => {
$('#winModal').fadeOut();
});
/********************************
// LOSE MODAL
********************************/
const youLose = () => {
// when the user loses, open the modal
$('#loseModal').fadeIn();
$('#lose-sound')[0].play();
}
// when the user clicks on <span> (x), close the modal
$close.on('click', () => {
$('#loseModal').fadeOut();
});
/******************************
// CRYSTAL GEM EVENT LISTENERS
******************************/
$('#top-diamond').on('click', () =>{
guess(topDiamond)
});
$('#right-diamond').on('click', () =>{
guess(rightDiamond)
});
$('#left-diamond').on('click', () =>{
guess(leftDiamond)
});
$('#bottom-diamond').on('click', () =>{
guess(bottomDiamond)
});
/*************************
// NOW WITH KEYPRESSES!
*************************/
document.onkeydown = (e) => {
e = e || window.event;
if(simonSequence.playing){
return; // don't do anything if the sequence is playing
}
if (e.keyCode == 38) { // up
guess(topDiamond);
topDiamond.glow();
} else if (e.keyCode == 37) { // left
guess(leftDiamond);
leftDiamond.glow();
} else if (e.keyCode == 39) { // right
guess(rightDiamond);
rightDiamond.glow();
} else if (e.keyCode == 40) { // down
guess(bottomDiamond);
bottomDiamond.glow();
}
};
/******************************
// START BUTTON EVENT LISTENER
******************************/
$('#start').on('click', () => {
$('#start-sound')[0].play();
$('#steven-says').html('Woah, that was so coooool!').typewrite({
'delay': 100
});
restartGame();
})
/******************************
// STEVEN SAYS EVENT LISTENER
******************************/
$('#steven').on('click', () => {
$('#steven-sound')[0].play();
})
/******************************
// RESTART BUTTON EVENT LISTENER
******************************/
$('#replay').on('click', () => {
$('#restart-sound')[0].play();
restartGame();
})
/******************************
// GAME MODE EVENT LISTENERS
******************************/
//toggles active to display which mode the player is currently on
$('#hero').on('click', () => {
$('#mode-sound')[0].play();
$('#hero').toggleClass("active");
$('#normal').toggleClass("active");
setMode('#hero');
})
$('#normal').on('click', () => {
$('#mode-sound')[0].play();
$('#hero').toggleClass("active");
$('#normal').toggleClass("active");
setMode('#normal');
})
})