-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
698 lines (594 loc) · 17.2 KB
/
script.js
File metadata and controls
698 lines (594 loc) · 17.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
console.log('Loading script...');
//// HARD CODED GAME SETTINGS
// frames per second
let FPS = 1000 / 60;
// STATE MANAGER
// idle -- default waiting for user to start
let game = 'over';
// start -- when start button is clicked, begin game loop
// gameStart should not do anything if there is an ongoing game
let gameStart = () => {
if (game === 'over' || game === 'win'){
game = 'start';
triggerStartOverlay();
handleUserDifficulty();
handleDataSet();
reset();
showSideBar();
showCombo();
startAllLoops();
userInputText.focus();
console.log(game,"--- gameStart")
}
}
// over -- end all operations and reset to default and set back to
let gameOver = () => {
game = 'over';
console.log("--- gameOver")
}
// rest data and display to default values
let reset = () => {
resetDisplay();
resetData();
resetInput();
stage = 1;
}
// USER HANDLING
// during game start
// record the user input only onchange and compare to the exist
let userInputText = document.querySelector('#user-input');
userInputText.addEventListener('keypress',(event)=>{handleSpaceDown(event)});
userInputText.addEventListener('input',(event)=>{handleInput(event)});
let currentInput;
let handleSpaceDown = (event) => {
if (game === 'start' && (event.key === " " || event.key === "Enter")) {
currentInput = event.target.value;
event.target.value = '';
deleteBox(activeBox, currentInput)
// force the new display
//showActive()
showSideBar()
} else if (game !== 'start' && (event.key === " " || event.key === "Enter")) {
event.target.value = '';
}
}
let handleInput = (event) => {
if (event.target.value === " "){
event.target.value = '';
}
}
let resetInput = () => {
userInputText.value = '';
}
// get speed of boxes
let display = document.querySelector('.display-game');
let handleDataSet = () => {
let dataSet = document.querySelector('#data-set').value;
console.log(dataSet,"---handleUserSpeed")
switch(dataSet){
case 'test':
textArr = testArr;
combo = testCombo;
endWord = testEndWord;
break;
case 'food':
textArr = ['coke','candy','cake','ramen','double','down','burger'];
combo = ['double','down','burger'];
endWord = 'Boba'
break;
case 'harry':
try{
textArr = getTextArr();
endGame = getEndgame();
setCombo();
} catch {
textArr = testArr;
combo = testCombo;
endWord = testEndWord;
}
break;
}
}
// refresh every ms data
// rate of boxes spawning
let refreshMS;
let handleUserDifficulty = () => {
let difficulty = document.querySelector('#user-difficulty').value;
switch(difficulty){
case 'easy':
refreshMS = 1500;
userSpeed = 0.7;
break;
case 'moderate':
refreshMS = 1000;
userSpeed = 1;
break;
case 'hard':
refreshMS = 800;
userSpeed = 1.5;
break;
case 'insane':
refreshMS = 300;
userSpeed = 1.9;
break;
}
console.log(difficulty,'---handleUserDifficulty')
}
///
// DATA HANDLING -- checks for changes in data
///
// use api to fetch data
// need to choose:
// data set
// combo
// trap word
let textArr = ['text'];
let combo = '';
let endWord = 'text';
let apiArr = [];
let testArr = ['get','it','right','rad','blu','gren'];
let testCombo = ['get','it','right'];
let testEndWord = 'trap';
async function getApiWords(){
const path = "http://hp-api.herokuapp.com/api/characters"
//let query = '';
try {
// get data
const res = await fetch(path);
apiArr = await res.json();
} catch(err){
textArr = testArr;
console.error(err)
}
}
let getTextArr = () => {
let newArr = apiArr.map((obj)=>{
var word = obj.patronus;
if (word !== "" && word.length < 7){
return word
}
}).filter((item)=>{
return item ;
})
return newArr;
}
let getEndgame = () => {
var index = Math.floor(Math.random() * apiArr.length)
var word = apiArr[index].name.split(' ')[0];
return word;
}
let setCombo = () => {
var comboArr = apiArr.map((obj)=>{
return obj.wand.wood
}).filter((item)=>{
return item !== '';
})
for (var i = 0; i < combo.length; i++) {
var index = Math.floor(Math.random() * comboArr.length)
combo[i] = comboArr[index];
}
textArr = textArr.concat(combo);
}
getApiWords();
let userSpeed = 0.9;
let color = ['green', 'blue', 'red']
class Box {
// constructor chooses which type of box to be made
constructor(boxType, velocity){
var word = this.randomText(textArr)
this.velocity = velocity
switch(boxType){
case 'normal':
var newBox = this.createBox(word);
newBox.style.color = 'white'
this.word = word;
this.box = newBox;
break;
case 'color':
var newBox = this.createBox(word);
var randColor = this.randomColor();
this.word = randColor;
newBox.style.color = 'white'
newBox.style.backgroundColor = randColor;
this.box = newBox;
break;
case 'trap':
var newBox = this.createBox(endWord);
newBox.style.color = 'white'
this.word = endWord;
this.box = newBox;
break;
case 'fast':
var newBox = this.createBox('zoom');
newBox.style.backgroundColor = 'fuchsia'
this.word = 'zoom';
newBox.style.color = 'white'
this.velocity = this.velocity * 2;
this.box = newBox;
}
}
createBox (word) {
var newBox = document.createElement('div');
newBox.classList.add('move');
var insideText = document.createElement('p')
insideText.innerText = word;
newBox.appendChild(insideText);
// positioning
this.x = 800;
newBox.style.right = this.x + 'px';
let y = Math.floor(Math.random() * 350);
this.y = y;
newBox.style.top = y + 'px';
return newBox;
}
// display
showBox () {
display.appendChild(this.box);
}
// update position
updatePosition () {
this.x = this.x - this.velocity
let newPos = this.x + 'px';
this.box.style.right = newPos;
}
// create new boxes with random text
randomText (arr) {
var index = Math.floor(Math.random() * arr.length)
return arr[index];
}
randomColor () {
var index = Math.floor(Math.random() * color.length)
return color[index];
}
}
// add box to active every gameloop
let activeBox = [];
// add boxes according to stages
//
let updateBox = () => {
// according to stage update diff things
let boxType;
let allBoxTypes = ['normal', 'color','trap','fast']
switch (stage){
case 1:
// stage 1
// normal
boxType = allBoxTypes[0]
break;
case 2:
// stage 2
// color
boxType = allBoxTypes[1]
break;
case 3:
// stage 3
// normal + trap
// random
var randNum = randomNumber(4);
if (randNum === 3){
boxType = allBoxTypes[2];
} else {
boxType = allBoxTypes[0];
}
break;
case 4:
// stage 4
// all
var randNum = randomNumber(5);
if (randNum === 4){
boxType = allBoxTypes[2];
} else if (randNum === 3){
boxType = allBoxTypes[1];
} else {
boxType = allBoxTypes[0];
}
break;
case 5:
// stage 5
// speed up
var randNum = randomNumber(4);
if (randNum === 3){
boxType = allBoxTypes[3];
} else {
boxType = allBoxTypes[0];
}
break;
}
var newBox = new Box(boxType, userSpeed);
newBox.showBox();
activeBox.push(newBox)
}
// NOTE: expensive to look up and delete in array
// consider hashmap with linked lists
// returns true if deleted false if not found indicating invalid input
let lastDelete = '';
let deleteBox = (arr, word) => {
//check for first occurance and delete it
let index = -1;
let child = null;
for (var i = 0; i < arr.length; i++) {
console.log(arr[i].word, word,'--- deleteBox')
if (arr[i].word === word){
if (word === endWord) {
handleGameEnd();
}
child = arr[i].box;
index = i;
break
}
}
//if only word exists
if (index > -1) {
// remove from screen
display.removeChild(child);
// remove from array
arr = arr.splice(index, 1)
lastDelete = word;
if (updateCombo()){
console.log(activeCombo,'--- updateCombo')
addScore(100);
}
addScore(10);
console.log("Deleted ---",word,"--- deleteBox")
return true
} else {
console.log(word + 'not found! --- deleteBox')
return false
}
}
let deleteTrapBox = () => {
if (activeBox[0].word === endWord){
display.removeChild(activeBox[0].box)
activeBox.shift();
}
}
// after deletion increment score
let score = 0;
let addScore = (num) => {
score += num;
handleStage();
}
// reset data to default
let resetData = () => {
activeBox = [];
score = 0;
lastDelete = '';
}
// combo will be an array of deleted word in specific order
let activeCombo = [];
let updateCombo = () => {
comboLength = activeCombo.length;
if ( comboLength === 0 && lastDelete === combo[0]){
activeCombo[0] = lastDelete;
return false
} else if ( comboLength !== 0 && lastDelete === combo[0]){
activeCombo = [lastDelete];
return false
} else if ( comboLength === 1 && lastDelete === combo[1]){
activeCombo[1] = lastDelete;
return false
} else if ( comboLength === 2 && lastDelete === combo[2]){
activeCombo = [];
console.log('combo successful! --- isCombo')
return true
}
activeCombo = [];
console.log(activeCombo,"--- updateCombo")
return false
}
// DISPLAY HANDLING -- updates to show things
// show game state in the side bar
// show point system
let gameHeading = document.querySelector('.game-state>p');
let stageHeading = document.querySelector('.game-stage>p');
let scoreHeading = document.querySelector('.score>p');
let lastDelHeading = document.querySelector('.last-delete>p');
let comboHeading = document.querySelector('.combo>p');
let showSideBar = () => {
var comboHeadingText = () => {
var concat = '';
for (var i = 0; i < activeCombo.length; i++) {
concat = concat + ' ' + activeCombo[i];
}
return concat
}
gameHeading.innerText = game;
stageHeading.innerText = stage;
scoreHeading.innerText = score;
lastDelHeading.innerText = lastDelete;
comboHeading.innerText = comboHeadingText();
}
let showCombo = () => {
var comboText = document.getElementById('table-combo');
comboText.innerText = `${combo[0]} + ${combo[1]} + ${combo[2]}`;
}
// change the displays to the default
let resetDisplay = () => {
display.innerHTML = ''
gameHeading.innerText = game;
}
///// ALL LOOPS /////
let startAllLoops = () => {
startStageLoop();
// startStageTimer();
startGameLoop();
}
let endAllLoops = () => {
endStageLoop();
endGameLoop();
}
// Data loops + stage tracker
let stage = 1;
let stageLoop;
let stageTimer;
let startStageLoop = () => {
stageLoop = setInterval(()=>{updateData();},refreshMS)
console.log('--- startStageLoop')
}
let endStageLoop = () => {
clearInterval(stageLoop);
console.log('--- endStageLoop')
}
let updateData = () => {
updateBox(apiArr);
}
// progress stages till stage complete
let handleStage = () => {
if (score > 600){
game = 'win'
handleGameWon();
console.log('won ---handleStage')
} else if (score > 500){
stage = 5;
console.log(stage,'---handleStage')
} else if (score > 400){
stage = 4;
console.log(stage,'---handleStage')
} else if (score > 300){
stage = 3;
console.log(stage,'---handleStage')
} else if (score > 100){
stage = 2;
console.log(stage,'---handleStage')
}
}
// GAME LOOP --
// check for changes in display, data and game state
let gameLoop;
let gameTimer;
let startGameLoop = () => {
gameLoop = setInterval(()=>{updateDisplay();}, FPS);
//gameTimer = setTimeout(()=> {isGameWon();}, timer)
console.log("--- startGameLoop")
}
let endGameLoop = () => {
clearInterval(gameLoop);
console.log("--- endGameLoop")
}
//check won condition
let isGameWon = () => {
// as long you have not lost, you won
if (game !== 'over') {
game = 'won';
console.log(game,'--- isGameWon')
handleGameWon();
return true
} else {
return false
}
}
let handleGameWon = () => {
triggerGamewonOverlay()
endAllLoops();
console.log(game)
showSideBar()
}
// double check logic when to check for endgame
let updateDisplay = () => {
// go thru the array and update all the obj position
// check if box at end of screen
// if normal lose
// if trap delete
let endScreen = 0;
for (var i = 0; i < activeBox.length; i++) {
activeBox[i].updatePosition();
if (activeBox[i].x < endScreen && activeBox[i].word === endWord){
console.log('trap detected');
deleteTrapBox();
} else if (activeBox[i].x < endScreen){
console.log('edge collision --- updateDisplay')
handleGameEnd();
return;
}
}
}
let handleGameEnd = () => {
clearTimeout(gameTimer);
// endStageTimer();
endAllLoops();
gameOver();
triggerGameoverOverlay()
showSideBar();
console.log("--- handleGameEnd")
}
// Helper functions
let randomNumber = (num) => {
return Math.floor(Math.random() * num);
}
// onload show first overlay √
let overlay;
let overlayOne = () => {
let focusOnPanel = document.querySelector('.display-game');
focusOnPanel.scrollIntoView({behavior: "smooth",block:"center", inline: "end"});
overlay = document.getElementById('how-to-1');
let boxCtn = document.querySelector('.box-ctn');
let allBoxTypes = ['normal', 'color','trap','fast']
for (var i = 0; i < allBoxTypes.length; i++) {
var newBox = new Box(allBoxTypes[i], 0);
newBox.box.style.position = 'initial';
boxCtn.appendChild(newBox.box);
}
}
// explain game concept
// when close √
// display none
// show next overlay and bring focus √
let overlayTwo = () => {
let focusOnPanel = document.querySelector('.instructions');
focusOnPanel.scrollIntoView({behavior: "smooth",block:"center", inline: "end"});
overlay.style.display = 'none';
overlay = document.getElementById('how-to-2');
overlay.style.display = 'flex';
}
// explain special box type
// indicate special combinations below
// when close
// display none
// show next overlay and bring focus
let overlayThree = () => {
let focusOnPanel = document.querySelector('.input-panel');
focusOnPanel.scrollIntoView({behavior: "smooth",block:"center", inline: "end"});
overlay.style.display = 'none';
overlay = document.getElementById('how-to-3');
overlay.style.display = 'flex';
}
// explain how to start game
// when close
// display none
// bring focus to game area
let overlayOver = () => {
let focusOnPanel = document.querySelector('.display-game');
focusOnPanel.scrollIntoView({behavior: "smooth",block:"center", inline: "end"});
overlay.style.display = 'none';
overlay = document.getElementById('how-to-3');
overlay.style.display = 'none';
}
// ALL OVERLAYS HERE
let triggerStartOverlay = () => {
var startOverlay = document.querySelector('#start-overlay');
startOverlay.classList.add('fade');
setTimeout(()=>{
startOverlay.classList.remove('fade');
console.log('---triggerStartOverlay')
},2000);
}
let triggerGameoverOverlay = () => {
var startOverlay = document.querySelector('#gameover-overlay');
startOverlay.classList.add('translate');
setTimeout(()=>{
startOverlay.classList.remove('translate');
console.log('---triggerStartOverlay')
},3000);
}
let triggerGamewonOverlay = () => {
var startOverlay = document.querySelector('#gamewon-overlay');
startOverlay.classList.add('translate');
setTimeout(()=>{
startOverlay.classList.remove('translate');
console.log('---triggerStartOverlay')
},3000);
}
window.onload = () => {
overlayOne();
}
console.log('Script loaded!');