11// This script ensures the startGame function is available globally
22// It's loaded without type="module" to ensure global scope
33
4+ // Wait for main.js to load and initialize
5+ let gameInitialized = false ;
6+
47// A backup implementation for window.startGame
58window . startGame = function ( ) {
69 console . log ( "Global startGame function called from global-starter.js" ) ;
@@ -12,59 +15,37 @@ window.startGame = function() {
1215 return ;
1316 }
1417
15- console . log ( "Game instance not found or not ready! Will retry in 500ms..." ) ;
16-
17- // Retry after a short delay to handle race conditions
18- setTimeout ( ( ) => {
19- if ( window . game && typeof window . game . startGame === 'function' ) {
20- console . log ( "Retrying game start..." ) ;
21- window . game . startGame ( ) ;
22- } else {
23- // Try a longer delay as a last resort
24- console . log ( "Game instance still not available after delay. Final retry in 1 second..." ) ;
25-
18+ // If game is not initialized yet, wait for main.js to load
19+ if ( ! gameInitialized ) {
20+ console . log ( "Waiting for main.js to initialize..." ) ;
21+
22+ // Check if main.js script element exists
23+ const mainScript = document . querySelector ( 'script[src*="main.js"]' ) ;
24+ if ( ! mainScript ) {
25+ console . error ( "main.js script not found in document!" ) ;
26+ return ;
27+ }
28+
29+ // Create a new promise that resolves when main.js loads
30+ const waitForMain = new Promise ( ( resolve ) => {
31+ mainScript . addEventListener ( 'load' , resolve ) ;
32+ } ) ;
33+
34+ // Wait for main.js to load and then try to start the game
35+ waitForMain . then ( ( ) => {
36+ console . log ( "main.js loaded, attempting to start game..." ) ;
2637 setTimeout ( ( ) => {
2738 if ( window . game && typeof window . game . startGame === 'function' ) {
28- console . log ( "Final retry successful! " ) ;
39+ console . log ( "Game instance found after main.js load, starting game... " ) ;
2940 window . game . startGame ( ) ;
3041 } else {
31- console . log ( "Game instance still not available after final delay." ) ;
32-
33- // Emergency: Create a new game instance if all else fails
34- try {
35- console . log ( "Emergency: attempting to create a new game instance..." ) ;
36- // Look for Game class, first on window, then as global
37- if ( typeof window . Game === 'function' ) {
38- console . log ( "Found Game class on window object" ) ;
39- window . game = new window . Game ( ) ;
40- window . game . startGame ( ) ;
41- } else if ( typeof Game === 'function' ) {
42- console . log ( "Found Game class as global" ) ;
43- window . game = new Game ( ) ;
44- window . game . startGame ( ) ;
45- } else {
46- console . error ( "Game class not found! Waiting for script to load..." ) ;
47-
48- // Wait for main.js to load completely
49- document . addEventListener ( 'DOMContentLoaded' , ( ) => {
50- console . log ( "DOM fully loaded, final attempt to start game" ) ;
51- if ( window . game ) {
52- window . game . startGame ( ) ;
53- } else if ( typeof window . Game === 'function' ) {
54- window . game = new window . Game ( ) ;
55- window . game . startGame ( ) ;
56- } else {
57- console . error ( "Unable to start game. Please refresh the page." ) ;
58- alert ( "Error starting game. Please refresh the page and try again." ) ;
59- }
60- } ) ;
61- }
62- } catch ( e ) {
63- console . error ( "Emergency game creation failed:" , e ) ;
64- alert ( "Error starting game. Please refresh the page and try again." ) ;
65- }
42+ console . error ( "Game instance still not available after main.js load" ) ;
6643 }
67- } , 1000 ) ;
68- }
69- } , 500 ) ;
44+ } , 100 ) ; // Small delay to ensure initialization
45+ } ) ;
46+
47+ return ;
48+ }
49+
50+ console . error ( "Unable to start game. Please refresh the page." ) ;
7051} ;
0 commit comments