Skip to content

Commit d13f9f7

Browse files
Add local Three.js files to fix CORS issues
1 parent 0b30e33 commit d13f9f7

File tree

14 files changed

+55595
-76
lines changed

14 files changed

+55595
-76
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<script type="importmap">
1111
{
1212
"imports": {
13-
"three": "https://unpkg.com/three@0.159.0/build/three.module.js",
14-
"three/": "https://unpkg.com/three@0.159.0/"
13+
"three": "./js/libs/three/three.module.js",
14+
"three/": "./js/libs/three/"
1515
}
1616
}
1717
</script>

js/global-starter.js

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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
58
window.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
};

js/libs/three-setup.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Import Three.js modules from CDN with explicit version
2-
const CDN_PATH = 'https://unpkg.com/three@0.159.0/';
1+
// Import Three.js modules locally
2+
const LOCAL_PATH = './three/';
33

44
// Import core Three.js
5-
import * as THREE from 'https://unpkg.com/three@0.159.0/build/three.module.js';
5+
import * as THREE from './three/three.module.js';
66

77
// Import controllers
8-
import { OrbitControls } from 'https://unpkg.com/three@0.159.0/examples/jsm/controls/OrbitControls.js';
8+
import { OrbitControls } from './three/examples/jsm/controls/OrbitControls.js';
99

1010
// Import post-processing
11-
import { EffectComposer } from 'https://unpkg.com/three@0.159.0/examples/jsm/postprocessing/EffectComposer.js';
12-
import { RenderPass } from 'https://unpkg.com/three@0.159.0/examples/jsm/postprocessing/RenderPass.js';
13-
import { ShaderPass } from 'https://unpkg.com/three@0.159.0/examples/jsm/postprocessing/ShaderPass.js';
14-
import { CopyShader } from 'https://unpkg.com/three@0.159.0/examples/jsm/shaders/CopyShader.js';
15-
import { UnrealBloomPass } from 'https://unpkg.com/three@0.159.0/examples/jsm/postprocessing/UnrealBloomPass.js';
11+
import { EffectComposer } from './three/examples/jsm/postprocessing/EffectComposer.js';
12+
import { RenderPass } from './three/examples/jsm/postprocessing/RenderPass.js';
13+
import { ShaderPass } from './three/examples/jsm/postprocessing/ShaderPass.js';
14+
import { CopyShader } from './three/examples/jsm/shaders/CopyShader.js';
15+
import { UnrealBloomPass } from './three/examples/jsm/postprocessing/UnrealBloomPass.js';
1616

1717
// Re-export all modules
1818
export {

0 commit comments

Comments
 (0)