@@ -12,7 +12,11 @@ import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPa
12
12
// Set up the scene
13
13
const scene = new THREE . Scene ( ) ;
14
14
15
- // Create atmospheric space background
15
+ /**
16
+ * Create atmospheric space background as a large sphere with a nebula-like gradient and subtle star effects.
17
+ * @returns background mesh to be added to the scene
18
+ */
19
+
16
20
function createSpaceBackground ( ) {
17
21
// Create background geometry - a large sphere that encompasses the scene
18
22
const backgroundGeometry = new THREE . SphereGeometry ( 500 , 32 , 16 ) ;
@@ -112,7 +116,11 @@ function createSpaceBackground() {
112
116
const spaceBackground = createSpaceBackground ( ) ;
113
117
scene . add ( spaceBackground ) ;
114
118
115
- // Calculate responsive field of view based on screen size
119
+ /**
120
+ * Calculate responsive field of view based on screen size
121
+ * @returns the calculated field of view value adjusted for screen dimensions
122
+ */
123
+
116
124
function getResponsiveFOV ( ) : number {
117
125
const minFOV = 60 ; // Current desktop FOV
118
126
const maxFOV = 100 ; // Wider view for small screens to fit more content
@@ -126,7 +134,11 @@ function getResponsiveFOV(): number {
126
134
return fov ;
127
135
}
128
136
129
- // Calculate responsive camera positions based on screen size
137
+ /**
138
+ * Calculate responsive camera positions based on screen size
139
+ * @returns an object containing introZ and gameZ camera positions adjusted for screen width
140
+ */
141
+
130
142
function getResponsiveCameraPositions ( ) {
131
143
const minIntroZ = 3.0 ; // Current desktop intro position
132
144
const maxIntroZ = 4.0 ; // Further back for mobile
@@ -185,7 +197,12 @@ const composer = new EffectComposer(renderer);
185
197
const renderPass = new RenderPass ( scene , camera ) ;
186
198
composer . addPass ( renderPass ) ;
187
199
188
- // Calculate responsive pixel size based on screen size
200
+ /**
201
+ * Calculate responsive pixel size based on screen width and height.
202
+ * Returns a pixel size that scales between a minimum and maximum value depending on screen dimensions.
203
+ * @returns responsive pixel size based on current screen size
204
+ */
205
+
189
206
function getResponsivePixelSize ( ) : number {
190
207
const minPixelSize = 2.0 ; // Smaller pixels for mobile
191
208
const maxPixelSize = 8.0 ; // Current desktop pixel size
@@ -199,7 +216,11 @@ function getResponsivePixelSize(): number {
199
216
return pixelSize ;
200
217
}
201
218
202
- // Calculate responsive scanline size based on screen size
219
+ /**
220
+ * Calculate responsive scanline size based on screen size.
221
+ * @returns the scanline size adjusted for the current screen dimensions.
222
+ */
223
+
203
224
function getResponsiveScanlineSize ( ) : number {
204
225
const minScanlineSize = 2 ; // Thinner scanlines for mobile
205
226
const maxScanlineSize = 5 ; // Current desktop scanline size
@@ -467,7 +488,10 @@ const crtOverlay = document.createElement("div");
467
488
crtOverlay . className = "crt-overlay" ;
468
489
document . body . appendChild ( crtOverlay ) ;
469
490
470
- // Function to update scanline size responsively
491
+ /**
492
+ * Function to update the scanline size responsively by adjusting the background size of the CRT overlay element.
493
+ */
494
+
471
495
function updateScanlineSize ( ) {
472
496
const scanlineSize = getResponsiveScanlineSize ( ) ;
473
497
if ( crtOverlay ) {
@@ -504,7 +528,10 @@ let musicGainNode: GainNode | null = null;
504
528
let isMuted = false ;
505
529
let musicFadeTimeout : number | null = null ; // Track fade timeout for cleanup
506
530
507
- // Load and decode the background music
531
+ /**
532
+ * Load and decode the background music asynchronously.
533
+ */
534
+
508
535
async function loadBackgroundMusic ( ) {
509
536
if ( ! audioContext ) return ;
510
537
@@ -517,7 +544,14 @@ async function loadBackgroundMusic() {
517
544
}
518
545
}
519
546
520
- // Start playing background music
547
+ /**
548
+ * Start playing background music with fade-in and looping.
549
+ * Clears any pending fade timeout before starting.
550
+ * Creates gain node for volume control and sets initial volume to 0.
551
+ * Connects audio nodes and starts playback with looping enabled.
552
+ * Handles music end event to restart music if game is still running.
553
+ */
554
+
521
555
function startBackgroundMusic ( ) {
522
556
if ( ! audioContext || ! musicBuffer || backgroundMusic ) return ;
523
557
@@ -564,7 +598,12 @@ function startBackgroundMusic() {
564
598
}
565
599
}
566
600
567
- // Stop background music
601
+ /**
602
+ * Stop background music with a fade out effect.
603
+ * Clears any pending fade timeout, fades out the music gain over 0.6 seconds,
604
+ * then stops and disconnects the background music and gain node.
605
+ */
606
+
568
607
function stopBackgroundMusic ( ) {
569
608
if ( ! backgroundMusic || ! musicGainNode || ! audioContext ) return ;
570
609
@@ -613,7 +652,10 @@ function stopBackgroundMusic() {
613
652
}
614
653
}
615
654
616
- // Toggle mute state
655
+ /**
656
+ * Toggle mute state and smoothly fade audio volume.
657
+ */
658
+
617
659
function toggleMute ( ) {
618
660
isMuted = ! isMuted ;
619
661
0 commit comments