added ambient sound feature

This commit is contained in:
2025-11-14 16:08:51 +02:00
parent 006d159a0f
commit 8eea84b697
5 changed files with 42 additions and 3 deletions
+22
View File
@@ -107,6 +107,10 @@ class GameEngine extends THREE.EventDispatcher{
const dashboard = new DashBoard(this);
this.dashboard = dashboard;
this.listener = new THREE.AudioListener();
this.camera.add(this.listener);
this.ambientSound = new THREE.Audio(this.listener);
this.activeObjects = new THREE.Group();
scene.add(this.activeObjects);
@@ -558,9 +562,19 @@ class GameEngine extends THREE.EventDispatcher{
this.physics.clear();
this.clickable.removeAll();
this.motionQueue.clearAll();
this.ambientSound.stop();
}
async playAmbientSound(source, path){
let buffer = await GameEngine.loadAudio(source, path);
this.ambientSound.setBuffer(buffer);
this.ambientSound.setLoop(true);
this.ambientSound.play();
}
static textureLoader = new THREE.TextureLoader();
static audioLoader = new THREE.AudioLoader();
static async loadTexture(url, path = assetPath, progress, assignTo) {
return new Promise((resolve, reject) => {
GameEngine.textureLoader.load(`${path}${url}`, texture => {
@@ -573,6 +587,14 @@ class GameEngine extends THREE.EventDispatcher{
})
}
static async loadAudio(url, path = assetPath, progress){
return new Promise((resolve, reject) => {
GameEngine.audioLoader.load(`${path}${url}`, buffer => {
resolve(buffer)
}, progress, reject)
})
}
loadTexture = GameEngine.loadTexture
}