AudioConfig.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { _decorator, Component } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 音频类型枚举
  5. */
  6. export enum AudioType {
  7. MUSIC = 'music', // 背景音乐
  8. UI_SOUND = 'ui_sound', // UI音效
  9. ENEMY_SOUND = 'enemy_sound', // 敌人音效
  10. ENVIRONMENT_SOUND = 'environment_sound', // 环境音效
  11. WEAPON_SOUND = 'weapon_sound' // 武器音效
  12. }
  13. /**
  14. * 音频配置接口
  15. */
  16. export interface IAudioConfig {
  17. /** 音频文件路径 */
  18. path: string;
  19. /** 音频类型 */
  20. type: AudioType;
  21. /** 默认音量 (0-1) */
  22. volume?: number;
  23. /** 是否循环播放 */
  24. loop?: boolean;
  25. /** 音频描述 */
  26. description?: string;
  27. }
  28. /**
  29. * 音频分类配置
  30. */
  31. export interface IAudioCategoryConfig {
  32. /** 分类名称 */
  33. name: string;
  34. /** 分类类型 */
  35. type: AudioType;
  36. /** 分类默认音量 */
  37. defaultVolume: number;
  38. /** 分类描述 */
  39. description: string;
  40. /** 该分类下的音频列表 */
  41. audios: IAudioConfig[];
  42. }
  43. /**
  44. * 音频系统配置管理器
  45. */
  46. @ccclass('AudioConfig')
  47. export class AudioConfig extends Component {
  48. /**
  49. * 音频分类配置
  50. */
  51. public static readonly AUDIO_CATEGORIES: IAudioCategoryConfig[] = [
  52. {
  53. name: '背景音乐',
  54. type: AudioType.MUSIC,
  55. defaultVolume: 0.6,
  56. description: '游戏背景音乐,包括主菜单、游戏内、胜利失败等场景音乐',
  57. audios: [
  58. {
  59. path: 'audio/music/main_menu',
  60. type: AudioType.MUSIC,
  61. volume: 0.6,
  62. loop: true,
  63. description: '主菜单背景音乐'
  64. },
  65. {
  66. path: 'audio/music/game_background',
  67. type: AudioType.MUSIC,
  68. volume: 0.5,
  69. loop: true,
  70. description: '游戏内背景音乐'
  71. },
  72. {
  73. path: 'audio/music/victory',
  74. type: AudioType.MUSIC,
  75. volume: 0.7,
  76. loop: false,
  77. description: '胜利音乐'
  78. },
  79. {
  80. path: 'audio/music/defeat',
  81. type: AudioType.MUSIC,
  82. volume: 0.7,
  83. loop: false,
  84. description: '失败音乐'
  85. }
  86. ]
  87. },
  88. {
  89. name: 'UI音效',
  90. type: AudioType.UI_SOUND,
  91. defaultVolume: 0.8,
  92. description: '用户界面相关音效,包括按钮点击、菜单切换等',
  93. audios: [
  94. {
  95. path: 'data/弹球音效/ui play',
  96. type: AudioType.UI_SOUND,
  97. volume: 0.8,
  98. loop: false,
  99. description: '按钮点击音效'
  100. },
  101. {
  102. path: 'audio/ui/button_hover',
  103. type: AudioType.UI_SOUND,
  104. volume: 0.6,
  105. loop: false,
  106. description: '按钮悬停音效'
  107. },
  108. {
  109. path: 'audio/ui/menu_open',
  110. type: AudioType.UI_SOUND,
  111. volume: 0.7,
  112. loop: false,
  113. description: '菜单打开音效'
  114. },
  115. {
  116. path: 'audio/ui/menu_close',
  117. type: AudioType.UI_SOUND,
  118. volume: 0.7,
  119. loop: false,
  120. description: '菜单关闭音效'
  121. }
  122. ]
  123. },
  124. {
  125. name: '敌人音效',
  126. type: AudioType.ENEMY_SOUND,
  127. defaultVolume: 0.7,
  128. description: '敌人相关音效,包括攻击、死亡、受击、行走等',
  129. audios: [
  130. // 敌人音效由 EnemyAudios 系统动态管理,基于 enemies.json 配置
  131. // 这里提供一些通用的默认音效
  132. {
  133. path: 'audio/enemy/default_attack',
  134. type: AudioType.ENEMY_SOUND,
  135. volume: 0.7,
  136. loop: false,
  137. description: '默认攻击音效'
  138. },
  139. {
  140. path: 'audio/enemy/default_death',
  141. type: AudioType.ENEMY_SOUND,
  142. volume: 0.8,
  143. loop: false,
  144. description: '默认死亡音效'
  145. },
  146. {
  147. path: 'audio/enemy/default_hit',
  148. type: AudioType.ENEMY_SOUND,
  149. volume: 0.6,
  150. loop: false,
  151. description: '默认受击音效'
  152. },
  153. {
  154. path: 'audio/enemy/default_walk',
  155. type: AudioType.ENEMY_SOUND,
  156. volume: 0.4,
  157. loop: true,
  158. description: '默认行走音效'
  159. }
  160. ]
  161. },
  162. {
  163. name: '环境音效',
  164. type: AudioType.ENVIRONMENT_SOUND,
  165. defaultVolume: 0.5,
  166. description: '环境相关音效,包括风声、水声、爆炸等',
  167. audios: [
  168. {
  169. path: 'audio/environment/wind',
  170. type: AudioType.ENVIRONMENT_SOUND,
  171. volume: 0.3,
  172. loop: true,
  173. description: '风声'
  174. },
  175. {
  176. path: 'audio/environment/explosion',
  177. type: AudioType.ENVIRONMENT_SOUND,
  178. volume: 0.8,
  179. loop: false,
  180. description: '爆炸音效'
  181. },
  182. {
  183. path: 'audio/environment/footsteps',
  184. type: AudioType.ENVIRONMENT_SOUND,
  185. volume: 0.4,
  186. loop: false,
  187. description: '脚步声'
  188. }
  189. ]
  190. },
  191. {
  192. name: '武器音效',
  193. type: AudioType.WEAPON_SOUND,
  194. defaultVolume: 0.8,
  195. description: '武器相关音效,包括射击、装弹、切换武器等',
  196. audios: [
  197. {
  198. path: 'audio/weapon/gun_shot',
  199. type: AudioType.WEAPON_SOUND,
  200. volume: 0.8,
  201. loop: false,
  202. description: '枪声'
  203. },
  204. {
  205. path: 'audio/weapon/reload',
  206. type: AudioType.WEAPON_SOUND,
  207. volume: 0.6,
  208. loop: false,
  209. description: '装弹音效'
  210. },
  211. {
  212. path: 'audio/weapon/weapon_switch',
  213. type: AudioType.WEAPON_SOUND,
  214. volume: 0.7,
  215. loop: false,
  216. description: '切换武器音效'
  217. }
  218. ]
  219. }
  220. ];
  221. /**
  222. * 根据音频类型获取分类配置
  223. * @param type 音频类型
  224. * @returns 分类配置
  225. */
  226. public static getCategoryConfig(type: AudioType): IAudioCategoryConfig | null {
  227. return this.AUDIO_CATEGORIES.find(category => category.type === type) || null;
  228. }
  229. /**
  230. * 根据路径获取音频配置
  231. * @param path 音频路径
  232. * @returns 音频配置
  233. */
  234. public static getAudioConfig(path: string): IAudioConfig | null {
  235. for (const category of this.AUDIO_CATEGORIES) {
  236. const audio = category.audios.find(audio => audio.path === path);
  237. if (audio) {
  238. return audio;
  239. }
  240. }
  241. return null;
  242. }
  243. /**
  244. * 获取指定类型的所有音频配置
  245. * @param type 音频类型
  246. * @returns 音频配置数组
  247. */
  248. public static getAudiosByType(type: AudioType): IAudioConfig[] {
  249. const category = this.getCategoryConfig(type);
  250. return category ? category.audios : [];
  251. }
  252. /**
  253. * 获取默认音量
  254. * @param type 音频类型
  255. * @returns 默认音量
  256. */
  257. public static getDefaultVolume(type: AudioType): number {
  258. const category = this.getCategoryConfig(type);
  259. return category ? category.defaultVolume : 0.5;
  260. }
  261. /**
  262. * 验证音频路径是否存在于配置中
  263. * @param path 音频路径
  264. * @returns 是否存在
  265. */
  266. public static isValidAudioPath(path: string): boolean {
  267. return this.getAudioConfig(path) !== null;
  268. }
  269. /**
  270. * 获取所有音频类型
  271. * @returns 音频类型数组
  272. */
  273. public static getAllAudioTypes(): AudioType[] {
  274. return [
  275. AudioType.MUSIC,
  276. AudioType.UI_SOUND,
  277. AudioType.ENEMY_SOUND,
  278. AudioType.ENVIRONMENT_SOUND,
  279. AudioType.WEAPON_SOUND
  280. ];
  281. }
  282. /**
  283. * 获取音频系统统计信息
  284. * @returns 统计信息
  285. */
  286. public static getAudioStats(): {
  287. totalCategories: number;
  288. totalAudios: number;
  289. audiosByType: Record<AudioType, number>;
  290. } {
  291. const audiosByType = {} as Record<AudioType, number>;
  292. let totalAudios = 0;
  293. for (const category of this.AUDIO_CATEGORIES) {
  294. audiosByType[category.type] = category.audios.length;
  295. totalAudios += category.audios.length;
  296. }
  297. return {
  298. totalCategories: this.AUDIO_CATEGORIES.length,
  299. totalAudios,
  300. audiosByType
  301. };
  302. }
  303. }
  304. /**
  305. * 音频配置的静态访问接口
  306. */
  307. export class AudioConfigHelper {
  308. /**
  309. * 获取音乐配置
  310. */
  311. static get music() {
  312. return AudioConfig.getAudiosByType(AudioType.MUSIC);
  313. }
  314. /**
  315. * 获取UI音效配置
  316. */
  317. static get uiSounds() {
  318. return AudioConfig.getAudiosByType(AudioType.UI_SOUND);
  319. }
  320. /**
  321. * 获取敌人音效配置
  322. */
  323. static get enemySounds() {
  324. return AudioConfig.getAudiosByType(AudioType.ENEMY_SOUND);
  325. }
  326. /**
  327. * 获取环境音效配置
  328. */
  329. static get environmentSounds() {
  330. return AudioConfig.getAudiosByType(AudioType.ENVIRONMENT_SOUND);
  331. }
  332. /**
  333. * 获取武器音效配置
  334. */
  335. static get weaponSounds() {
  336. return AudioConfig.getAudiosByType(AudioType.WEAPON_SOUND);
  337. }
  338. }