audioManager.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { AudioClip, AudioSource, Component, Node } from "cc";
  2. import { constants } from "../data/constants";
  3. import { designManager } from "./designManager";
  4. import { localStorageManager } from "./localStorageManager";
  5. import { resManager } from "./resManager";
  6. export class audioManager {
  7. private static _instance: audioManager;
  8. private constructor() { };
  9. public static get instance(): audioManager {
  10. if (!this._instance) {
  11. this._instance = new audioManager();
  12. }
  13. return this._instance;
  14. }
  15. audioSource: AudioSource;
  16. audioClips: any = {};
  17. musicVolume: number = 0;
  18. musicName: string = "";
  19. init(audioSource: AudioSource) {
  20. this.audioSource = audioSource;
  21. this.musicVolume = localStorageManager.instance.getMusicVolume();
  22. }
  23. loadAudio(onProgress?: Function, onComplete?: Function) {
  24. resManager.instance.loadAssetByBundleDir(constants.bundles.audio.name,
  25. "", AudioClip, onProgress, (assetArr) => {
  26. console.log("audioManager.loadAudio success");
  27. for (let i in assetArr) {
  28. let asset = assetArr[i];
  29. let name = asset.name;
  30. if (!name) {
  31. continue;
  32. }
  33. this.audioClips[name] = asset;
  34. }
  35. if (onComplete) {
  36. onComplete();
  37. }
  38. });
  39. }
  40. onRolePlayAnimation(component: Component, roleType: number, roleId: number, aniName: string) {
  41. if (roleType != constants.roleItemTypes.player && roleType != constants.roleItemTypes.monster) {
  42. return;
  43. }
  44. if (aniName != constants.animations.attack && aniName != constants.animations.die) {
  45. return;
  46. }
  47. let row = null;
  48. if (roleType == constants.roleItemTypes.player) {
  49. row = designManager.instance.getRowById(constants.tableName.role, roleId);
  50. } else {
  51. row = designManager.instance.getRowById(constants.tableName.monster, roleId);
  52. }
  53. if (!row) {
  54. console.log("找不到对应的配置表数据", roleType, roleId, aniName);
  55. return;
  56. }
  57. let audioName = row[aniName];
  58. if (!audioName) {
  59. if (roleType == constants.roleItemTypes.player && aniName == constants.animations.die) {
  60. // 玩家角色本身就没有配置死亡音效
  61. return;
  62. }
  63. console.log("缺少音效配置", row, aniName);
  64. return;
  65. }
  66. if (roleType == constants.roleItemTypes.player) {
  67. let delay = -1;;
  68. if (aniName == constants.animations.attack) {
  69. switch (roleId) {
  70. case constants.roleIds.gong_jian_shou:
  71. delay = 0.45;
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. if (delay == 0) {
  78. this.playEffect(audioName);
  79. } else if (delay > 0) {
  80. component.scheduleOnce(() => {
  81. this.playEffect(audioName);
  82. }, delay);
  83. }
  84. return;
  85. }
  86. if (roleType == constants.roleItemTypes.monster) {
  87. let delay = -1;;
  88. if (aniName == constants.animations.attack) {
  89. switch (roleId) {
  90. case constants.monsterIds.xie_zi:
  91. delay = 0.15;
  92. break;
  93. default:
  94. delay = 0;
  95. break;
  96. }
  97. } else if (aniName == constants.animations.die) {
  98. delay = 0;
  99. }
  100. if (delay == 0) {
  101. this.playEffect(audioName);
  102. } else if (delay > 0) {
  103. component.scheduleOnce(() => {
  104. this.playEffect(audioName);
  105. }, delay);
  106. }
  107. return;
  108. }
  109. }
  110. onButtonClick(component: Component, btnNode: Node) {
  111. let name = btnNode.name;
  112. let needPlayBtnAudio = true;
  113. if (name.length > 5 && name.substring(0, 5) == "role_") {
  114. needPlayBtnAudio = false;
  115. let delay = -1;
  116. let audioName = "";
  117. switch (name) {
  118. case "role_box":
  119. delay = 1.8;
  120. if (btnNode["isBoss"]) {
  121. delay = 1.5;
  122. }
  123. audioName = constants.audioNames.baoxiang;
  124. break;
  125. case "role_arrow":
  126. delay = 0.1;
  127. audioName = constants.audioNames.hit_gong;
  128. break;
  129. case "role_equip":
  130. delay = 0.6;
  131. audioName = constants.audioNames.equip;
  132. break;
  133. case "role_gunshi":
  134. delay = 0;
  135. audioName = constants.audioNames.gunshi;
  136. break;
  137. case "role_dapao":
  138. delay = 0.3;
  139. audioName = constants.audioNames.pao;
  140. break;
  141. default:
  142. break;
  143. }
  144. if (delay == 0) {
  145. this.playEffect(audioName);
  146. } else if (delay > 0) {
  147. component.scheduleOnce(() => {
  148. this.playEffect(audioName);
  149. }, delay);
  150. }
  151. }
  152. if (needPlayBtnAudio) {
  153. this.playEffect(constants.audioNames.button);
  154. }
  155. }
  156. // 切换静音状态
  157. switchIsMute() {
  158. if (this.musicVolume > 0) {
  159. // 切换为静音
  160. this.setMusicVolume(0);
  161. this.audioSource.pause();
  162. } else {
  163. // 打开声音
  164. this.setMusicVolume(1);
  165. this.playMusic(this.musicName);
  166. }
  167. }
  168. getMusiceVolume(): number {
  169. return this.musicVolume;
  170. }
  171. setMusicVolume(val: number) {
  172. val = Math.round(val * 1000) / 1000;
  173. this.musicVolume = val;
  174. localStorageManager.instance.setMusicVolume(val);
  175. this.audioSource.volume = val;
  176. }
  177. playMusic(musicName: string) {
  178. if (!musicName) {
  179. return;
  180. }
  181. this.musicName = musicName;
  182. let clip = this.getAudioClip(musicName);
  183. if (clip) {
  184. this.audioSource.clip = clip;
  185. this.audioSource.play();
  186. }
  187. }
  188. playEffect(effectName: string) {
  189. if (!effectName) {
  190. return;
  191. }
  192. let clip = this.getAudioClip(effectName);
  193. if (clip) {
  194. this.audioSource.playOneShot(clip);
  195. }
  196. }
  197. getAudioClip(name: string): any {
  198. if (this.musicVolume <= 0) {
  199. // 静音状态
  200. return null;
  201. }
  202. let clip = this.audioClips[name];
  203. if (!clip) {
  204. console.log("AudioClip not load:" + name);
  205. return null;
  206. }
  207. return clip;
  208. }
  209. }