EnemyAudios.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { _decorator, Component, Node } from 'cc';
  2. import { AudioManager } from './AudioManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 敌人音效管理器
  6. * 负责播放敌人相关的各种音效
  7. */
  8. @ccclass('EnemyAudios')
  9. export class EnemyAudios extends Component {
  10. // 单例实例
  11. private static _instance: EnemyAudios = null;
  12. // 音频管理器引用
  13. @property({ type: AudioManager, tooltip: '音频管理器组件引用' })
  14. public audioManager: AudioManager = null;
  15. onLoad() {
  16. console.log('[EnemyAudios] onLoad 被调用,节点路径:', this.getNodePath());
  17. // 设置单例
  18. if (EnemyAudios._instance === null) {
  19. EnemyAudios._instance = this;
  20. console.log('[EnemyAudios] 单例实例已设置');
  21. } else {
  22. console.warn('[EnemyAudios] 单例实例已存在,销毁重复节点');
  23. this.node.destroy();
  24. return;
  25. }
  26. // 检查音频管理器引用
  27. if (!this.audioManager) {
  28. console.error('[EnemyAudios] AudioManager组件未绑定,请在编辑器中将AudioManager节点拖拽到audioManager属性上');
  29. } else {
  30. console.log('[EnemyAudios] AudioManager组件绑定成功');
  31. }
  32. }
  33. /**
  34. * 获取单例实例
  35. */
  36. public static getInstance(): EnemyAudios {
  37. if (!EnemyAudios._instance) {
  38. console.error('[EnemyAudios] 单例实例未初始化,请确保EnemyAudios组件已挂载到场景中');
  39. }
  40. return EnemyAudios._instance;
  41. }
  42. /**
  43. * 获取节点路径(用于调试)
  44. */
  45. private getNodePath(): string {
  46. let path = this.node.name;
  47. let current = this.node;
  48. while (current.parent) {
  49. current = current.parent;
  50. path = current.name + '/' + path;
  51. }
  52. return path;
  53. }
  54. /**
  55. * 播放敌人攻击音效
  56. * @param enemyConfig 敌人配置数据
  57. */
  58. public playAttackSound(enemyConfig: any): void {
  59. if (!this.audioManager || !enemyConfig) return;
  60. // 优先使用audio配置中的attack_sound
  61. let soundPath = enemyConfig.audio?.attack_sound;
  62. // 如果audio配置中没有或为"Null",则使用audioConfig中的attackSound
  63. if (!soundPath || soundPath === 'Null' || soundPath === 'nan') {
  64. soundPath = enemyConfig.audioConfig?.attackSound;
  65. }
  66. // 如果还是没有有效音效路径,使用默认音效
  67. if (!soundPath || soundPath === 'Null' || soundPath === 'nan' || soundPath === '') {
  68. soundPath = 'data/弹球音效/hammer1'; // 默认攻击音效
  69. }
  70. console.log(`[EnemyAudios] 播放攻击音效: ${soundPath}`);
  71. this.audioManager.playEnemySound(soundPath);
  72. }
  73. /**
  74. * 播放敌人死亡音效
  75. * @param enemyConfig 敌人配置数据
  76. */
  77. public playDeathSound(enemyConfig: any): void {
  78. if (!this.audioManager || !enemyConfig) return;
  79. // 优先使用audio配置中的death_sound
  80. let soundPath = enemyConfig.audio?.death_sound;
  81. // 如果audio配置中没有或为"Null",则使用audioConfig中的deathSound
  82. if (!soundPath || soundPath === 'Null' || soundPath === 'nan') {
  83. soundPath = enemyConfig.audioConfig?.deathSound;
  84. }
  85. // 如果还是没有有效音效路径,使用默认音效
  86. if (!soundPath || soundPath === 'Null' || soundPath === 'nan' || soundPath === '') {
  87. soundPath = 'data/弹球音效/normal zombie die 1'; // 默认死亡音效
  88. }
  89. console.log(`[EnemyAudios] 播放死亡音效: ${soundPath}`);
  90. this.audioManager.playEnemySound(soundPath);
  91. }
  92. onDestroy() {
  93. if (EnemyAudios._instance === this) {
  94. EnemyAudios._instance = null;
  95. }
  96. }
  97. }
  98. /**
  99. * 敌人音效的静态接口
  100. * 提供便捷的全局访问方法
  101. */
  102. export class EnemyAudio {
  103. /**
  104. * 播放敌人攻击音效
  105. * @param enemyConfig 敌人配置数据
  106. */
  107. static playAttackSound(enemyConfig: any): void {
  108. console.log('[EnemyAudio] 静态方法 playAttackSound 被调用');
  109. const instance = EnemyAudios.getInstance();
  110. if (instance) {
  111. instance.playAttackSound(enemyConfig);
  112. } else {
  113. console.error('[EnemyAudio] EnemyAudios实例未找到,无法播放攻击音效');
  114. }
  115. }
  116. /**
  117. * 播放敌人死亡音效
  118. * @param enemyConfig 敌人配置数据
  119. */
  120. static playDeathSound(enemyConfig: any): void {
  121. console.log('[EnemyAudio] 静态方法 playDeathSound 被调用');
  122. const instance = EnemyAudios.getInstance();
  123. if (instance) {
  124. instance.playDeathSound(enemyConfig);
  125. } else {
  126. console.error('[EnemyAudio] EnemyAudios实例未找到,无法播放死亡音效');
  127. }
  128. }
  129. }