import { AudioClip, AudioSource, Component, Node } from "cc"; import { constants } from "../data/constants"; import { designManager } from "./designManager"; import { localStorageManager } from "./localStorageManager"; import { resManager } from "./resManager"; export class audioManager { private static _instance: audioManager; private constructor() { }; public static get instance(): audioManager { if (!this._instance) { this._instance = new audioManager(); } return this._instance; } audioSource: AudioSource; audioClips: any = {}; musicVolume: number = 0; musicName: string = ""; init(audioSource: AudioSource) { this.audioSource = audioSource; this.musicVolume = localStorageManager.instance.getMusicVolume(); } loadAudio(onProgress?: Function, onComplete?: Function) { resManager.instance.loadAssetByBundleDir(constants.bundles.audio.name, "", AudioClip, onProgress, (assetArr) => { console.log("audioManager.loadAudio success"); for (let i in assetArr) { let asset = assetArr[i]; let name = asset.name; if (!name) { continue; } this.audioClips[name] = asset; } if (onComplete) { onComplete(); } }); } onRolePlayAnimation(component: Component, roleType: number, roleId: number, aniName: string) { if (roleType != constants.roleItemTypes.player && roleType != constants.roleItemTypes.monster) { return; } if (aniName != constants.animations.attack && aniName != constants.animations.die) { return; } let row = null; if (roleType == constants.roleItemTypes.player) { row = designManager.instance.getRowById(constants.tableName.role, roleId); } else { row = designManager.instance.getRowById(constants.tableName.monster, roleId); } if (!row) { console.log("找不到对应的配置表数据", roleType, roleId, aniName); return; } let audioName = row[aniName]; if (!audioName) { if (roleType == constants.roleItemTypes.player && aniName == constants.animations.die) { // 玩家角色本身就没有配置死亡音效 return; } console.log("缺少音效配置", row, aniName); return; } if (roleType == constants.roleItemTypes.player) { let delay = -1;; if (aniName == constants.animations.attack) { switch (roleId) { case constants.roleIds.gong_jian_shou: delay = 0.45; break; default: break; } } if (delay == 0) { this.playEffect(audioName); } else if (delay > 0) { component.scheduleOnce(() => { this.playEffect(audioName); }, delay); } return; } if (roleType == constants.roleItemTypes.monster) { let delay = -1;; if (aniName == constants.animations.attack) { switch (roleId) { case constants.monsterIds.xie_zi: delay = 0.15; break; default: delay = 0; break; } } else if (aniName == constants.animations.die) { delay = 0; } if (delay == 0) { this.playEffect(audioName); } else if (delay > 0) { component.scheduleOnce(() => { this.playEffect(audioName); }, delay); } return; } } onButtonClick(component: Component, btnNode: Node) { let name = btnNode.name; let needPlayBtnAudio = true; if (name.length > 5 && name.substring(0, 5) == "role_") { needPlayBtnAudio = false; let delay = -1; let audioName = ""; switch (name) { case "role_box": delay = 1.8; if (btnNode["isBoss"]) { delay = 1.5; } audioName = constants.audioNames.baoxiang; break; case "role_arrow": delay = 0.1; audioName = constants.audioNames.hit_gong; break; case "role_equip": delay = 0.6; audioName = constants.audioNames.equip; break; case "role_gunshi": delay = 0; audioName = constants.audioNames.gunshi; break; case "role_dapao": delay = 0.3; audioName = constants.audioNames.pao; break; default: break; } if (delay == 0) { this.playEffect(audioName); } else if (delay > 0) { component.scheduleOnce(() => { this.playEffect(audioName); }, delay); } } if (needPlayBtnAudio) { this.playEffect(constants.audioNames.button); } } // 切换静音状态 switchIsMute() { if (this.musicVolume > 0) { // 切换为静音 this.setMusicVolume(0); this.audioSource.pause(); } else { // 打开声音 this.setMusicVolume(1); this.playMusic(this.musicName); } } getMusiceVolume(): number { return this.musicVolume; } setMusicVolume(val: number) { val = Math.round(val * 1000) / 1000; this.musicVolume = val; localStorageManager.instance.setMusicVolume(val); this.audioSource.volume = val; } playMusic(musicName: string) { if (!musicName) { return; } this.musicName = musicName; let clip = this.getAudioClip(musicName); if (clip) { this.audioSource.clip = clip; this.audioSource.play(); } } playEffect(effectName: string) { if (!effectName) { return; } let clip = this.getAudioClip(effectName); if (clip) { this.audioSource.playOneShot(clip); } } getAudioClip(name: string): any { if (this.musicVolume <= 0) { // 静音状态 return null; } let clip = this.audioClips[name]; if (!clip) { console.log("AudioClip not load:" + name); return null; } return clip; } }