2020-05-09 14:45:23 +00:00
|
|
|
/* typehints:start */
|
|
|
|
import { GameRoot } from "./root";
|
|
|
|
/* typehints:end */
|
|
|
|
|
|
|
|
import { Vector } from "../core/vector";
|
|
|
|
import { SOUNDS } from "../platform/sound";
|
|
|
|
|
2020-06-25 11:12:35 +00:00
|
|
|
const avgSoundDurationSeconds = 0.1;
|
2020-06-17 12:56:21 +00:00
|
|
|
const maxOngoingSounds = 2;
|
2020-06-25 11:12:35 +00:00
|
|
|
const maxOngoingUiSounds = 5;
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
// Proxy to the application sound instance
|
|
|
|
export class SoundProxy {
|
|
|
|
/**
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
constructor(root) {
|
|
|
|
this.root = root;
|
|
|
|
|
|
|
|
// Store a list of sounds and when we started them
|
2020-06-17 12:56:21 +00:00
|
|
|
this.playing3DSounds = [];
|
|
|
|
this.playingUiSounds = [];
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays a new ui sound
|
|
|
|
* @param {string} id Sound ID
|
|
|
|
*/
|
|
|
|
playUi(id) {
|
|
|
|
assert(typeof id === "string", "Not a valid sound id: " + id);
|
2020-06-17 12:56:21 +00:00
|
|
|
this.internalUpdateOngoingSounds();
|
|
|
|
if (this.playingUiSounds.length > maxOngoingUiSounds) {
|
|
|
|
// Too many ongoing sounds
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
this.root.app.sound.playUiSound(id);
|
2020-06-17 12:56:21 +00:00
|
|
|
this.playingUiSounds.push(this.root.time.realtimeNow());
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the ui click sound
|
|
|
|
*/
|
|
|
|
playUiClick() {
|
|
|
|
this.playUi(SOUNDS.uiClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the ui error sound
|
|
|
|
*/
|
|
|
|
playUiError() {
|
|
|
|
this.playUi(SOUNDS.uiError);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays a 3D sound whose volume is scaled based on where it was emitted
|
|
|
|
* @param {string} id Sound ID
|
|
|
|
* @param {Vector} pos World space position
|
|
|
|
*/
|
|
|
|
play3D(id, pos) {
|
|
|
|
assert(typeof id === "string", "Not a valid sound id: " + id);
|
|
|
|
assert(pos instanceof Vector, "Invalid sound position");
|
|
|
|
this.internalUpdateOngoingSounds();
|
|
|
|
|
2020-06-17 12:56:21 +00:00
|
|
|
if (this.playing3DSounds.length > maxOngoingSounds) {
|
2020-05-09 14:45:23 +00:00
|
|
|
// Too many ongoing sounds
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.root.app.sound.play3DSound(id, pos, this.root);
|
2020-06-17 12:56:21 +00:00
|
|
|
this.playing3DSounds.push(this.root.time.realtimeNow());
|
2020-05-09 14:45:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the list of ongoing sounds
|
|
|
|
*/
|
|
|
|
internalUpdateOngoingSounds() {
|
|
|
|
const now = this.root.time.realtimeNow();
|
2020-06-17 12:56:21 +00:00
|
|
|
for (let i = 0; i < this.playing3DSounds.length; ++i) {
|
|
|
|
if (now - this.playing3DSounds[i] > avgSoundDurationSeconds) {
|
|
|
|
this.playing3DSounds.splice(i, 1);
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < this.playingUiSounds.length; ++i) {
|
|
|
|
if (now - this.playingUiSounds[i] > avgSoundDurationSeconds) {
|
|
|
|
this.playingUiSounds.splice(i, 1);
|
2020-05-09 14:45:23 +00:00
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|