mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-09-07 04:07:19 +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 类型定义与导出规范 优化接口注释与类型约束,提升开发体验
143 lines
4.6 KiB
TypeScript
143 lines
4.6 KiB
TypeScript
import type { CallbackObject, IRequestProtocol, NetData } from './NetInterface';
|
||
import type { NetConnectOptions, NetNode } from './NetNode';
|
||
|
||
/**
|
||
* 使用流程文档可参考、简化与服务器对接、使用新版API体验,可进入下面地址获取新版本,替换network目录中的内容
|
||
* https://store.cocos.com/app/detail/5877
|
||
*/
|
||
|
||
/*
|
||
* 网络节点管理类
|
||
*/
|
||
export class NetManager {
|
||
private static _instance: NetManager;
|
||
protected _channels: { [key: number]: NetNode } = {};
|
||
|
||
/** 网络管理单例对象 */
|
||
static getInstance(): NetManager {
|
||
if (!this._instance) {
|
||
this._instance = new NetManager();
|
||
}
|
||
return this._instance;
|
||
}
|
||
|
||
/**
|
||
* 添加网络节点
|
||
* @param node 网络节点
|
||
* @param channelId 通道编号
|
||
* @example
|
||
// 游戏服务器心跳协议
|
||
class GameProtocol extends NetProtocolPako {
|
||
// 自定义心跳协议
|
||
getHearbeat(): NetData {
|
||
return '{"action":"LoginAction","method":"heart","data":"null","callback":"LoginAction_heart"}';
|
||
}
|
||
}
|
||
|
||
var net = new NetNodeGame();
|
||
var ws = new WebSock(); // WebSocket 网络连接对象
|
||
var gp = new GameProtocol(); // 网络通讯协议对象
|
||
var gt = new NetGameTips() // 网络提示对象
|
||
net.init(ws, gp, gt);
|
||
NetManager.getInstance().setNetNode(net, NetChannelType.Game);
|
||
*/
|
||
setNetNode(node: NetNode, channelId = 0) {
|
||
this._channels[channelId] = node;
|
||
}
|
||
|
||
/** 移除Node */
|
||
removeNetNode(channelId: number) {
|
||
delete this._channels[channelId];
|
||
}
|
||
|
||
/**
|
||
* 网络节点连接服务器
|
||
* @param options 连接参数
|
||
* @param channelId 通道编号
|
||
* @example
|
||
var options = {
|
||
url: 'ws://127.0.0.1:3000',
|
||
autoReconnect: 0 // -1 永久重连,0不自动重连,其他正整数为自动重试次数
|
||
}
|
||
NetManager.getInstance().connect(options, NetChannelType.Game);
|
||
*/
|
||
connect(options: NetConnectOptions, channelId = 0): boolean {
|
||
if (this._channels[channelId]) {
|
||
return this._channels[channelId].connect(options);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/** 节点连接发送数据*/
|
||
send(buf: NetData, force = false, channelId = 0): number {
|
||
const node = this._channels[channelId];
|
||
if (node) {
|
||
return node!.send(buf, force);
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* 发起请求,并在在结果返回时调用指定好的回调函数
|
||
* @param reqProtocol 请求协议
|
||
* @param rspObject 回调对象
|
||
* @param showTips 是否触发请求提示
|
||
* @param force 是否强制发送
|
||
* @param channelId 通道编号
|
||
* @example
|
||
let protocol: IRequestProtocol = {
|
||
action: action,
|
||
method: method,
|
||
data: JSON.stringify(data),
|
||
isCompress: this.isCompress,
|
||
channelid: netConfig.channelid
|
||
}
|
||
return this.request(protocol, rspObject, showTips, force);
|
||
*/
|
||
request(reqProtocol: IRequestProtocol, rspObject: CallbackObject, showTips = true, force = false, channelId = 0) {
|
||
const node = this._channels[channelId];
|
||
if (node) {
|
||
node.request(reqProtocol, rspObject, showTips, force);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 同request功能一致,但在request之前会先判断队列中是否已有rspCmd,如有重复的则直接返回
|
||
* @param reqProtocol 请求协议
|
||
* @param rspObject 回调对象
|
||
* @param showTips 是否触发请求提示
|
||
* @param force 是否强制发送
|
||
* @param channelId 通道编号
|
||
* @example
|
||
let protocol: IRequestProtocol = {
|
||
action: action,
|
||
method: method,
|
||
data: JSON.stringify(data),
|
||
isCompress: this.isCompress,
|
||
channelid: netConfig.channelid
|
||
}
|
||
return this.request(protocol, rspObject, showTips, force);
|
||
*/
|
||
requestUnique(reqProtocol: IRequestProtocol, rspObject: CallbackObject, showTips = true, force = false, channelId = 0): boolean {
|
||
const node = this._channels[channelId];
|
||
if (node) {
|
||
return node.requestUnique(reqProtocol, rspObject, showTips, force);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 节点网络断开
|
||
* @param code 关闭码
|
||
* @param reason 关闭原因
|
||
* @param channelId 通道编号
|
||
* @example
|
||
* NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
|
||
*/
|
||
close(code?: number, reason?: string, channelId = 0) {
|
||
if (this._channels[channelId]) {
|
||
return this._channels[channelId].closeSocket(code, reason);
|
||
}
|
||
}
|
||
}
|