randomized sounds better, made config easier for adding new effects.

This commit is contained in:
Michael Mainguy 2023-08-22 16:04:52 -05:00
parent 9d964cec0e
commit 95899878c7
2 changed files with 15 additions and 19 deletions

View File

@ -39,25 +39,25 @@ export class CustomEnvironment {
window.setTimeout((sound) => {
sound.play()
}, 2000, sounds.background);
const birds: Array<Sound> = [sounds.birds, sounds.dove];
const effects: Array<Sound> = sounds.backgroundEffects;
window.setInterval((sounds: Array<Sound>) => {
if (Math.random() < .6) {
if (Math.random() < .5) {
return;
}
const sound = Math.floor(Math.random() * sounds.length);
const x = Math.floor(Math.random() * 20);
const z = Math.floor(Math.random() * 20);
const x = Math.floor(Math.random() * 20) - 10;
const z = Math.floor(Math.random() * 20) - 10;
const position = new Vector3(x, 0, z);
if (sounds[sound].isPlaying) {
} else {
sounds[sound].setPosition(position);
sounds[sound].setVolume(Math.random() * .5);
sounds[sound].setVolume(Math.random() * .3);
sounds[sound].play();
}
}, 2000, birds);
}, 2000, effects);
} catch (error) {
console.log(error);
}

View File

@ -3,8 +3,6 @@ import {Scene, Sound} from "@babylonjs/core";
export class DiaSounds {
private readonly scene: Scene;
private readonly _birds: Sound;
public get tick() {
return new Sound("tick", '/assets/sounds/tick.mp3', this.scene);
}
@ -53,8 +51,15 @@ export class DiaSounds {
volume: .5,
loop: false
}
this._birds = this.buildSpatialSound("warbler", "/assets/sounds/warbler.mp3");
this._dove = this.buildSpatialSound("dove", "/assets/sounds/dove.mp3");
this._backgroundEffects.push(this.buildSpatialSound("warbler2", "/assets/sounds/warbler2.mp3"));
this._backgroundEffects.push(this.buildSpatialSound("warbler3", "/assets/sounds/warbler3.mp3"));
this._backgroundEffects.push(this.buildSpatialSound("crickets", "/assets/sounds/crickets.mp3"));
this._backgroundEffects.push(this.buildSpatialSound("dove", "/assets/sounds/dove.mp3"))
}
_backgroundEffects: Array<Sound> = [];
public get backgroundEffects(): Array<Sound> {
return this._backgroundEffects;
}
private buildSpatialSound(name: string, url: string) {
@ -92,15 +97,6 @@ export class DiaSounds {
return this._low;
}
public get birds(): Sound {
return this._birds;
}
_dove: Sound;
public get dove() {
return this._dove;
}
public get bounce() {
const bounce = this._bounce.clone();
bounce.updateOptions({offset: 0, volume: this.volume, length: .990});