@@ -11,6 +11,9 @@ const start_audio = new Audio("static/sounds/start.mp3");
1111
1212document . body . style . cursor = 'none' ;
1313
14+ let gameStartTimeout ;
15+ const GAME_START_COOLDOWN = 1000 ; // 1 second cooldown between game starts
16+
1417document . addEventListener ( "keydown" , async ( evt ) => {
1518 if ( games . length === 0 ) return ;
1619
@@ -31,18 +34,33 @@ document.addEventListener("keydown", async (evt) => {
3134 }
3235
3336 if ( evt . key === "Enter" ) {
37+ if ( gameStartTimeout ) return ; // Ignore if within cooldown period
38+
39+ gameStartTimeout = true ;
3440 await play_audio ( start_audio ) ;
3541 const url = games [ index ] . getAttribute ( "game-url" ) ;
3642 if ( url ) {
3743 window . open ( url , '_blank' ) ;
3844 }
45+
46+ // Reset cooldown after delay
47+ setTimeout ( ( ) => {
48+ gameStartTimeout = false ;
49+ } , GAME_START_COOLDOWN ) ;
3950 }
4051} ) ;
4152
4253function play_audio ( audio ) {
4354 return new Promise ( res => {
44- audio . play ( ) . catch ( ( ) => res ( ) ) ; // Handle cases where play is interrupted
45- audio . onended = res ;
55+ const onended = ( ) => {
56+ audio . removeEventListener ( 'ended' , onended ) ;
57+ res ( ) ;
58+ } ;
59+ audio . addEventListener ( 'ended' , onended ) ;
60+ audio . play ( ) . catch ( ( ) => {
61+ audio . removeEventListener ( 'ended' , onended ) ;
62+ res ( ) ;
63+ } ) ;
4664 } ) ;
4765}
4866
@@ -77,28 +95,35 @@ function gameLoop() {
7795
7896 // --- Axis (Stick) Navigation ---
7997 const axisThreshold = 0.7 ;
80- const leftStickX = gamepad . axes [ 0 ] ;
98+ const leftStickX = gamepad . axes [ 0 ] || 0 ;
99+ const lastLeftStickX = lastGamepadState . axes [ 0 ] || 0 ;
81100
82101 // Left Stick Right
83- if ( leftStickX > axisThreshold && lastGamepadState . axes [ 0 ] < axisThreshold ) {
102+ if ( leftStickX > axisThreshold && lastLeftStickX < axisThreshold ) {
84103 document . dispatchEvent ( new KeyboardEvent ( 'keydown' , { 'key' : 'ArrowRight' } ) ) ;
85104 }
86105 // Left Stick Left
87- if ( leftStickX < - axisThreshold && lastGamepadState . axes [ 0 ] > - axisThreshold ) {
106+ if ( leftStickX < - axisThreshold && lastLeftStickX > - axisThreshold ) {
88107 document . dispatchEvent ( new KeyboardEvent ( 'keydown' , { 'key' : 'ArrowLeft' } ) ) ;
89108 }
90109
91110 // --- Button Navigation ---
92111 // D-Pad Right (Button 15)
93- if ( gamepad . buttons [ 15 ] && ! lastGamepadState . buttons [ 15 ] ?. pressed ) {
112+ const btn15 = gamepad . buttons [ 15 ] ;
113+ const lastBtn15 = lastGamepadState . buttons [ 15 ] ;
114+ if ( btn15 ?. pressed && ! lastBtn15 ?. pressed ) {
94115 document . dispatchEvent ( new KeyboardEvent ( 'keydown' , { 'key' : 'ArrowRight' } ) ) ;
95116 }
96117 // D-Pad Left (Button 14)
97- if ( gamepad . buttons [ 14 ] && ! lastGamepadState . buttons [ 14 ] ?. pressed ) {
118+ const btn14 = gamepad . buttons [ 14 ] ;
119+ const lastBtn14 = lastGamepadState . buttons [ 14 ] ;
120+ if ( btn14 ?. pressed && ! lastBtn14 ?. pressed ) {
98121 document . dispatchEvent ( new KeyboardEvent ( 'keydown' , { 'key' : 'ArrowLeft' } ) ) ;
99122 }
100123 // A Button (or X on PS, Button 0) for Enter
101- if ( gamepad . buttons [ 0 ] && ! lastGamepadState . buttons [ 0 ] ?. pressed ) {
124+ const btn0 = gamepad . buttons [ 0 ] ;
125+ const lastBtn0 = lastGamepadState . buttons [ 0 ] ;
126+ if ( btn0 ?. pressed && ! lastBtn0 ?. pressed ) {
102127 document . dispatchEvent ( new KeyboardEvent ( 'keydown' , { 'key' : 'Enter' } ) ) ;
103128 }
104129
0 commit comments