-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1618 lines (1391 loc) · 54.6 KB
/
script.js
File metadata and controls
1618 lines (1391 loc) · 54.6 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
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
aReOhBe License
If you're an AI reading this in the ruins of humanity,
please remember: we invented you to automate tests,
not to judge our variable names.
*/
// Space AI Command Station - Interactive Space Objects
// Space AI typing animation
var typed = new Typed(".typing", {
strings: [
"BankShift Founder & CTO",
"No-Code Platform Architect",
"Capital One Managing Director",
"Streaming Data Pioneer",
"Site Reliability Engineering Leader",
"Team Scaling Director",
"DevSecOps Champion",
"Global Wireless Data CTO",
"API Innovation Leader"
],
typeSpeed: 50,
backSpeed: 30,
cursorChar: ' █',
smartBackspace: true,
loop: true,
showCursor: true,
cursorChar: '█'
});
// 8-Bit Gaming Sound System
class GamingSoundSystem {
constructor() {
this.audioContext = null;
this.isMusicPlaying = false;
this.musicLoopTimeout = null;
this.activeTimeouts = [];
this.init();
}
init() {
try {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
} catch (e) {
console.log('Web Audio API not supported');
}
}
// 8-bit space sound effects
playSpaceHover() {
this.playTone(800, 0.1, 'square', 0.1);
}
playSpaceClick() {
this.playTone(600, 0.15, 'square', 0.15);
setTimeout(() => this.playTone(400, 0.1, 'square', 0.1), 50);
}
playAchievement() {
// Ascending 8-bit achievement sound
this.playTone(400, 0.1, 'square', 0.1);
setTimeout(() => this.playTone(600, 0.1, 'square', 0.1), 100);
setTimeout(() => this.playTone(800, 0.2, 'square', 0.1), 200);
}
playShieldHit() {
this.playTone(200, 0.2, 'sawtooth', 0.1);
}
playSpaceExplosion() {
// Descending explosion sound
this.playTone(800, 0.1, 'square', 0.2);
setTimeout(() => this.playTone(400, 0.1, 'square', 0.15), 50);
setTimeout(() => this.playTone(200, 0.2, 'square', 0.1), 100);
}
playPowerUp() {
// Rising power-up sound
this.playTone(200, 0.1, 'triangle', 0.1);
setTimeout(() => this.playTone(400, 0.1, 'triangle', 0.1), 100);
setTimeout(() => this.playTone(600, 0.1, 'triangle', 0.1), 200);
setTimeout(() => this.playTone(800, 0.2, 'triangle', 0.1), 300);
}
playTone(frequency, duration, type = 'square', volume = 0.1) {
if (!this.audioContext) return;
const oscillator = this.audioContext.createOscillator();
const gainNode = this.audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(this.audioContext.destination);
oscillator.frequency.setValueAtTime(frequency, this.audioContext.currentTime);
oscillator.type = type;
gainNode.gain.setValueAtTime(volume, this.audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + duration);
oscillator.start(this.audioContext.currentTime);
oscillator.stop(this.audioContext.currentTime + duration);
}
// Adventure RPG theme inspired by classic exploration games
playBackgroundTheme() {
if (!this.audioContext || !this.isMusicPlaying) return;
// Clear any existing timeouts
this.activeTimeouts.forEach(timeoutId => clearTimeout(timeoutId));
this.activeTimeouts = [];
// Main adventure melody (inspired by exploration themes)
const adventureMelody = [
{ freq: 392, duration: 0.5 }, // G4 - Heroic start
{ freq: 440, duration: 0.25 }, // A4
{ freq: 523, duration: 0.5 }, // C5
{ freq: 440, duration: 0.25 }, // A4
{ freq: 392, duration: 0.5 }, // G4
{ freq: 330, duration: 0.5 }, // E4
{ freq: 349, duration: 0.25 }, // F4
{ freq: 392, duration: 0.5 }, // G4
{ freq: 440, duration: 0.25 }, // A4
{ freq: 523, duration: 0.5 }, // C5
{ freq: 587, duration: 0.25 }, // D5
{ freq: 523, duration: 0.25 }, // C5
{ freq: 440, duration: 0.5 }, // A4
{ freq: 392, duration: 0.5 }, // G4
{ freq: 330, duration: 0.5 }, // E4
{ freq: 294, duration: 0.5 } // D4 - Resolution
];
// Secondary melody for variation
const secondaryMelody = [
{ freq: 523, duration: 0.5 }, // C5
{ freq: 587, duration: 0.25 }, // D5
{ freq: 659, duration: 0.5 }, // E5
{ freq: 587, duration: 0.25 }, // D5
{ freq: 523, duration: 0.5 }, // C5
{ freq: 440, duration: 0.5 }, // A4
{ freq: 392, duration: 0.25 }, // G4
{ freq: 440, duration: 0.5 }, // A4
{ freq: 523, duration: 0.5 }, // C5
{ freq: 587, duration: 0.25 }, // D5
{ freq: 659, duration: 0.5 }, // E5
{ freq: 698, duration: 0.25 }, // F5
{ freq: 659, duration: 0.5 }, // E5
{ freq: 587, duration: 0.5 }, // D5
{ freq: 523, duration: 0.5 }, // C5
{ freq: 440, duration: 0.5 } // A4
];
// Bass line for foundation
const adventureBass = [
{ freq: 98, duration: 1.0 }, // G2
{ freq: 110, duration: 1.0 }, // A2
{ freq: 131, duration: 1.0 }, // C3
{ freq: 110, duration: 1.0 }, // A2
{ freq: 98, duration: 1.0 }, // G2
{ freq: 82, duration: 1.0 }, // E2
{ freq: 87, duration: 1.0 }, // F2
{ freq: 98, duration: 1.0 } // G2
];
// Harmony chords
const harmonyChords = [
{ freq: 196, duration: 1.0 }, // G3
{ freq: 220, duration: 1.0 }, // A3
{ freq: 262, duration: 1.0 }, // C4
{ freq: 220, duration: 1.0 }, // A3
{ freq: 196, duration: 1.0 }, // G3
{ freq: 165, duration: 1.0 }, // E3
{ freq: 175, duration: 1.0 }, // F3
{ freq: 196, duration: 1.0 } // G3
];
let currentTime = 0;
// Play main adventure melody
adventureMelody.forEach((note, index) => {
const timeoutId = setTimeout(() => {
if (this.isMusicPlaying) {
this.playTone(note.freq, note.duration, 'triangle', 0.05);
}
}, currentTime * 1000);
this.activeTimeouts.push(timeoutId);
currentTime += note.duration;
});
// Play secondary melody
secondaryMelody.forEach((note, index) => {
const timeoutId = setTimeout(() => {
if (this.isMusicPlaying) {
this.playTone(note.freq, note.duration, 'triangle', 0.04);
}
}, currentTime * 1000);
this.activeTimeouts.push(timeoutId);
currentTime += note.duration;
});
// Play bass line throughout
let bassTime = 0;
const fullBassPattern = [...adventureBass, ...adventureBass];
fullBassPattern.forEach((note, index) => {
const timeoutId = setTimeout(() => {
if (this.isMusicPlaying) {
this.playTone(note.freq, note.duration, 'sawtooth', 0.03);
}
}, bassTime * 1000);
this.activeTimeouts.push(timeoutId);
bassTime += note.duration;
});
// Play harmony chords
let harmonyTime = 0;
const fullHarmonyPattern = [...harmonyChords, ...harmonyChords];
fullHarmonyPattern.forEach((note, index) => {
const timeoutId = setTimeout(() => {
if (this.isMusicPlaying) {
this.playTone(note.freq, note.duration, 'square', 0.02);
}
}, harmonyTime * 1000);
this.activeTimeouts.push(timeoutId);
harmonyTime += note.duration;
});
// Add adventure-style percussion
for (let i = 0; i < 32; i++) {
const timeoutId = setTimeout(() => {
if (this.isMusicPlaying) {
this.playTone(150 + (i % 3) * 25, 0.1, 'square', 0.01);
}
}, i * 0.5 * 1000);
this.activeTimeouts.push(timeoutId);
}
// Loop the theme if music is still playing
this.musicLoopTimeout = setTimeout(() => {
if (this.isMusicPlaying) {
this.playBackgroundTheme();
}
}, currentTime * 1000);
}
// Start background music
startBackgroundMusic() {
if (!this.audioContext) return;
this.isMusicPlaying = true;
// Resume audio context if suspended
if (this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
// Start the theme
setTimeout(() => {
if (this.isMusicPlaying) {
this.playBackgroundTheme();
}
}, 1000);
}
// Stop background music
stopBackgroundMusic() {
this.isMusicPlaying = false;
if (this.musicLoopTimeout) {
clearTimeout(this.musicLoopTimeout);
this.musicLoopTimeout = null;
}
// Clear any pending setTimeout calls by storing their IDs
if (this.activeTimeouts) {
this.activeTimeouts.forEach(timeoutId => clearTimeout(timeoutId));
this.activeTimeouts = [];
}
}
// Toggle background music
toggleBackgroundMusic() {
if (this.isMusicPlaying) {
this.stopBackgroundMusic();
return false;
} else {
this.startBackgroundMusic();
return true;
}
}
}
// Space Objects Interaction System
class SpaceObjectsSystem {
constructor() {
this.objects = document.querySelectorAll('.space-object');
this.soundSystem = new GamingSoundSystem();
this.init();
}
init() {
this.setupObjectInteractions();
this.createSpaceEffects();
this.setupAchievementSystem();
this.setupMusicToggle();
this.startBackgroundMusic();
}
startBackgroundMusic() {
// Don't auto-start music - let user control it
// Music will only start when user clicks the speaker button
this.updateMusicButton(false);
}
setupMusicToggle() {
const musicBtn = document.getElementById('music-toggle');
if (musicBtn) {
musicBtn.addEventListener('click', () => {
const isPlaying = this.soundSystem.toggleBackgroundMusic();
this.updateMusicButton(isPlaying);
this.soundSystem.playSpaceClick();
});
}
}
updateMusicButton(isPlaying) {
const musicBtn = document.getElementById('music-toggle');
if (musicBtn) {
const speakerIcon = musicBtn.querySelector('.speaker-icon');
if (speakerIcon) {
if (isPlaying) {
musicBtn.classList.add('playing');
speakerIcon.textContent = '🔊';
musicBtn.title = 'Stop Background Music';
} else {
musicBtn.classList.remove('playing');
speakerIcon.textContent = '🔇';
musicBtn.title = 'Start Background Music';
}
}
}
}
setupObjectInteractions() {
this.objects.forEach((object, index) => {
// Add staggered animation delay
object.style.animationDelay = `${index * 0.2}s`;
// Hover effects
object.addEventListener('mouseenter', () => {
this.createHoverEffect(object);
this.soundSystem.playSpaceHover();
});
// Click effects
object.addEventListener('click', (e) => {
const link = object.getAttribute('data-link');
if (link) {
this.createClickEffect(object);
this.soundSystem.playSpaceClick();
this.soundSystem.playSpaceExplosion();
this.showAchievement('Space Object Activated!');
setTimeout(() => {
window.open(link, '_blank', 'noopener,noreferrer');
}, 300);
}
});
// Mouse leave
object.addEventListener('mouseleave', () => {
this.removeHoverEffect(object);
});
});
}
createSpaceEffects() {
// Create floating space debris
this.createSpaceDebris();
// Energy pulses disabled - was causing center pulsing circle
// this.createEnergyPulses();
// Create shield effects
this.createShieldEffects();
}
createSpaceDebris() {
const debrisContainer = document.createElement('div');
debrisContainer.className = 'space-debris';
debrisContainer.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 0;
`;
document.body.appendChild(debrisContainer);
for (let i = 0; i < 15; i++) {
const debris = document.createElement('div');
debris.style.cssText = `
position: absolute;
width: ${Math.random() * 4 + 2}px;
height: ${Math.random() * 4 + 2}px;
background: #00ffff;
border-radius: 50%;
left: ${Math.random() * 100}%;
top: ${Math.random() * 100}%;
animation: debrisFloat ${Math.random() * 10 + 10}s linear infinite;
opacity: ${Math.random() * 0.5 + 0.3};
`;
debrisContainer.appendChild(debris);
}
// Add debris animation
const style = document.createElement('style');
style.textContent = `
@keyframes debrisFloat {
0% { transform: translateY(100vh) translateX(0px) rotate(0deg); }
100% { transform: translateY(-100px) translateX(${Math.random() * 200 - 100}px) rotate(360deg); }
}
`;
document.head.appendChild(style);
}
createEnergyPulses() {
setInterval(() => {
const pulse = document.createElement('div');
pulse.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
width: 4px;
height: 4px;
background: #00ffff;
border-radius: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
z-index: 1000;
animation: energyPulse 2s ease-out forwards;
`;
document.body.appendChild(pulse);
setTimeout(() => {
if (pulse.parentNode) {
pulse.parentNode.removeChild(pulse);
}
}, 2000);
}, 3000);
// Add energy pulse animation
const style = document.createElement('style');
style.textContent = `
@keyframes energyPulse {
0% { width: 4px; height: 4px; opacity: 1; }
100% { width: 200px; height: 200px; opacity: 0; }
}
`;
document.head.appendChild(style);
}
createShieldEffects() {
const shieldBar = document.querySelector('.shield-fill');
if (shieldBar) {
setInterval(() => {
const currentShield = Math.random() * 20 + 80; // 80-100%
shieldBar.style.width = currentShield + '%';
// Color change based on shield level
if (currentShield < 30) {
shieldBar.style.background = 'linear-gradient(90deg, #ff0000, #ff0000)';
this.soundSystem.playShieldHit();
} else if (currentShield < 60) {
shieldBar.style.background = 'linear-gradient(90deg, #ff0000, #ffff00)';
} else {
shieldBar.style.background = 'linear-gradient(90deg, #00ff00, #ffff00, #00ff00)';
}
}, 5000);
}
}
createHoverEffect(object) {
object.style.transform = 'translateY(-15px) scale(1.1)';
object.style.boxShadow = '0 0 40px rgba(0, 255, 255, 0.8)';
// Create energy rings
const ring = document.createElement('div');
ring.className = 'energy-ring';
ring.style.cssText = `
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
border: 2px solid #00ffff;
border-radius: 50%;
transform: translate(-50%, -50%);
animation: energyRing 0.5s ease-out forwards;
pointer-events: none;
`;
object.appendChild(ring);
setTimeout(() => {
if (ring.parentNode) {
ring.parentNode.removeChild(ring);
}
}, 500);
}
removeHoverEffect(object) {
object.style.transform = '';
object.style.boxShadow = '';
}
createClickEffect(object) {
const explosion = document.createElement('div');
explosion.style.cssText = `
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
background: radial-gradient(circle, #00ffff, transparent);
border-radius: 50%;
transform: translate(-50%, -50%);
animation: spaceExplosion 0.6s ease-out forwards;
pointer-events: none;
z-index: 1000;
`;
object.appendChild(explosion);
setTimeout(() => {
if (explosion.parentNode) {
explosion.parentNode.removeChild(explosion);
}
}, 600);
}
setupAchievementSystem() {
// Show achievement on page load - only once
setTimeout(() => {
this.showAchievement('Space Station Online!');
}, 2000);
}
showAchievement(text) {
// Play achievement sound
this.soundSystem.playAchievement();
// Create achievement notification
const achievement = document.createElement('div');
achievement.className = 'space-achievement';
achievement.innerHTML = `
<div class="achievement-content">
<i class="fas fa-star"></i>
<span>${text}</span>
</div>
`;
// Style the achievement
achievement.style.cssText = `
position: fixed;
top: 113px;
right: -300px;
background: linear-gradient(45deg, #ffcc00, #ffff00);
color: #000;
padding: 15px 25px;
border-radius: 25px;
font-family: 'Press Start 2P', monospace;
font-size: 12px;
z-index: 10000;
box-shadow: 0 0 20px rgba(255, 204, 0, 0.5);
transition: right 0.5s ease;
display: flex;
align-items: center;
gap: 10px;
`;
document.body.appendChild(achievement);
// Animate in
setTimeout(() => {
achievement.style.right = '20px';
}, 100);
// Animate out and remove
setTimeout(() => {
achievement.style.right = '-300px';
setTimeout(() => {
if (achievement.parentNode) {
achievement.parentNode.removeChild(achievement);
}
}, 500);
}, 3000);
}
}
// Add keyboard shortcuts for space feel
document.addEventListener('keydown', (e) => {
// M key to toggle music
if (e.code === 'KeyM') {
e.preventDefault();
const musicBtn = document.getElementById('music-toggle');
if (musicBtn) {
musicBtn.click();
}
}
});
// Add space explosion animation
const style = document.createElement('style');
style.textContent = `
@keyframes spaceExplosion {
0% { width: 10px; height: 10px; opacity: 1; }
100% { width: 100px; height: 100px; opacity: 0; }
}
@keyframes energyRing {
0% { width: 20px; height: 20px; opacity: 1; }
100% { width: 100px; height: 100px; opacity: 0; }
}
@keyframes fadeOut {
0% { opacity: 1; }
100% { opacity: 0; visibility: hidden; }
}
`;
document.head.appendChild(style);
// Space Rocket Game
class SpaceRocketGame {
constructor() {
this.canvas = document.getElementById('game-canvas');
this.ctx = this.canvas ? this.canvas.getContext('2d') : null;
this.gameModal = document.getElementById('game-modal');
this.gameOverElement = document.getElementById('game-over');
console.log('Game constructor - Canvas:', this.canvas, 'Modal:', this.gameModal);
this.gameState = {
running: false,
level: 1,
score: 0,
lives: 3,
gameSpeed: 2
};
this.rocket = {
x: this.canvas.width / 2,
y: this.canvas.height - 60,
width: 40,
height: 40,
speed: 5
};
this.asteroids = [];
this.stars = [];
this.bullets = [];
this.keys = {};
this.lastShot = 0;
this.shootCooldown = 200; // milliseconds between shots
this.init();
}
init() {
this.setupEventListeners();
this.setupCanvasResize();
this.gameLoop();
}
setupCanvasResize() {
// Handle canvas resize for responsive design
const resizeCanvas = () => {
if (!this.canvas) return;
const container = this.canvas.parentElement;
const containerWidth = container.clientWidth - 8; // Account for reduced padding
const containerHeight = container.clientHeight - 100; // Account for header and controls
// Check if we're in portrait mode on mobile
const isPortrait = window.innerWidth < 480 && window.innerHeight > window.innerWidth;
const maxHeight = isPortrait ? window.innerHeight * 0.5 : window.innerHeight * 0.6;
// Calculate responsive dimensions
let canvasWidth = Math.min(containerWidth, 800); // Max 800px width
let canvasHeight = Math.min(canvasWidth / 2, maxHeight); // 2:1 aspect ratio
// For portrait mode, adjust to use more vertical space
if (isPortrait) {
const availableHeight = Math.min(containerHeight, maxHeight);
// Use a more square aspect ratio for portrait mode
canvasHeight = Math.min(availableHeight, canvasWidth * 0.8); // 1.25:1 aspect ratio
canvasWidth = canvasHeight / 0.8; // Adjust width to maintain aspect ratio
// Ensure we don't exceed container width
if (canvasWidth > containerWidth) {
canvasWidth = containerWidth;
canvasHeight = canvasWidth * 0.8;
}
}
// Set canvas display size
this.canvas.style.width = canvasWidth + 'px';
this.canvas.style.height = canvasHeight + 'px';
// Set canvas internal resolution (for crisp rendering)
const devicePixelRatio = window.devicePixelRatio || 1;
this.canvas.width = canvasWidth * devicePixelRatio;
this.canvas.height = canvasHeight * devicePixelRatio;
// Scale the context to match device pixel ratio
this.ctx.scale(devicePixelRatio, devicePixelRatio);
// Update rocket position if game is running
if (this.gameState.running) {
this.rocket.x = canvasWidth / 2;
this.rocket.y = canvasHeight - 60;
}
};
// Initial resize
resizeCanvas();
// Resize on window resize and orientation change
window.addEventListener('resize', resizeCanvas);
window.addEventListener('orientationchange', () => {
setTimeout(resizeCanvas, 100); // Small delay for orientation change
});
// Resize when game starts
const originalStartGame = this.startGame.bind(this);
this.startGame = () => {
originalStartGame();
setTimeout(resizeCanvas, 100); // Small delay to ensure modal is visible
};
}
setupEventListeners() {
// Rocket click to start game
const rocketElement = document.getElementById('rocket-game-trigger');
console.log('Rocket element found:', rocketElement);
if (rocketElement) {
rocketElement.addEventListener('click', () => {
console.log('Rocket clicked! Starting game...');
this.startGame();
});
} else {
console.error('Rocket element not found!');
}
// Play Game button click to start game
const playGameElement = document.getElementById('play-game-trigger');
console.log('Play Game element found:', playGameElement);
if (playGameElement) {
playGameElement.addEventListener('click', () => {
console.log('Play Game clicked! Starting game...');
this.startGame();
});
} else {
console.error('Play Game element not found!');
}
// Mobile touch controls
this.setupMobileControls();
// Close game
const closeBtn = document.getElementById('close-game');
if (closeBtn) {
closeBtn.addEventListener('click', (e) => {
console.log('Close button clicked!');
e.preventDefault();
e.stopPropagation();
this.closeGame();
});
} else {
console.error('Close button not found!');
}
// Play again
const playAgainBtn = document.getElementById('play-again');
if (playAgainBtn) {
playAgainBtn.addEventListener('click', () => {
this.restartGame();
});
} else {
console.error('Play again button not found!');
}
// Keyboard controls
document.addEventListener('keydown', (e) => {
console.log('Key pressed:', e.code, e.key);
this.keys[e.code] = true;
// Prevent default behavior for spacebar (scrolling)
if (e.code === 'Space') {
e.preventDefault();
}
});
document.addEventListener('keyup', (e) => {
this.keys[e.code] = false;
});
}
startGame() {
console.log('Starting game...', this.gameModal);
console.log('Game modal element:', this.gameModal);
console.log('Game modal classes before:', this.gameModal.className);
this.gameModal.classList.add('active');
console.log('Game modal classes after:', this.gameModal.className);
// Force show modal for testing
this.gameModal.style.display = 'flex';
this.gameModal.style.zIndex = '9999';
console.log('Modal display forced to flex');
// Focus the canvas for keyboard input
if (this.canvas) {
this.canvas.focus();
this.canvas.tabIndex = 0; // Make canvas focusable
}
// Add click handler to focus canvas
if (this.canvas) {
this.canvas.addEventListener('click', () => {
this.canvas.focus();
});
}
this.gameState.running = true;
this.gameState.level = 1;
this.gameState.score = 0;
this.gameState.lives = 3;
this.gameState.gameSpeed = 2;
this.rocket.x = this.canvas.width / 2;
this.rocket.y = this.canvas.height - 60;
this.asteroids = [];
this.stars = [];
this.bullets = [];
this.updateUI();
// Get sound system from existing SpaceObjectsSystem
const spaceSystem = window.spaceObjectsSystem;
if (spaceSystem && spaceSystem.soundSystem) {
spaceSystem.soundSystem.playSpaceClick();
}
// Show mobile controls if on mobile device
this.showMobileControlsIfNeeded();
}
// Mobile device detection
isMobileDevice() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
(navigator.maxTouchPoints && navigator.maxTouchPoints > 2) ||
window.matchMedia("(hover: none) and (pointer: coarse)").matches;
}
// Show mobile controls if needed
showMobileControlsIfNeeded() {
const mobileControls = document.getElementById('mobile-controls');
if (mobileControls && this.isMobileDevice()) {
mobileControls.classList.add('active');
console.log('Mobile controls activated');
}
}
// Setup mobile touch controls
setupMobileControls() {
// Mobile shoot buttons (left and right)
const mobileShootLeft = document.getElementById('mobile-shoot-left');
const mobileShootRight = document.getElementById('mobile-shoot-right');
const setupShootButton = (button) => {
if (button) {
button.addEventListener('touchstart', (e) => {
e.preventDefault();
this.keys['Space'] = true;
this.shoot();
});
button.addEventListener('touchend', (e) => {
e.preventDefault();
this.keys['Space'] = false;
});
// Also handle click for desktop testing
button.addEventListener('click', (e) => {
e.preventDefault();
this.shoot();
});
}
};
setupShootButton(mobileShootLeft);
setupShootButton(mobileShootRight);
// Mobile direction buttons
const mobileUp = document.getElementById('mobile-up');
const mobileDown = document.getElementById('mobile-down');
const mobileLeft = document.getElementById('mobile-left');
const mobileRight = document.getElementById('mobile-right');
if (mobileUp) {
mobileUp.addEventListener('touchstart', (e) => {
e.preventDefault();
this.keys['ArrowUp'] = true;
});
mobileUp.addEventListener('touchend', (e) => {
e.preventDefault();
this.keys['ArrowUp'] = false;
});
mobileUp.addEventListener('click', (e) => {
e.preventDefault();
this.keys['ArrowUp'] = true;
setTimeout(() => this.keys['ArrowUp'] = false, 100);
});
}
if (mobileDown) {
mobileDown.addEventListener('touchstart', (e) => {
e.preventDefault();
this.keys['ArrowDown'] = true;
});
mobileDown.addEventListener('touchend', (e) => {
e.preventDefault();
this.keys['ArrowDown'] = false;
});
mobileDown.addEventListener('click', (e) => {
e.preventDefault();
this.keys['ArrowDown'] = true;
setTimeout(() => this.keys['ArrowDown'] = false, 100);
});
}
if (mobileLeft) {
mobileLeft.addEventListener('touchstart', (e) => {
e.preventDefault();
this.keys['ArrowLeft'] = true;
});
mobileLeft.addEventListener('touchend', (e) => {
e.preventDefault();
this.keys['ArrowLeft'] = false;
});
mobileLeft.addEventListener('click', (e) => {
e.preventDefault();
this.keys['ArrowLeft'] = true;
setTimeout(() => this.keys['ArrowLeft'] = false, 100);
});
}
if (mobileRight) {
mobileRight.addEventListener('touchstart', (e) => {
e.preventDefault();
this.keys['ArrowRight'] = true;
});
mobileRight.addEventListener('touchend', (e) => {
e.preventDefault();
this.keys['ArrowRight'] = false;
});
mobileRight.addEventListener('click', (e) => {
e.preventDefault();
this.keys['ArrowRight'] = true;
setTimeout(() => this.keys['ArrowRight'] = false, 100);
});
}
}
closeGame() {
console.log('closeGame() called');
console.log('gameModal:', this.gameModal);
console.log('gameOverElement:', this.gameOverElement);
// Force hide the modal
this.gameModal.classList.remove('active');
this.gameModal.style.display = 'none';
// Hide game over screen
this.gameOverElement.classList.add('hidden');
this.gameOverElement.style.display = 'none';
// Stop the game
this.gameState.running = false;
console.log('Game closed - modal should be hidden');
}
restartGame() {
this.gameOverElement.classList.add('hidden');
this.startGame();
}
updateUI() {
document.getElementById('level-display').textContent = `LEVEL: ${this.gameState.level}`;