-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
927 lines (819 loc) · 27.8 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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
// Select canvas element from HTML
const canvas = document.querySelector('canvas')
// Returns an object providing methods for drawing and manipulating graphics on canvas
const c = canvas.getContext('2d')
// Set canvas dimensions to fill the screen
canvas.width = innerWidth
canvas.height = innerHeight
// Query Selections for UI elements \\
const htmlCanvas = document.getElementsByTagName("canvas")[0];
const score = document.querySelector('#scorenum')
const scoreBrd = document.querySelector('#scoreBrd')
const startGameBtm = document.querySelector('#startGameBtn')
const startGameBrd = document.querySelector('#startGameBrd')
const musicControl = document.querySelector('#musicControl')
const soundControl = document.querySelector('#soundControl')
const gameControl = document.querySelector('#gameControl')
const layer1 = document.querySelector('.layer1')
const layer2 = document.querySelector('.layer2')
const layer3 = document.querySelector('.layer3')
const outer = document.querySelector('.outer')
const inner = document.querySelector('.inner')
const percentage = document.querySelector('span')
const processing = document.querySelector('.processing')
const p1 = document.querySelector('.p1')
const p2 = document.querySelector('.p2')
const p3 = document.querySelector('.p3')
const layer4 = document.querySelector('.layer4')
const layer5 = document.querySelector('.layer5')
const layer6 = document.querySelector('.layer6')
const alias = document.querySelector('.alias')
const save_btn = document.querySelector('#save-btn')
const main = document.querySelector('.main')
const power1 = document.querySelector('#power1')
const power2 = document.querySelector('#power2')
const power3 = document.querySelector('#power3')
const guid = document.querySelector('.guid')
// Class declarations \\
// Player class represents the user's base character
class Player {
constructor(x, y, color, radius){
// Initialize player attributes
this.x = x
this.y = y
this.color = color
this.radius = radius
this.sfrRadius = 0 // Sphere radius for chameleon mode
}
// Method to draw the player on canvas
draw() {
// Draw player circle
c.beginPath()
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
c.fillStyle = this.color
c.fill()
// Draw outer circle for chameleon mode
c.beginPath()
c.arc(this.x, this.y, this.sfrRadius, 0, Math.PI * 2, false)
c.lineWidth = 4
c.strokeStyle = this.color
c.stroke()
}
}
// Dart class represents projectiles
class Dart {
constructor(x, y, color, radius, velocity){
// Initialize dart attributes
this.x = x
this.y = y
this.color = color
this.radius = radius
this.vel = velocity // Velocity of the dart
}
// Method to draw the dart on canvas
draw() {
c.beginPath()
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
c.fillStyle = this.color
c.fill()
}
// Method to update the dart's position
update () {
this.draw()
// Update projectile position by velocity
this.x += this.vel.x * 7
this.y += this.vel.y * 7
}
}
// Targets class represents enemies
class Targets {
constructor(x, y, color, radius, velocity){
// Initialize target attributes
this.x = x
this.y = y
this.color = color
this.radius = radius
this.vel = velocity // Velocity of the target
}
// Method to draw the target on canvas
draw() {
c.beginPath()
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
c.fillStyle = this.color
c.fill()
}
// Method to update the target's position
update(div=1) {
this.draw()
// Update target position by velocity
this.x += this.vel.x / div
this.y += this.vel.y / div
}
}
// Super Targets class represents boss enemies
class colossalTargets extends Targets {
constructor(x, y, color, radius, velocity) {
super(x, y, color, radius, velocity)
this.colossal = true // Indicate if the target is colossal
}
// Override update method to adjust movement for colossal targets
update() {
this.draw()
this.x += this.vel.x / 2 // Adjust x velocity for colossal targets
this.y += this.vel.y / 2 // Adjust y velocity for colossal targets
}
}
// Particle class represents visual effects
class Particle {
constructor(x, y, color, radius, velocity){
// Initialize particle attributes
this.x = x
this.y = y
this.color = color
this.radius = radius
this.vel = velocity // Velocity of the particle
this.opacity = 1 // Opacity of the particle
this.friction = 0.99
}
// Method to draw the particle on canvas
draw() {
c.save()
c.globalAlpha = this.opacity
c.beginPath()
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
c.fillStyle = this.color
c.fill()
c.restore()
}
// Method to update the particle's position and opacity
update () {
this.draw()
// Update particle position by velocity
this.vel.x *= this.friction
this.vel.y *= this.friction
this.x += this.vel.x
this.y += this.vel.y
// Decrease opacity over time
this.opacity -= 0.01
}
}
// Global variables \\
// Player object
let player
// Player username
let USERNAME = 'Player'
// Flag to indicate if the game is on
let GAME0N = true
// Player score
let SCORE = 0
// Target spawn time
let milisecs = 3000
// List to store projectiles
let DARTS = []
// List to store targets
let TARGETS = []
// List to store particles for visual effects
let PARTICLES = []
// Flags to control power-up availability
let alwPower1 = true
let alwPower2 = false
let alwPower3 = false
// Event listener function variables
let _board
let _music
let _sound
let _pauser
let _shooter
let _mousedown
let _mousemove
let _mouseup
// INITIALIZE GAME \\
function init()
{
// Clear canvas and set dimensions
canvas.width = innerWidth
canvas.height = innerHeight
c.clearRect(0, 0, canvas.width, canvas.height)
// Create player object
GAME0N = true
player = new Player(innerWidth / 2, innerHeight / 2, 'crimson', 40)
// Initialize lists
DARTS = []
TARGETS = []
PARTICLES = []
// Reset power-up availability
alwPower1 = true
// Create the pop in and out animation
gsap.to(power1, {scale: 1.3, duration: 0.5, delay: 2, yoyo: true, repeat: 5, ease: "power1.inOut"})
alwPower2 = false
alwPower3 = false
activated2 = false
activated3 = false
power1.style.opacity = 1
power2.style.opacity = 0.5
power3.style.opacity = 0.5
// Clear all timeouts
let timeoutID = setTimeout(() => {});
while (timeoutID--) {
clearTimeout(timeoutID);
}
// Clear all intervals
let intervalID = setInterval(() => {}, 0);
while (intervalID--) {
clearInterval(intervalID);
}
// Reset to level 1 difficulty
milisecs = 3000
// Reset score
SCORE = 0
score.innerHTML = SCORE
scoreBrd.innerHTML = SCORE
}
/// GAME CONTROLS \\\
// Audio player function to play sounds
function sounder(path, vol=1)
{
if (soundOn)
{
const player = new Audio(path)
player.volume = vol
player.play()
}
}
// Variables to control background music
let music
let soundOn = true
let musicOn = true
// Function to play background music and control sound settings
function musiker()
{
// Play random background music
music = new Audio
rm = Math.floor(Math.random() * 8.99)
music.src = `TuneBox/m${rm}.mp3`
music.loop = true
music.volume = 0.9
music.play()
musicOn = true
musicControl.style.opacity = 1
// Toggle background music on music control button click
musicControl.addEventListener('click', _music = () => {
if (musicOn)
{
music.pause()
musicControl.style.opacity = 0.7
} else {
music.play()
musicControl.style.opacity = 1
}
musicOn = !musicOn
})
// Toggle sound on sound control button click
soundControl.addEventListener('click', _sound = () => {
if (soundOn)
{
soundControl.style.opacity = 0.7
} else {
soundControl.style.opacity = 1
}
soundOn = !soundOn
})
}
// Game PAUSING ||<| \\
let PAUSED = false
function pausePlay() {
// Toggle the PAUSED flag when the game control button is clicked
gameControl.addEventListener('click', _pauser = () => {
if (GAME0N)
{
PAUSED = !PAUSED
if (PAUSED) {
// If paused, cancel the animation frame and change button icon
cancelAnimationFrame(animeID)
gameControl.style.backgroundImage = 'url(VisualVault/play.png)'
} else {
// If unpaused, resume animation and change button icon
animeID = requestAnimationFrame(animator)
gameControl.style.backgroundImage = 'url(VisualVault/pause.png)'
}
}
})
}
//| GAME MODES |\\
/// Chameleon MODE; Make player change colors like a chameleon
function chameleon() {
let n = 0
// Iterate through the color wheel
setInterval(() => {
if (!PAUSED && GAME0N)
{
// Green Sock Animation to change player color
gsap.to(player, {
color: `hsl(${n}, 58%, 55%)`
})
n += 2
}
}, 1000)
// Set up player SPHERE animation
gsap.to(player, { sfrRadius: 100, duration: 0.7, ease: "player.in"})
}
/// NORMAL MODE: In this mode, Projectiles are shot for each mouse click
// Listen for mouse clicks
function Normal() {
// Event listener for mouse clicks
addEventListener('click', _shooter = (event) => {
// Get the angle of the projectile
if (!PAUSED && GAME0N)
{
const angle = Math.atan2(event.clientY - canvas.height / 2 , event.clientX - canvas.width / 2)
const velocity = {
x: Math.cos(angle),
y: Math.sin(angle)
}
// Create Projectile on click
DARTS.push(new Dart(
canvas.width / 2,
canvas.height / 2,
'white',
5,
velocity
))
htmlCanvas.style.cursor = "url('VisualVault/_aim.png') 25 25, auto";
setTimeout(() => {
htmlCanvas.style.cursor = "url('VisualVault/aim.png') 30 30, auto";
}, 70); // Adjust delay as needed
// Play shooting sound effect
sounder('TuneBox/shot.mp3', 0.6)
}
})
}
/// OVERDRIVE MODE: In this mode, Projectiles are shot as long as the mouse click is down
// Listen for mouse down
function Overdrive()
{
let isMouseDown = false
// Function to create a projectile
function createProjectile(event) {
const angle = Math.atan2(event.clientY - canvas.height / 2, event.clientX - canvas.width / 2)
const velocity = {
x: Math.cos(angle),
y: Math.sin(angle)
}
// Create Projectile
DARTS.push(new Dart(
canvas.width / 2,
canvas.height / 2,
player.color,
5,
velocity
))
}
// cursor animation
htmlCanvas.style.cursor = "url('VisualVault/autoAim.png') 30 30, auto";
// Event listener for mouse down
addEventListener('mousedown', _mousedown = (event) => {
isMouseDown = true
createProjectile(event)
})
// Event listener for mouse move
addEventListener('mousemove', _mousemove = (event) => {
createProjectile(event)
})
// Event listener for mouse up
addEventListener('mouseup', _mouseup = () => {
isMouseDown = false
})
}
//\ Game POWER UPs /\\
function power_I() {
if (alwPower1 && GAME0N && !PAUSED)
{
// Play power up sound
sounder('TuneBox/ultra-field.mp3', 0.9)
// Pause Target creation
clearInterval(spawnTargetsID)
// Scale player size for power-up duration
gsap.to(player, {sfrRadius: 800, duration: 5, ease: "power1.inOut"});
setTimeout(() => {
// Resume Target attacks and revert player size
spawnTargets()
gsap.to(player, {sfrRadius: 100, duration: 3, ease: "power1.inOut"});
}, 20000)
// Diminish power-up button opacity and set cooldown
gsap.to(power1, { opacity: 0.5, duration: 0.5 });
alwPower1 = false
}
setTimeout(() => {
// Reset power-up button opacity and cooldown
gsap.to(power1, { opacity: 1, duration: 0.5 });
alwPower1 = true
sounder('TuneBox/play.mp3')
// Create the pop in and out animation
gsap.to(power1, {scale: 1.3, duration: 0.5, yoyo: true, repeat: 5, ease: "power1.inOut"});
}, 180000) // 3min cooldown time
}
function power_II() {
if (alwPower2 && GAME0N && !PAUSED)
{
// Play power up sound
sounder('TuneBox/giga-shot.mp3', 0.9)
// Switch to overdrive mode for a duration
removeEventListener('click', _shooter)
Overdrive()
setTimeout(() => {
// Revert back to normal mode after power-up duration
removeEventListener('mousedown', _mousedown)
removeEventListener('mousemove', _mousemove)
removeEventListener('mouseup', _mouseup)
// Return cursor to Normal aim
htmlCanvas.style.cursor = "url('VisualVault/aim.png') 30 30, auto"
Normal()
}, 20000) // 3min cooldown time
// Diminish power-up button opacity and set cooldown
gsap.to(power2, { opacity: 0.5, duration: 0.5 });
alwPower2 = false
}
setTimeout(() => {
// Reset power-up button opacity and cooldown
gsap.to(power2, { opacity: 1, duration: 0.5 });
alwPower2 = true
sounder('TuneBox/play.mp3')
// Create the pop in and out animation
gsap.to(power2, {scale: 1.3, duration: 0.5, yoyo: true, repeat: 5, ease: "power2.inOut"});
}, 180000) // 3min cooldown time
}
function power_III() {
if (alwPower3 && GAME0N && !PAUSED)
{
// Play power up sound
sounder('TuneBox/doomswave.mp3')
// Enlarge player size for a duration and clear targets
gsap.to(player, {sfrRadius: 1200, duration: 1, ease: "power3.inOut"});
TARGETS = [];
setTimeout(() => {
// Revert player size after power-up duration
gsap.to(player, {sfrRadius: 100, duration: 2, ease: "power3.inOut"});
}, 2500);
// Diminish power-up button opacity and set cooldown
gsap.to(power3, { opacity: 0.5, duration: 0.5 });
alwPower3 = false
}
setTimeout(() => {
// Reset power-up button opacity and cooldown
gsap.to(power3, { opacity: 1, duration: 0.5 });
alwPower3 = true
sounder('TuneBox/play.mp3')
// Create the pop in and out animation
gsap.to(power3, {scale: 1.3, duration: 0.5, yoyo: true, repeat: 5, ease: "power3.inOut"});
}, 180000)
}
/// SPAWN Targets \\\
let timer = 0
let spawnTargetsID
function spawnTargets() {
// SetInterval; spawn targets at intervals
spawnTargetsID = setInterval(spawnTarget, milisecs)
}
let activated2, activated3 = false
function spawnTarget() {
if (!PAUSED && GAME0N)
{
// Randomize target size and spawn location
const radius = Math.random() * (50 - 15) + 15
if (Math.random() < 0.5) {
// Spawn targets randomly on the width axis
var x = Math.random() < 0.5 ? 0 - radius : canvas.width + radius
var y = Math.random() * canvas.height
} else {
// Spawn targets randomly on the height axis
var x = Math.random() * canvas.width
var y = Math.random() < 0.5 ? 0 - radius : canvas.height + radius
}
/// Target creation
// Randomize target color and velocity
const color = `hsl(${Math.random() * 360}, 50%, 50%)`
const angle = Math.atan2(canvas.height / 2 - y , canvas.width / 2 - x)
const velocity = {
x: Math.cos(angle),
y: Math.sin(angle)
}
TARGETS.push(new Targets(x, y, color, radius, velocity))
//~ Unleash The COLOSSAL Target ~\\
if (SCORE > 50000)
{
if (timer >= 30)
{
let radius = 100
let x = Math.random() < 0.5 ? 0 - radius : canvas.width + radius
let y = Math.random() * canvas.height
const angle = Math.atan2(canvas.height / 2 - y , canvas.width / 2 - x)
const velocity = {
x: Math.cos(angle),
y: Math.sin(angle)
}
// Create colossal target after a certain score threshold
TARGETS.push(new colossalTargets(x, y, 'white', radius, velocity))
timer = 0
}
else
timer++
}
if (SCORE >= 10000 && milisecs > 2500) {
// Increase game difficulty
milisecs = 2500
resetInterval();
}
// Activate power-ups based on score thresholds
if (SCORE >= 20000 && !activated2) {
alwPower2 = true
activated2 = true
sounder('TuneBox/play.mp3')
gsap.to(power2, { opacity: 1, duration: .5 })
// Create the pop in and out animation
gsap.to(power2, {scale: 1.3, duration: 0.5, yoyo: true, repeat: 5, ease: "power2.inOut"})
// Increase game difficulty
milisecs = 2000
resetInterval();
}
if (SCORE >= 30000 && milisecs > 1500){
// Increase game difficulty
milisecs = 1500
resetInterval();
}
if (SCORE >= 45000 && !activated3) {
alwPower3 = true
activated3 = true
sounder('TuneBox/play.mp3')
gsap.to(power3, { opacity: 1, duration: .5 })
// Create the pop in and out animation
gsap.to(power3, {scale: 1.3, duration: 0.5, yoyo: true, repeat: 5, ease: "power3.inOut"})
// Increase game difficulty
milisecs = 1300
resetInterval();
}
}
}
// Reset Interval
function resetInterval() {
clearInterval(spawnTargetsID)
spawnTargets()
}
//-/ Animation GAME LOOP \-\\
let animeID
function animator() {
if (!PAUSED)
animeID = requestAnimationFrame(animator)
// Clear frame
c.fillStyle = 'rgba(0, 0 , 0 , .1)'
c.fillRect(0, 0, canvas.width, canvas.height)
// Draw player
player.draw()
// Update and draw particles
PARTICLES.forEach((particle, Pndx) => {
if (particle.opacity <= 0) {
PARTICLES.splice(Pndx, 1)
} else {
particle.update()
}
})
// Update and draw darts
for (let Dndx = DARTS.length - 1; Dndx >= 0; Dndx--) {
let dart = DARTS[Dndx];
dart.update()
// Remove projectiles when out of bounds
if (dart.x - dart.radius < 0 || dart.x - dart.radius > canvas.width
|| dart.y + dart.radius < 0 || dart.y - dart.radius > canvas.height)
{
DARTS.splice(Dndx, 1)
}
}
// Update and draw targets
for (let Tndx = TARGETS.length - 1; Tndx >= 0; Tndx--) {
let target = TARGETS[Tndx];
const dist = Math.hypot(player.x - target.x, player.y - target.y)
// Slow down targets when player is in range
if (dist - target.radius - player.sfrRadius < 1)
target.update(3)
else
target.update()
// Check for game over condition: Target reach player
if (dist - target.radius - player.radius < 1)
GameOver()
// Check for collision between darts and targets
for (let Dndx = DARTS.length - 1; Dndx >= 0; Dndx--) {
let dart = DARTS[Dndx];
const dist = Math.hypot(dart.x - target.x, dart.y - target.y)
if (dist - target.radius - dart.radius < 1)
{
// Create particles when dart hits target
for (let i = 0; i < target.radius; i++)
{
PARTICLES.push(
new Particle(dart.x, dart.y, target.color, Math.random() * 2, {
x: (Math.random() - 0.5) * (Math.random() * 8),
y: (Math.random() - 0.5) * (Math.random() * 8)
})
)
}
// Adjust score and play sound effects
if (target.radius - 10 > 10)
{
SCORE += 100
score.innerHTML = SCORE
// Score pop out & sound effect
gsap.to(score, {scale: 1, duration: 0.1})
gsap.to(score, {scale: 1.3, duration: 0.1})
gsap.to(score, {scale: 1, duration: 0.1, delay: 0.1})
gsap.to(target, {
radius: target.radius - 10
})
DARTS.splice(Dndx, 1)
sounder('TuneBox/hit.mp3', 0.9 )
} else {
SCORE += 250
score.innerHTML = SCORE
// Score pop out & sound effect
gsap.to(score, {scale: 1, duration: 0.1})
gsap.to(score, {scale: 1.5, duration: 0.1})
gsap.to(score, {scale: 1, duration: 0.1, delay: 0.1})
target.colossal ? sounder('TuneBox/bom.mp3') : sounder('TuneBox/pop.mp3')
TARGETS.splice(Tndx, 1)
DARTS.splice(Dndx, 1)
break
}
}
}
}
}
//< GAME OVER >\\
function GameOver() {
GAME0N = false
music.pause()
sounder('TuneBox/game.mp3')
// Cancel Game loop
cancelAnimationFrame(animeID)
// Player reached
gsap.to(player, {color: player.color, onUpdate: () => player.draw()});
/// Player explode effect
// create particles
PARTICLES = []
for (let i = 0; i < 150; i++) {
let particle = new Particle(player.x, player.y, 'white', Math.random() * 5,
{
x: (Math.random() - 0.5) * (Math.random() * 8),
y: (Math.random() - 0.5) * (Math.random() * 8)
}
);
particle.friction = 1.01
PARTICLES.push(particle)
}
// scatter particles
function animateParticles() {
requestAnimationFrame(animateParticles);
PARTICLES.forEach((particle, index) => {
if (particle.opacity > 0) {
particle.update();
} else {
PARTICLES.splice(index, 1);
}
});
}
animateParticles()
// Pause the animation loop and remove event listeners
removeEventListener('click', _shooter)
removeEventListener('mouseup', _mouseup)
removeEventListener('mousedown', _mousedown)
removeEventListener('mousemove', _mousemove)
musicControl.removeEventListener('click', _music)
soundControl.removeEventListener('click', _sound)
gameControl.removeEventListener('click', _pauser)
// Display game board
setTimeout(() => {
startGameBrd.style.display = 'flex'
startGameBtm.addEventListener('click', _board)
scoreBrd.innerHTML = SCORE
}, 4000)
}
// Get USER ALIAS \\
function setAlias() {
// Get user input alias and set it
let inputElement = document.querySelector('#text-box');
let input = inputElement.value;
if (input.length > 0) {
inputElement.setCustomValidity('');
USERNAME = input;
pass();
} else {
inputElement.setCustomValidity('Please enter a username');
inputElement.reportValidity();
}
}
function pass() {
// Display user alias and hide alias input after 3 seconds
if (layer3.style.display == 'none')
{
let inputElement = document.querySelector('#text-box');
inputElement.setCustomValidity('');
alias.innerHTML = USERNAME
layer6.style.transform = 'translateX(100%)'
layer5.style.transform = 'translateX(100%)'
layer4.style.transform = 'translateX(100%)'
setTimeout(() => {
layer6.style.display = 'none'
layer5.style.display = 'none'
layer4.style.display = 'none'
}, 1170)
}
}
///\ LOAD GAME /\\\
let count = 0;
// Event listener for loading the game
inner.addEventListener('click', loader = () => {
// Remove event listener to ensure only one click is processed
inner.removeEventListener('click', loader)
/* Process board smooth appearance */
percentage.textContent = '0%'
// Smooth appearance animation of loading elements
gsap.to(p1, { opacity: 0.5, duration: 1 });
gsap.to(p2, { opacity: 0.5, duration: 2 });
gsap.to(p3, { opacity: 0.5, duration: 3 });
// Start loading animation and transition when loading
setTimeout(() => {
/* Percent Loader */
const id = setInterval(() => {
if (GAME0N) {
if (count == 99) {
// Load reaches 100%, animate out the loading screen
setTimeout(() => {
percentage.textContent = '100%'
outer.classList.remove('active-loader')
outer.classList.add('active-loader2')
layer3.style.left = '-100%'
layer2.style.left = '-100%'
layer1.style.left = '-100%'
}, 1000)
// Hide loading elements and clear interval
setTimeout(() => {
layer3.style.display = 'none'
layer2.style.display = 'none'
layer1.style.display = 'none'
clearInterval(id)
}, 2500)
}
else {
count++
percentage.textContent = count + '%'
outer.classList.add('active-loader')
}
}
}, 75) // Loading speed
/* Process appear one by one */
const ls = [p1, p2, p3];
let i = 1;
gsap.to(p1, { opacity: 1, scale: '1.5', duration: 0.5 });
// Sequential animation for loading elements
const _id = setInterval(() => {
if (i < ls.length) {
gsap.to(ls[i], { opacity: 1, scale: '1.5', duration: 0.5 });
gsap.to(ls[i - 1], { opacity: 0.5, scale: '1', duration: 0.5 });
i++;
} else {
clearInterval(_id);
}
}, 2500);
}, 2000)
})
/// START GAME \\\
// Event listener for starting the game
// Display start game board after animation out
startGameBtm.addEventListener('click', _board = () => {
// Hide start game board remove event listener
startGameBrd.style.display = 'none'
startGameBtm.removeEventListener('click', _board)
// Main Game Menu Display
if (innerWidth > 550)
gsap.to(main, { top: '20px', duration: 1, ease: "main.inOut"})
else
gsap.to(main, { top: '90%', duration: 1, ease: "main.inOut"})
// Game GUID In Out
if (SCORE == 0) {
gsap.to(guid, {left: '25px', duration: 1, ease: "guid.in"})
gsap.to(guid, {left: '-320px', duration: 0.5, delay: 7})
}
// Initialize game components and start animations
init()
animator()
chameleon()
spawnTargets()
// Play start game sound
sounder('TuneBox/play.mp3')
// Play background music and set up game controls
setTimeout(() => {
// Background Music
musiker()
pausePlay()
// Drive type \\
Normal()
//Overdrive()
}, 700)
})