mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-06-23 19:22:47 +08:00
新增与优化核心模块 🎯 GUI 交互增强 ButtonInterceptor:按钮点击事件劫持器 劫持 EventHandler.emitEvents / ButtonSimple.onClick 核心方法 支持多类型按钮音效配置注册,按按钮类型自动匹配音效 单例模式管理,支持全局动态激活 / 停用拦截功能 🧩 对象池管理系统 GameNodePool:通用游戏节点对象池管理器 基于预制体(Prefab)UUID 实现池化隔离管理 支持特效、UI 元素等高频对象预加载、复用与回收 全局单例访问,统一管控对象创建 / 销毁逻辑 🔊 音频管理系统 AudioManager:音频核心管理器 独立背景音乐(AudioMusic)+ 音效池(AudioEffectPool)双模块 支持全局音量控制、播放状态恢复与场景切换管理 AudioEffect:基础音效播放器 AudioEnum:音频类型、音效分类枚举定义 📦 资源加载系统 ResLoader:资源加载核心控制器 资源包(Bundle)统一管理 + 并发加载限流控制 远程资源本地缓存、自动释放与内存优化 完整加载进度回调 + 错误异常处理机制 ResTypes:资源类型规范定义 ResErrors:资源加载错误码与异常处理 ResUtils:资源路径、格式转换等工具方法 🧱 模块化组件系统 GameComponent:游戏显示对象组件基类 集成资源自动引用计数(RC)管理 模块化部件(GamePartRegistry)插拔式架构 内置音频、按钮、事件、键盘、节点、对象池、资源七大部件 部件封装: GamePartAudio:音频功能部件 GamePartButton:按钮交互部件 GamePartEvent:全局事件部件 GamePartKeyboard:键盘输入部件 GamePartNode:节点操作部件 GamePartNodePool:对象池调用部件 GamePartRes:资源加载部件 📝 类型定义完善 新增 IAudio 音频数据与播放参数接口 统一全模块 TS 类型定义与导出规范 优化接口注释与类型约束,提升开发体验
121 lines
3.5 KiB
TypeScript
121 lines
3.5 KiB
TypeScript
import type { AudioClip } from 'cc';
|
||
import { Node } from 'cc';
|
||
import type { IAudioData, IAudioParams } from './IAudio';
|
||
import { AudioEffect } from './AudioEffect';
|
||
import { AudioEffectType } from './AudioEnum';
|
||
|
||
/**
|
||
* 背景音乐
|
||
* 1、播放一个新背景音乐时,先停止正在播放的背景资源,最后播放新的背景音乐
|
||
* 2、背景音乐循环播放时,不会触发播放完成事件
|
||
* 3、不负责加载资源,也不负责释放资源,资源加载与引用计数完全由外部(GameResModule + ResAutoTracker)管理
|
||
*/
|
||
export class AudioMusic extends Node {
|
||
/** 音效配置数据 */
|
||
private data: { [node: string]: IAudioData } = null!;
|
||
|
||
private _progress = 0;
|
||
private _ae: AudioEffect = null!;
|
||
|
||
/**
|
||
* 音效开关
|
||
* @param type 音效类型
|
||
* @returns 音效开关
|
||
*/
|
||
getSwitch() {
|
||
return this.data[AudioEffectType.Music].switch;
|
||
}
|
||
/**
|
||
* 音效音量设置
|
||
* @param type 音效类型
|
||
* @param value 音效开关
|
||
*/
|
||
setSwitch(value: boolean) {
|
||
this.data[AudioEffectType.Music].switch = value;
|
||
if (!value) this.stop();
|
||
}
|
||
|
||
/**
|
||
* 音效音量获取
|
||
* @param type 音效类型
|
||
* @returns 音效音量
|
||
*/
|
||
getVolume(): number {
|
||
return this.data[AudioEffectType.Music].volume;
|
||
}
|
||
/**
|
||
* 音效音量设置
|
||
* @param value 音效音量
|
||
*/
|
||
setVolume(value: number) {
|
||
this.data[AudioEffectType.Music].volume = value;
|
||
this._ae.volume = value;
|
||
}
|
||
|
||
/** 获取音乐播放进度 */
|
||
get progress(): number {
|
||
if (this._ae.duration > 0) this._progress = this._ae.currentTime / this._ae.duration;
|
||
return this._progress;
|
||
}
|
||
/**
|
||
* 设置音乐当前播放进度
|
||
* @param value 进度百分比0到1之间
|
||
*/
|
||
set progress(value: number) {
|
||
this._progress = value;
|
||
this._ae.currentTime = value * this._ae.duration;
|
||
}
|
||
|
||
constructor() {
|
||
super();
|
||
this.name = 'AudioMusic';
|
||
this._ae = this.addComponent(AudioEffect);
|
||
this._ae.onComplete = this.onAudioEffectPlayComplete.bind(this);
|
||
}
|
||
|
||
/** 音效播放完成 */
|
||
private onAudioEffectPlayComplete(ae: AudioEffect) {
|
||
ae.params && ae.params.onPlayComplete && ae.params.onPlayComplete(ae);
|
||
}
|
||
|
||
/**
|
||
* 播放音乐
|
||
* @param clip AudioClip 实例
|
||
* @param params 背景音乐资源播放参数
|
||
*/
|
||
play(clip: AudioClip, params?: IAudioParams) {
|
||
if (!this.getSwitch()) return;
|
||
|
||
if (this._ae.playing) this.stop();
|
||
|
||
this._ae.params = params!;
|
||
this._ae.clip = clip;
|
||
this._ae.loop = params?.loop ?? true;
|
||
this._ae.volume = params?.volume ?? this.getVolume();
|
||
this._ae.currentTime = 0;
|
||
this._ae.play();
|
||
}
|
||
|
||
/** 恢复当前暂停的音乐与音效播放 */
|
||
resume() {
|
||
if (!this._ae.playing && this.progress > 0) this._ae.play();
|
||
}
|
||
|
||
/** 暂停当前音乐与音效的播放 */
|
||
pause() {
|
||
if (this._ae.playing) this._ae.pause();
|
||
}
|
||
|
||
/** 停止当前音乐与音效的播放 */
|
||
stop(): void {
|
||
if (this._ae.playing) this._ae.stop();
|
||
}
|
||
|
||
/** 节点销毁时清理 */
|
||
onDestroy() {
|
||
this.stop();
|
||
this._ae = null!;
|
||
this.data = null!;
|
||
}
|
||
}
|