2021-04-21 16:47:19 +02:00
|
|
|
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
|
|
|
|
import BaseSoundManager = Phaser.Sound.BaseSoundManager;
|
|
|
|
import BaseSound = Phaser.Sound.BaseSound;
|
|
|
|
import SoundConfig = Phaser.Types.Sound.SoundConfig;
|
|
|
|
|
|
|
|
class SoundManager {
|
2021-09-06 14:27:54 +02:00
|
|
|
private soundPromises: Map<string, Promise<BaseSound>> = new Map<string, Promise<Phaser.Sound.BaseSound>>();
|
|
|
|
public loadSound(loadPlugin: LoaderPlugin, soundManager: BaseSoundManager, soundUrl: string): Promise<BaseSound> {
|
|
|
|
let soundPromise = this.soundPromises.get(soundUrl);
|
2021-04-23 15:35:34 +02:00
|
|
|
if (soundPromise !== undefined) {
|
|
|
|
return soundPromise;
|
|
|
|
}
|
2021-09-06 14:27:54 +02:00
|
|
|
soundPromise = new Promise<BaseSound>((res) => {
|
2021-05-07 17:03:07 +02:00
|
|
|
const sound = soundManager.get(soundUrl);
|
2021-04-21 16:47:19 +02:00
|
|
|
if (sound !== null) {
|
|
|
|
return res(sound);
|
|
|
|
}
|
|
|
|
loadPlugin.audio(soundUrl, soundUrl);
|
2021-09-06 14:27:54 +02:00
|
|
|
loadPlugin.once("filecomplete-audio-" + soundUrl, () => {
|
2021-06-02 16:46:28 +02:00
|
|
|
res(soundManager.add(soundUrl));
|
|
|
|
});
|
2021-04-21 16:47:19 +02:00
|
|
|
loadPlugin.start();
|
|
|
|
});
|
2021-09-06 14:27:54 +02:00
|
|
|
this.soundPromises.set(soundUrl, soundPromise);
|
2021-04-23 15:35:34 +02:00
|
|
|
return soundPromise;
|
2021-04-21 16:47:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:27:54 +02:00
|
|
|
public async playSound(
|
|
|
|
loadPlugin: LoaderPlugin,
|
|
|
|
soundManager: BaseSoundManager,
|
|
|
|
soundUrl: string,
|
|
|
|
config: SoundConfig | undefined
|
|
|
|
): Promise<void> {
|
|
|
|
const sound = await this.loadSound(loadPlugin, soundManager, soundUrl);
|
2021-04-23 17:03:17 +02:00
|
|
|
if (config === undefined) sound.play();
|
|
|
|
else sound.play(config);
|
2021-04-23 15:35:34 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 14:27:54 +02:00
|
|
|
public stopSound(soundManager: BaseSoundManager, soundUrl: string) {
|
2021-04-23 15:35:34 +02:00
|
|
|
soundManager.get(soundUrl).stop();
|
2021-04-21 16:47:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 16:08:43 +02:00
|
|
|
export const soundManager = new SoundManager();
|