| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- import { _decorator, Component } from 'cc';
- const { ccclass, property } = _decorator;
- /**
- * 音频类型枚举
- */
- export enum AudioType {
- MUSIC = 'music', // 背景音乐
- UI_SOUND = 'ui_sound', // UI音效
- ENEMY_SOUND = 'enemy_sound', // 敌人音效
- ENVIRONMENT_SOUND = 'environment_sound', // 环境音效
- WEAPON_SOUND = 'weapon_sound' // 武器音效
- }
- /**
- * 音频配置接口
- */
- export interface IAudioConfig {
- /** 音频文件路径 */
- path: string;
- /** 音频类型 */
- type: AudioType;
- /** 默认音量 (0-1) */
- volume?: number;
- /** 是否循环播放 */
- loop?: boolean;
- /** 音频描述 */
- description?: string;
- }
- /**
- * 音频分类配置
- */
- export interface IAudioCategoryConfig {
- /** 分类名称 */
- name: string;
- /** 分类类型 */
- type: AudioType;
- /** 分类默认音量 */
- defaultVolume: number;
- /** 分类描述 */
- description: string;
- /** 该分类下的音频列表 */
- audios: IAudioConfig[];
- }
- /**
- * 音频系统配置管理器
- */
- @ccclass('AudioConfig')
- export class AudioConfig extends Component {
-
- /**
- * 音频分类配置
- */
- public static readonly AUDIO_CATEGORIES: IAudioCategoryConfig[] = [
- {
- name: '背景音乐',
- type: AudioType.MUSIC,
- defaultVolume: 0.6,
- description: '游戏背景音乐,包括主菜单、游戏内、胜利失败等场景音乐',
- audios: [
- {
- path: 'audio/music/main_menu',
- type: AudioType.MUSIC,
- volume: 0.6,
- loop: true,
- description: '主菜单背景音乐'
- },
- {
- path: 'audio/music/game_background',
- type: AudioType.MUSIC,
- volume: 0.5,
- loop: true,
- description: '游戏内背景音乐'
- },
- {
- path: 'audio/music/victory',
- type: AudioType.MUSIC,
- volume: 0.7,
- loop: false,
- description: '胜利音乐'
- },
- {
- path: 'audio/music/defeat',
- type: AudioType.MUSIC,
- volume: 0.7,
- loop: false,
- description: '失败音乐'
- }
- ]
- },
- {
- name: 'UI音效',
- type: AudioType.UI_SOUND,
- defaultVolume: 0.8,
- description: '用户界面相关音效,包括按钮点击、菜单切换等',
- audios: [
- {
- path: 'data/弹球音效/ui play',
- type: AudioType.UI_SOUND,
- volume: 0.8,
- loop: false,
- description: '按钮点击音效'
- },
- {
- path: 'audio/ui/button_hover',
- type: AudioType.UI_SOUND,
- volume: 0.6,
- loop: false,
- description: '按钮悬停音效'
- },
- {
- path: 'audio/ui/menu_open',
- type: AudioType.UI_SOUND,
- volume: 0.7,
- loop: false,
- description: '菜单打开音效'
- },
- {
- path: 'audio/ui/menu_close',
- type: AudioType.UI_SOUND,
- volume: 0.7,
- loop: false,
- description: '菜单关闭音效'
- }
- ]
- },
- {
- name: '敌人音效',
- type: AudioType.ENEMY_SOUND,
- defaultVolume: 0.7,
- description: '敌人相关音效,包括攻击、死亡、受击、行走等',
- audios: [
- // 敌人音效由 EnemyAudios 系统动态管理,基于 enemies.json 配置
- // 这里提供一些通用的默认音效
- {
- path: 'audio/enemy/default_attack',
- type: AudioType.ENEMY_SOUND,
- volume: 0.7,
- loop: false,
- description: '默认攻击音效'
- },
- {
- path: 'audio/enemy/default_death',
- type: AudioType.ENEMY_SOUND,
- volume: 0.8,
- loop: false,
- description: '默认死亡音效'
- },
- {
- path: 'audio/enemy/default_hit',
- type: AudioType.ENEMY_SOUND,
- volume: 0.6,
- loop: false,
- description: '默认受击音效'
- },
- {
- path: 'audio/enemy/default_walk',
- type: AudioType.ENEMY_SOUND,
- volume: 0.4,
- loop: true,
- description: '默认行走音效'
- }
- ]
- },
- {
- name: '环境音效',
- type: AudioType.ENVIRONMENT_SOUND,
- defaultVolume: 0.5,
- description: '环境相关音效,包括风声、水声、爆炸等',
- audios: [
- {
- path: 'audio/environment/wind',
- type: AudioType.ENVIRONMENT_SOUND,
- volume: 0.3,
- loop: true,
- description: '风声'
- },
- {
- path: 'audio/environment/explosion',
- type: AudioType.ENVIRONMENT_SOUND,
- volume: 0.8,
- loop: false,
- description: '爆炸音效'
- },
- {
- path: 'audio/environment/footsteps',
- type: AudioType.ENVIRONMENT_SOUND,
- volume: 0.4,
- loop: false,
- description: '脚步声'
- }
- ]
- },
- {
- name: '武器音效',
- type: AudioType.WEAPON_SOUND,
- defaultVolume: 0.8,
- description: '武器相关音效,包括射击、装弹、切换武器等',
- audios: [
- {
- path: 'audio/weapon/gun_shot',
- type: AudioType.WEAPON_SOUND,
- volume: 0.8,
- loop: false,
- description: '枪声'
- },
- {
- path: 'audio/weapon/reload',
- type: AudioType.WEAPON_SOUND,
- volume: 0.6,
- loop: false,
- description: '装弹音效'
- },
- {
- path: 'audio/weapon/weapon_switch',
- type: AudioType.WEAPON_SOUND,
- volume: 0.7,
- loop: false,
- description: '切换武器音效'
- }
- ]
- }
- ];
-
- /**
- * 根据音频类型获取分类配置
- * @param type 音频类型
- * @returns 分类配置
- */
- public static getCategoryConfig(type: AudioType): IAudioCategoryConfig | null {
- return this.AUDIO_CATEGORIES.find(category => category.type === type) || null;
- }
-
- /**
- * 根据路径获取音频配置
- * @param path 音频路径
- * @returns 音频配置
- */
- public static getAudioConfig(path: string): IAudioConfig | null {
- for (const category of this.AUDIO_CATEGORIES) {
- const audio = category.audios.find(audio => audio.path === path);
- if (audio) {
- return audio;
- }
- }
- return null;
- }
-
- /**
- * 获取指定类型的所有音频配置
- * @param type 音频类型
- * @returns 音频配置数组
- */
- public static getAudiosByType(type: AudioType): IAudioConfig[] {
- const category = this.getCategoryConfig(type);
- return category ? category.audios : [];
- }
-
- /**
- * 获取默认音量
- * @param type 音频类型
- * @returns 默认音量
- */
- public static getDefaultVolume(type: AudioType): number {
- const category = this.getCategoryConfig(type);
- return category ? category.defaultVolume : 0.5;
- }
-
- /**
- * 验证音频路径是否存在于配置中
- * @param path 音频路径
- * @returns 是否存在
- */
- public static isValidAudioPath(path: string): boolean {
- return this.getAudioConfig(path) !== null;
- }
-
- /**
- * 获取所有音频类型
- * @returns 音频类型数组
- */
- public static getAllAudioTypes(): AudioType[] {
- return [
- AudioType.MUSIC,
- AudioType.UI_SOUND,
- AudioType.ENEMY_SOUND,
- AudioType.ENVIRONMENT_SOUND,
- AudioType.WEAPON_SOUND
- ];
- }
-
- /**
- * 获取音频系统统计信息
- * @returns 统计信息
- */
- public static getAudioStats(): {
- totalCategories: number;
- totalAudios: number;
- audiosByType: Record<AudioType, number>;
- } {
- const audiosByType = {} as Record<AudioType, number>;
- let totalAudios = 0;
-
- for (const category of this.AUDIO_CATEGORIES) {
- audiosByType[category.type] = category.audios.length;
- totalAudios += category.audios.length;
- }
-
- return {
- totalCategories: this.AUDIO_CATEGORIES.length,
- totalAudios,
- audiosByType
- };
- }
- }
- /**
- * 音频配置的静态访问接口
- */
- export class AudioConfigHelper {
- /**
- * 获取音乐配置
- */
- static get music() {
- return AudioConfig.getAudiosByType(AudioType.MUSIC);
- }
-
- /**
- * 获取UI音效配置
- */
- static get uiSounds() {
- return AudioConfig.getAudiosByType(AudioType.UI_SOUND);
- }
-
- /**
- * 获取敌人音效配置
- */
- static get enemySounds() {
- return AudioConfig.getAudiosByType(AudioType.ENEMY_SOUND);
- }
-
- /**
- * 获取环境音效配置
- */
- static get environmentSounds() {
- return AudioConfig.getAudiosByType(AudioType.ENVIRONMENT_SOUND);
- }
-
- /**
- * 获取武器音效配置
- */
- static get weaponSounds() {
- return AudioConfig.getAudiosByType(AudioType.WEAPON_SOUND);
- }
- }
|