diff --git a/src/utils/Sound.ts b/src/utils/Sound.ts new file mode 100644 index 0000000..c99fcad --- /dev/null +++ b/src/utils/Sound.ts @@ -0,0 +1,77 @@ +import { sound } from "@pixi/sound"; +import { logger } from "@/core/Logger"; + +class SoundManager { + private volume: number = 1; + private muted: boolean = false; + + constructor() { + sound.volume = this.volume; + } + + add(name: string, url: string, options?: { singleInstance?: boolean; autoPlay?: boolean }): void { + sound.add(name, { + url, + singleInstance: options?.singleInstance ?? true, + autoPlay: options?.autoPlay ?? false, + }); + logger.debug(`Sound: added "${name}"`); + } + + play(name: string): void { + if (this.muted) return; + sound.play(name); + } + + stop(name: string): void { + sound.stop(name); + } + + pause(name: string): void { + sound.pause(name); + } + + pauseAll(): void { + sound.pauseAll(); + } + + resumeAll(): void { + if (!this.muted) { + sound.resumeAll(); + } + } + + setVolume(volume: number): void { + this.volume = Math.max(0, Math.min(1, volume)); + sound.volume = this.volume; + } + + getVolume(): number { + return this.volume; + } + + mute(): void { + this.muted = true; + sound.muteAll(); + } + + unmute(): void { + this.muted = false; + sound.unmuteAll(); + } + + isMuted(): boolean { + return this.muted; + } + + exists(name: string): boolean { + return sound.exists(name); + } + + clear(): void { + sound.removeAll(); + } +} + +export const soundManager = new SoundManager(); +export default soundManager; \ No newline at end of file