pinball/assets/_Game/Scripts/Manager/AudioManager.ts

178 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-06-07 00:08:39 -07:00
import { _decorator, AudioClip, AudioSource } from 'cc';
2024-05-27 02:19:31 -07:00
2024-06-07 00:08:39 -07:00
interface ISoundOptions {
volume?: number;
loop?: boolean;
playbackRate?: number;
}
export class SoundSource {
2024-05-27 02:19:31 -07:00
private _source: AudioSource;
public get playing() {
return this._source.playing;
}
public get loop() {
return this._source.loop;
}
public set loop(value: boolean) {
this._source.loop = value;
}
private _volume = 1;
public get volume() {
return this._volume;
}
public set volume(value: number) {
this._volume = value;
if (!this.mute) {
this._source.volume = value;
}
}
private _mute = false;
public get mute() {
return this._mute;
}
public set mute(value: boolean) {
if (value) {
this._source.volume = 0;
} else {
this._source.volume = this.volume;
}
this._mute = value;
}
2024-06-07 00:08:39 -07:00
public get clip() {
return this._source.clip;
}
public set clip(value: AudioClip) {
this._source.clip = value;
}
public get playbackRate() {
return this._source.getPlaybackRate();
}
public set playbackRate(value: number) {
this._source.setPlaybackRate(value);
}
2024-05-27 02:19:31 -07:00
constructor(audioClip: AudioClip) {
this._source = new AudioSource();
this._source.playOnAwake = false;
this._source.clip = audioClip;
}
public play() {
if (!this.loop) {
if (this.playing) {
this._source.playOneShot(this._source.clip, this.mute ? 0 : this.volume);
return;
}
this._source.play();
} else {
if (!this.playing) this._source.play();
}
}
public stop() {
this._source.stop();
}
}
export default class AudioManager {
2024-06-07 00:08:39 -07:00
private static readonly storageKey = 'gad-game-galaxy-seeker-mute';
private static _audioSourcesSfx: Map<AudioClip, SoundSource> = new Map();
private static _audioSourceBgm: SoundSource;
2024-05-27 02:19:31 -07:00
private static _isMute: boolean = false;
public static get mute() {
return this._isMute;
}
public static toggleMute(): boolean {
this._isMute = !this._isMute;
this.setMute(this._isMute);
return this._isMute;
}
public static setMute(mute: boolean) {
2024-06-07 00:08:39 -07:00
this._audioSourceBgm.mute = mute;
2024-05-27 02:19:31 -07:00
this._audioSourcesSfx.forEach((source) => {
2024-06-07 00:08:39 -07:00
source.mute = mute;
2024-05-27 02:19:31 -07:00
});
2024-06-07 00:08:39 -07:00
this._isMute = mute;
2024-05-27 02:19:31 -07:00
}
2024-06-07 00:08:39 -07:00
public static playBGM(audio: AudioClip, opts?: ISoundOptions) {
const config: ISoundOptions = {
volume: 1,
2024-05-27 02:19:31 -07:00
loop: true,
2024-06-07 00:08:39 -07:00
playbackRate: 1,
2024-05-27 02:19:31 -07:00
...opts,
2024-06-07 00:08:39 -07:00
};
if (this._audioSourceBgm) {
this._audioSourceBgm.stop();
this._audioSourceBgm.clip = audio;
} else {
this._audioSourceBgm = new SoundSource(audio);
}
this._audioSourceBgm.loop = config.loop;
this._audioSourceBgm.volume = config.volume;
this._audioSourceBgm.mute = this._isMute;
2024-05-27 02:19:31 -07:00
this._audioSourceBgm.play();
2024-06-17 04:07:20 -07:00
this._audioSourceBgm.playbackRate = config.playbackRate;
2024-05-27 02:19:31 -07:00
}
public static setPlayRateBGM(rate: number) {
2024-06-07 00:08:39 -07:00
this._audioSourceBgm.playbackRate = rate;
2024-05-27 02:19:31 -07:00
}
2024-06-07 00:08:39 -07:00
public static playSfx(audioClip: AudioClip, opts?: ISoundOptions) {
2024-05-27 02:19:31 -07:00
let soundSource = this._audioSourcesSfx.get(audioClip);
2024-06-07 00:08:39 -07:00
const config: ISoundOptions = {
2024-05-27 02:19:31 -07:00
volume: 1,
loop: false,
2024-06-07 00:08:39 -07:00
playbackRate: 1,
2024-05-27 02:19:31 -07:00
...opts,
};
if (soundSource) {
soundSource.volume = config.volume;
soundSource.loop = config.loop;
soundSource.mute = this._isMute;
soundSource.play();
return;
}
2024-06-07 00:08:39 -07:00
soundSource = new SoundSource(audioClip);
2024-05-27 02:19:31 -07:00
soundSource.loop = config.loop;
soundSource.volume = config.volume;
soundSource.mute = this._isMute;
this._audioSourcesSfx.set(audioClip, soundSource);
soundSource.play();
}
public static stopBgm() {
this._audioSourceBgm.stop();
}
public static stopSfx(audioClip: AudioClip) {
this._audioSourcesSfx.get(audioClip).stop();
}
public static stopAllSfxLoop() {
this._audioSourcesSfx.forEach((sfx) => sfx.stop());
}
2024-06-07 00:08:39 -07:00
public static findAudioSourcesSfx(audioClip: AudioClip): SoundSource {
2024-05-27 02:19:31 -07:00
return this._audioSourcesSfx.get(audioClip);
}
}