import { _decorator } from "cc"; import { logger } from "db://assets/Scripts/Main/Common/Logger"; const { ccclass, property } = _decorator; //事件管理器 @ccclass export default class li_EventManager { private static _Instance: li_EventManager = null; private _netEvent: object = {}; private _innerEvent: object = {}; public static get I(): li_EventManager { if (!li_EventManager._Instance) { li_EventManager._Instance = new li_EventManager(); } return li_EventManager._Instance; } /**监听网络事件 */ public onNetEL(netCodeId: number | string, ...param: any[]) { if (netCodeId) { let handlerArray = this._netEvent[netCodeId]; if (!handlerArray) { return; } for (let i = 0; i < handlerArray.length; i++) { const func = handlerArray[i]; if (func) { if (func.i_target) { func.apply(func.i_target, param); } else { func(param[0], param[1]); } } } } } /**添加网络事件 */ public addNetEL(netCodeId: number | string, i_callback: any, i_target: any = null): void { if (this._netEvent[netCodeId] == null) { this._netEvent[netCodeId] = []; } i_callback.i_target = i_target; this._netEvent[netCodeId].push(i_callback); } /**移除网络事件 */ public removeNetEL(netCodeId: number | string, i_callback: any) { let handlerArray = this._netEvent[netCodeId]; if (!handlerArray) return; let index = handlerArray.indexOf(i_callback); if (index >= 0) { handlerArray.splice(index, 1); for (let idx in handlerArray) { if (handlerArray[+idx] == i_callback) { return; } } i_callback.i_target = null; } } /**监听本地事件 */ public onInnerEL(innerId: number | string, ...param: any[]) { if (this._innerEvent[innerId] == null) { return; } let handlerArray: Array = this._innerEvent[innerId]; let i: number = 0; let length: number = handlerArray.length; let handler: Array = null; while (i < length) { handler = handlerArray[i]; if (handler == null) { i++; continue; } if ((handler[1] && handler[1]["isValid"] && handler[1]["isValid"] == false) || (handler[1] && handler[1]["node"] && handler[1]["node"]["isValid"] && handler[1]["node"]["isValid"] == false)) { //当前事件的节点以失效或销毁(事件未移除) handlerArray.splice(i, 1); } else { try { handler[0].apply(handler[1], param); } catch (e) { logger.error("innerErr:" + e.stack); } } if (handlerArray.length != length) { length = handlerArray.length; i--; } i++; } } /**添加本地事件 */ public addInnerEL(innerId: number | string, i_callback: Function, obj: any): void { let handlerArray: Array = this._innerEvent[innerId]; if (handlerArray == null) { handlerArray = []; this._innerEvent[innerId] = handlerArray; } //检测是否已经存在 for (let i = 0; i < handlerArray.length; i++) { if (handlerArray[i] == null || (handlerArray[i][0] == i_callback && handlerArray[i][1] == obj)) { return; } } this._innerEvent[innerId].push([i_callback, obj]); } /**移除本地事件 */ public removeInnerEL(innerId: number | string, i_callback: Function, obj: any) { let handlerArray: Array = this._innerEvent[innerId]; if (!handlerArray) return; for (let i = 0; i < handlerArray.length; i++) { if (handlerArray[i] == null || (handlerArray[i][0] == i_callback && handlerArray[i][1] == obj)) { handlerArray.splice(i, 1); break; } } if (handlerArray.length == 0) { handlerArray[innerId] = null; delete this._innerEvent[innerId]; } } /** * 移除某一对象的所有内部监听 * @param listenerObj 侦听函数所属对象 */ public removeAllInnerEL(listenerObj: any): void { let keys = Object.keys(this._innerEvent); for (let i: number = 0, len = keys.length; i < len; i++) { let type = keys[i]; let arr: Array = this._innerEvent[type]; if (arr) { for (let j = 0; j < arr.length; j++) { if (arr[j][1] == listenerObj) { arr.splice(j, 1); j--; } } if (arr.length == 0) { delete this._innerEvent[type]; } } } } /** * 移除某一对象的所有网络监听 * @param listenerObj 侦听函数所属对象 */ public removeAllNetEL(listenerObj: any): void { let keys = Object.keys(this._netEvent); for (let i: number = 0, len = keys.length; i < len; i++) { let type = keys[i]; let arr: Array = this._netEvent[type]; if (arr) { for (let j = 0; j < arr.length; j++) { if (arr[j].i_target == listenerObj) { arr.splice(j, 1); j--; } } if (arr.length == 0) { delete this._netEvent[type]; } } } } /**移除所有监听 */ public removeAllEL() { this._netEvent = {}; this._innerEvent = {}; } }