| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { _decorator, Component, Node } from 'cc';
- import { AudioManager } from './AudioManager';
- const { ccclass, property } = _decorator;
- /**
- * 敌人音效管理器
- * 负责播放敌人相关的各种音效
- */
- @ccclass('EnemyAudios')
- export class EnemyAudios extends Component {
- // 单例实例
- private static _instance: EnemyAudios = null;
-
- // 音频管理器引用
- @property({ type: AudioManager, tooltip: '音频管理器组件引用' })
- public audioManager: AudioManager = null;
-
- onLoad() {
- console.log('[EnemyAudios] onLoad 被调用,节点路径:', this.getNodePath());
-
- // 设置单例
- if (EnemyAudios._instance === null) {
- EnemyAudios._instance = this;
- console.log('[EnemyAudios] 单例实例已设置');
- } else {
- console.warn('[EnemyAudios] 单例实例已存在,销毁重复节点');
- this.node.destroy();
- return;
- }
-
- // 检查音频管理器引用
- if (!this.audioManager) {
- console.error('[EnemyAudios] AudioManager组件未绑定,请在编辑器中将AudioManager节点拖拽到audioManager属性上');
- } else {
- console.log('[EnemyAudios] AudioManager组件绑定成功');
- }
- }
-
- /**
- * 获取单例实例
- */
- public static getInstance(): EnemyAudios {
- if (!EnemyAudios._instance) {
- console.error('[EnemyAudios] 单例实例未初始化,请确保EnemyAudios组件已挂载到场景中');
- }
- return EnemyAudios._instance;
- }
-
- /**
- * 获取节点路径(用于调试)
- */
- private getNodePath(): string {
- let path = this.node.name;
- let current = this.node;
-
- while (current.parent) {
- current = current.parent;
- path = current.name + '/' + path;
- }
-
- return path;
- }
-
- /**
- * 播放敌人攻击音效
- * @param enemyConfig 敌人配置数据
- */
- public playAttackSound(enemyConfig: any): void {
- if (!this.audioManager || !enemyConfig) return;
-
- // 优先使用audio配置中的attack_sound
- let soundPath = enemyConfig.audio?.attack_sound;
-
- // 如果audio配置中没有或为"Null",则使用audioConfig中的attackSound
- if (!soundPath || soundPath === 'Null' || soundPath === 'nan') {
- soundPath = enemyConfig.audioConfig?.attackSound;
- }
-
- // 如果还是没有有效音效路径,使用默认音效
- if (!soundPath || soundPath === 'Null' || soundPath === 'nan' || soundPath === '') {
- soundPath = 'data/弹球音效/hammer1'; // 默认攻击音效
- }
- console.log(`[EnemyAudios] 播放攻击音效: ${soundPath}`);
- this.audioManager.playEnemySound(soundPath);
- }
-
- /**
- * 播放敌人死亡音效
- * @param enemyConfig 敌人配置数据
- */
- public playDeathSound(enemyConfig: any): void {
- if (!this.audioManager || !enemyConfig) return;
-
- // 优先使用audio配置中的death_sound
- let soundPath = enemyConfig.audio?.death_sound;
-
- // 如果audio配置中没有或为"Null",则使用audioConfig中的deathSound
- if (!soundPath || soundPath === 'Null' || soundPath === 'nan') {
- soundPath = enemyConfig.audioConfig?.deathSound;
- }
-
- // 如果还是没有有效音效路径,使用默认音效
- if (!soundPath || soundPath === 'Null' || soundPath === 'nan' || soundPath === '') {
- soundPath = 'data/弹球音效/normal zombie die 1'; // 默认死亡音效
- }
- console.log(`[EnemyAudios] 播放死亡音效: ${soundPath}`);
- this.audioManager.playEnemySound(soundPath);
- }
-
- onDestroy() {
- if (EnemyAudios._instance === this) {
- EnemyAudios._instance = null;
- }
- }
- }
- /**
- * 敌人音效的静态接口
- * 提供便捷的全局访问方法
- */
- export class EnemyAudio {
- /**
- * 播放敌人攻击音效
- * @param enemyConfig 敌人配置数据
- */
- static playAttackSound(enemyConfig: any): void {
- console.log('[EnemyAudio] 静态方法 playAttackSound 被调用');
- const instance = EnemyAudios.getInstance();
- if (instance) {
- instance.playAttackSound(enemyConfig);
- } else {
- console.error('[EnemyAudio] EnemyAudios实例未找到,无法播放攻击音效');
- }
- }
- /**
- * 播放敌人死亡音效
- * @param enemyConfig 敌人配置数据
- */
- static playDeathSound(enemyConfig: any): void {
- console.log('[EnemyAudio] 静态方法 playDeathSound 被调用');
- const instance = EnemyAudios.getInstance();
- if (instance) {
- instance.playDeathSound(enemyConfig);
- } else {
- console.error('[EnemyAudio] EnemyAudios实例未找到,无法播放死亡音效');
- }
- }
- }
|