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
@@ -70,7 +70,10 @@ class InteractiveObject extends EventDispatcher{
this.io = await new InteractiveObjectsImports[obj.type](gameEngine, obj); this.io = await new InteractiveObjectsImports[obj.type](gameEngine, obj);
this.source = this.io.source || this.io; this.source = this.io.source || this.io;
this.object = this.io.object; this.object = this.io.object;
this.io.addEventListener?.('finish', this.dispatchEvent.bind(this)) this.emits = this.io.emits;
this.io.emits?.forEach(event=>{
this.io.addEventListener?.(event, this.dispatchEvent.bind(this))
})
break; break;
} }
if (obj.shouldBeLocked){ if (obj.shouldBeLocked){
+9
View File
@@ -30,6 +30,12 @@
</template> </template>
</asset-selector> </asset-selector>
<asset-selector @select="assignAudio" :type="['Audio']">
<template v-slot:activator="props">
<v-btn v-bind="props" prepend-icon="mdi-volume-medium" block color="deep-purple-accent-2" class="my-4">Select ambient sound</v-btn>
</template>
</asset-selector>
<v-form class="py-4"> <v-form class="py-4">
<v-text-field density="compact" :label="l.name" v-model="modelValue.title"></v-text-field> <v-text-field density="compact" :label="l.name" v-model="modelValue.title"></v-text-field>
<v-textarea :label="l.description" v-model="modelValue.description"></v-textarea> <v-textarea :label="l.description" v-model="modelValue.description"></v-textarea>
@@ -88,6 +94,9 @@ export default {
assignIntro(e){ assignIntro(e){
this.modelValue.intro = e.id; this.modelValue.intro = e.id;
}, },
assignAudio(e){
this.modelValue.audio = e.id;
}
} }
} }
</script> </script>
+22
View File
@@ -107,6 +107,10 @@ class GameEngine extends THREE.EventDispatcher{
const dashboard = new DashBoard(this); const dashboard = new DashBoard(this);
this.dashboard = dashboard; 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(); this.activeObjects = new THREE.Group();
scene.add(this.activeObjects); scene.add(this.activeObjects);
@@ -558,9 +562,19 @@ class GameEngine extends THREE.EventDispatcher{
this.physics.clear(); this.physics.clear();
this.clickable.removeAll(); this.clickable.removeAll();
this.motionQueue.clearAll(); 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 textureLoader = new THREE.TextureLoader();
static audioLoader = new THREE.AudioLoader();
static async loadTexture(url, path = assetPath, progress, assignTo) { static async loadTexture(url, path = assetPath, progress, assignTo) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
GameEngine.textureLoader.load(`${path}${url}`, texture => { 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 loadTexture = GameEngine.loadTexture
} }
+6 -2
View File
@@ -100,7 +100,7 @@ export default {
async expandScenarioData(scene){ async expandScenarioData(scene){
const promises = []; const promises = [];
['environment', 'scene', 'intro'].filter(e=>scene.data[e]).forEach(e=>{ ['environment', 'scene', 'intro', 'audio'].filter(e=>scene.data[e]).forEach(e=>{
promises.push(this.$api.gameObject.load(scene.data[e]).then(r=>scene.data['$'+e] = r.data)) promises.push(this.$api.gameObject.load(scene.data[e]).then(r=>scene.data['$'+e] = r.data))
}) })
@@ -130,6 +130,10 @@ export default {
if (this.scene.data.$environment){ if (this.scene.data.$environment){
await gameEngine.loadPanorama(this.scene.data.$environment.asset.name); await gameEngine.loadPanorama(this.scene.data.$environment.asset.name);
} }
if (this.scene.data.$audio){
await gameEngine.playAmbientSound(this.scene.data.$audio.asset.name);
gameEngine.ambientSound.setVolume( 0.5 );
}
if (this.scene.data.$scene){ if (this.scene.data.$scene){
let env = await gameEngine.load(this.scene.data.$scene.asset.name); let env = await gameEngine.load(this.scene.data.$scene.asset.name);
this.setObjectAttributes(l, this.scene.data, env.scene, env, 100); this.setObjectAttributes(l, this.scene.data, env.scene, env, 100);
@@ -194,7 +198,7 @@ export default {
}); });
if (finished == expectToFinish){ if (finished == expectToFinish){
//GO TO NEXT LEVEL //GO TO NEXT LEVEL
console.log('LEVEL FINISHED')
} }
}) })
} }
+1
View File
@@ -38,6 +38,7 @@ export default {
}, { }, {
value: 'audio', value: 'audio',
icon: 'volume-medium', icon: 'volume-medium',
type: 'Audio',
color: 'deep-purple-accent-2' color: 'deep-purple-accent-2'
}] }]
} }