-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSceneManager.js
More file actions
103 lines (87 loc) · 3.36 KB
/
SceneManager.js
File metadata and controls
103 lines (87 loc) · 3.36 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
import { createNight } from './scenes/night.js';
import { createDayDynamicTerrain } from './scenes/day.js';
import { createOutdoor } from './scenes/outdoor.js';
import { createRoom } from './scenes/room.js';
import { createUnderground } from './scenes/underground.js';
import { createTown } from './scenes/town.js';
import { createRoomGI } from './scenes/roomGI.js';
import { createInn } from './scenes/inn.js';
import { createBuilder } from './scenes/builder.js';
class SceneManager {
constructor(canvasId, engine) {
this.canvas = document.getElementById(canvasId);
this.engine = new BABYLON.Engine(this.canvas, true);
this.guiTextures = new Map();
this.scenes = [];
this.activeScene = null;
this.sceneCreators = {
night: createNight,
day: createDayDynamicTerrain,
outdoor: createOutdoor,
room: createRoom,
underground: createUnderground,
town: createTown,
roomGI: createRoomGI,
inn: createInn,
builder: createBuilder
};
}
async loadScene(sceneCreationFunction) {
const scene = await sceneCreationFunction(this.engine);
scene.damagePopupAnimationGroup = new BABYLON.AnimationGroup("popupAnimation", scene);
this.scenes.push(scene);
this.guiTextures.set(scene, new BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI", true, scene));
this.activeGUI = this.guiTextures.get(this.activeScene);
return scene;
}
async switchToScene(index) {
if (this.activeScene) {
this.engine.stopRenderLoop();
if (DEBUG) this.activeScene.debugLayer.hide();
// this.activeScene.dispose(); // Optional: dispose only if not planning to return to this scene
}
this.activeScene = this.scenes[index];
this.activeGUI = this.guiTextures.get(this.activeScene);
this.engine.runRenderLoop(() => {
this.activeScene.render();
});
if (DEBUG) this.activeScene.debugLayer.show();
}
// todo map of scenes near the current scene
// in this case, just load starting zone
async start() {
let timeout = 100;
if (!FAST_RELOAD) timeout = 1000;
setTimeout(() => {
this.canvas.classList.add('visible');
}, timeout);
const urlParams = new URLSearchParams(window.location.search);
const debugParam = urlParams.get('debug');
if (debugParam === 'true') { DEBUG = true; }
const sceneParam = urlParams.get('scene');
const defaultScene = this.sceneCreators[sceneParam] || this.sceneCreators.outdoor; // Default to outdoor if no valid scene parameter
await this.loadScene(defaultScene);
await this.switchToScene(0);
this.canvas.focus();
// Uncomment this for key based scene switching.
// await this.loadScene(this.sceneCreators['inn']);
// await this.loadScene(this.sceneCreators['builder']);
// Setup scene switching logic, e.g., based on user input or game events
// window.addEventListener('keydown', (e) => {
// if (e.key === 'i') {
// this.switchToScene(0);
// } else if (e.key === 'o') {
// this.switchToScene(1);
// } else if (e.key === 'p') {
// this.switchToScene(2);
// }
// });
window.addEventListener('resize', () => {
this.engine.resize();
});
const endTime = performance.now();
const domLoadTime = endTime - startTime;
console.log(`Scene loaded in ${domLoadTime.toFixed(2)} milliseconds`);
}
}
export default SceneManager;