diff --git a/CHANGELOG.md b/CHANGELOG.md index 945009f..a82df92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,4 +37,8 @@ * 仓库地址: https://github.com/Gongxh0901/kunpo-quadtree - 拆分行为树模块,使用 `npm install kunpocc-behaviortree` 安装 - * 仓库地址: https://github.com/Gongxh0901/kunpocc-behaviortree \ No newline at end of file + * 仓库地址: https://github.com/Gongxh0901/kunpocc-behaviortree + +## 1.1.4 事件模块拆分 +- 拆分事件模块,使用 `npm install kunpocc-event` 安装 + * 仓库地址: https://github.com/Gongxh0901/kunpocc-event \ No newline at end of file diff --git a/package.json b/package.json index af0bdd3..d43d68a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kunpocc", - "version": "1.1.3", + "version": "1.1.4", "description": "基于creator3.0+的kunpocc库", "main": "./dist/kunpocc.cjs", "module": "./dist/kunpocc.mjs", @@ -43,7 +43,8 @@ "registry": "https://registry.npmjs.org/" }, "dependencies": { - "fairygui-cc": "^1.2.2" + "fairygui-cc": "^1.2.2", + "kunpocc-event": "^0.0.2" }, "devDependencies": { "@cocos/creator-types": "^3.8.0", diff --git a/src/cocos/CocosEntry.ts b/src/cocos/CocosEntry.ts index 4e58c53..ba2c7b7 100644 --- a/src/cocos/CocosEntry.ts +++ b/src/cocos/CocosEntry.ts @@ -5,7 +5,6 @@ */ import { _decorator, Component, director, game, JsonAsset, macro, sys } from "cc"; -import { GlobalEvent } from "../global/GlobalEvent"; import { GlobalTimer } from "../global/GlobalTimer"; import { enableDebugMode, FrameConfig } from "../global/header"; import { InnerTimer } from "../global/InnerTimer"; @@ -47,7 +46,6 @@ export abstract class CocosEntry extends Component { this.node.setSiblingIndex(this.node.children.length - 1); PropsHelper.setConfig(this.uiConfig?.json); this.initPlatform(); - this.initEvent(); this.initTime(); this.initAdapter(); this.initModule(); @@ -108,14 +106,6 @@ export abstract class CocosEntry extends Component { debug(`platform: ${PlatformType[Platform.platform]}`); } - /** - * 初始化事件 - * @internal - */ - private initEvent(): void { - GlobalEvent._initGlobalEvent(); - } - /** * 初始化时间 * @internal diff --git a/src/event/Event.ts b/src/event/Event.ts deleted file mode 100644 index fbcc215..0000000 --- a/src/event/Event.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * @Author: Gongxh - * @Date: 2024-12-21 - * @Description: - */ - -/** @internal */ -export class Event { - public id: number; - public name: string; - public target: any; - public once: boolean = false; - public callback: (...arg: any[]) => void; - public _destroy: boolean = false; - public _reset(): void { - this._destroy = false; - } - public _recycle(): void { - this._destroy = true; - } -} \ No newline at end of file diff --git a/src/event/EventFactory.ts b/src/event/EventFactory.ts deleted file mode 100644 index d907f2f..0000000 --- a/src/event/EventFactory.ts +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @Author: Gongxh - * @Date: 2024-12-21 - * @Description: - */ - -import { Event } from "./Event"; - -/** @internal */ -export class EventFactory { - private _id: number = 0; - private _stack: Event[] = []; - private _maxCapacity: number = 64; - private _msgClass: new () => Event; - - get id(): number { - return this._id++; - } - - constructor(capacity: number, objectClass: new () => Event) { - this._maxCapacity = capacity; - this._msgClass = objectClass; - } - - public allocate(): T { - if (this._stack.length == 0) { - const ret = new this._msgClass() as T; - ret.id = this.id; - return ret; - } - const ret = this._stack.pop() as T; - ret._reset(); - return ret; - } - - public recycle(ret: Event): boolean { - if (this._maxCapacity > 0 && this._stack.length < this._maxCapacity) { - ret._recycle(); - this._stack.push(ret); - return true; - } - return false; - } -} \ No newline at end of file diff --git a/src/event/EventManager.ts b/src/event/EventManager.ts deleted file mode 100644 index f2440a6..0000000 --- a/src/event/EventManager.ts +++ /dev/null @@ -1,219 +0,0 @@ -/** - * @Author: Gongxh - * @Date: 2024-12-21 - * @Description: - */ - -import { Event } from "./Event"; -import { EventFactory } from "./EventFactory"; - -export class EventManager { - /** @internal */ - private _idToEvent: Map = new Map(); - /** @internal */ - private _nameToIds: Map> = new Map>(); - /** @internal */ - private _targetToIds: Map> = new Map>(); - /** @internal */ - private _factroy: EventFactory = new EventFactory(64, Event); - /** - * 添加事件监听器。 - * @param name - 事件名称。 - * @param callback - 回调函数,当事件触发时执行。 - * @param target - 可选参数,指定事件监听的目标对象。 - * 该方法将事件和回调函数注册到事件管理器中,以便在事件触发时执行相应的回调函数。 - */ - public addEvent(name: string, callback: (...args: any[]) => void, target?: any): void { - this._addEvent(name, callback, false, target); - } - - /** - * 添加一个只触发一次的事件监听器。 - * @param name - 事件名称。 - * @param callback - 事件触发时要执行的回调函数。 - * @param target - 可选参数,指定事件监听器的目标对象。 - */ - public addEventOnce(name: string, callback: (...args: any[]) => void, target?: any): void { - this._addEvent(name, callback, true, target); - } - - /** - * 发送事件给所有注册的监听器。 - * @param name - 事件名称。 - * @param target - 可选参数,指定目标对象,只有目标对象匹配时才会触发监听器。 (制定目标对象 效率更高) - * @param args - 传递给监听器回调函数的参数。 - */ - public send(name: string, target?: any, ...args: any[]): void { - let nameToIds = this._nameToIds; - if (!nameToIds.has(name)) { - return; - } - let ids = nameToIds.get(name); - let listenerMap = this._idToEvent; - - let needRemoveIds: number[] = []; - let triggerList: Event[] = []; - for (const id of ids.values()) { - if (!listenerMap.has(id)) { - throw new Error(`消息ID:【${id}】不存在`); - } - let listener = listenerMap.get(id); - if (!listener._destroy && (!target || target == listener.target)) { - triggerList.push(listener); - if (listener.once) { - listener._destroy = true; - needRemoveIds.push(listener.id); - } - } - } - for (const listener of triggerList) { - listener.callback(...args); - } - if (needRemoveIds.length > 0) { - for (const id of needRemoveIds) { - this._remove(id); - } - } - } - - /** - * 移除指定名称的事件监听器。 - * @param name - 事件名称。 - * @param callback - 要移除的回调函数。 - * @param target - 回调函数绑定的目标对象。 - * 该方法会遍历与指定名称关联的所有监听器ID,检查每个监听器的回调函数和目标对象, - * 如果匹配则将其ID添加到待移除列表中,最后统一移除这些监听器。 - */ - public remove(name: string, callback: () => void, target: any): void { - let nameToIds = this._nameToIds; - if (!nameToIds.has(name)) { - return; - } - let ids = nameToIds.get(name); - if (ids.size == 0) { - return; - } - - let needRemoveIds: number[] = []; - for (const id of ids.values()) { - let listener = this._idToEvent.get(id); - let needRemove = true; - if (callback && listener.callback != callback) { - needRemove = false; - } - if (target && listener.target != target) { - needRemove = false; - } - needRemove && needRemoveIds.push(id); - } - if (needRemoveIds.length > 0) { - for (const id of needRemoveIds) { - this._remove(id); - } - } - } - - public removeByNameAndTarget(name: string, target: any): void { - this.remove(name, null, target); - } - - public removeByNameAndCallback(name: string, callback: () => void): void { - this.remove(name, callback, null); - } - - /** - * 移除与指定目标关联的所有监听器。 - * 如果目标不存在或关联的监听器ID集合为空,则不执行任何操作。 - * 对于每个监听器ID,从_idToEvent映射中删除监听器,并将其回收到工厂中。 - * 同时,更新_nameToIds映射,确保名称到ID集合的映射保持最新。 - * @param target - 要移除监听器的目标对象。 - */ - public removeList(target: any): void { - let targetToIds = this._targetToIds; - if (!targetToIds.has(target)) { - return; - } - let ids = targetToIds.get(target); - if (ids.size == 0) { - return; - } - for (const id of ids.values()) { - let listener = this._idToEvent.get(id); - let name = listener.name; - - this._idToEvent.delete(id); - this._factroy.recycle(listener); - - let nameToIds = this._nameToIds; - if (nameToIds.has(name)) { - nameToIds.get(name).delete(id); - } - } - ids.clear(); - } - - public destroyAll(): void { - let listeners = this._idToEvent; - for (const listener of listeners.values()) { - this._factroy.recycle(listener); - } - this._idToEvent.clear(); - this._nameToIds.clear(); - this._targetToIds.clear(); - } - - /** @internal */ - public _addEvent(name: string, callback: (...arg: any[]) => void, once: boolean, target: any): void { - let listener = this._factroy.allocate(); - listener.name = name; - listener.target = target; - listener.once = once; - listener.callback = callback; - this._idToEvent.set(listener.id, listener); - - let nameToIds = this._nameToIds; - let ids: Set; - if (nameToIds.has(name)) { - ids = nameToIds.get(name); - } else { - ids = new Set(); - nameToIds.set(name, ids); - } - ids.add(listener.id); - if (target) { - let targetToIds = this._targetToIds; - if (!targetToIds.has(target)) { - let ids = new Set(); - ids.add(listener.id); - targetToIds.set(target, ids); - } else { - let ids = targetToIds.get(target); - ids.add(listener.id); - } - } - } - - /** @internal */ - private _remove(id: number): void { - if (!this._idToEvent.has(id)) { - return; - } - let ids = this._idToEvent.get(id); - let name = ids.name; - let target = ids.target; - - this._idToEvent.delete(id); - this._factroy.recycle(ids); - - let nameToIds = this._nameToIds; - if (nameToIds.has(name)) { - nameToIds.get(name).delete(id); - } - if (target) { - let targetToIds = this._targetToIds; - if (targetToIds.has(target)) { - targetToIds.get(target).delete(id); - } - } - } -} \ No newline at end of file diff --git a/src/global/Adapter.ts b/src/global/Adapter.ts index ab86c59..f3afd92 100644 --- a/src/global/Adapter.ts +++ b/src/global/Adapter.ts @@ -5,9 +5,9 @@ */ import { ResolutionPolicy, view } from "cc"; +import { GlobalEvent } from "kunpocc-event"; import { debug } from "../tool/log"; import { WindowManager } from "../ui/WindowManager"; -import { GlobalEvent } from "./GlobalEvent"; import { Screen } from "./Screen"; import { size } from "./header"; diff --git a/src/global/GlobalEvent.ts b/src/global/GlobalEvent.ts deleted file mode 100644 index e048cad..0000000 --- a/src/global/GlobalEvent.ts +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @Author: Gongxh - * @Date: 2024-12-22 - * @Description: 全局事件 - */ - -import { EventManager } from "../event/EventManager"; - -export class GlobalEvent { - /** @internal */ - private static _globalEvent: EventManager = null; - public static add(eventName: string, callback: (...args: any[]) => void, target: any): void { - this._globalEvent.addEvent(eventName, callback, target); - } - - public static addOnce(eventName: string, callback: (...args: any[]) => void, target: any): void { - this._globalEvent.addEventOnce(eventName, callback, target); - } - - public static send(eventName: string, ...args: any[]): void { - this._globalEvent.send(eventName, null, ...args); - } - - public static sendToTarget(eventName: string, target: any, ...args: any[]) { - this._globalEvent.send(eventName, target, ...args); - } - - public static remove(eventName: string, callback: (...args: any[]) => void, target?: any): void { - this._globalEvent.remove(eventName, callback, target); - } - - public static removeByNameAndTarget(eventName: string, target: any) { - this._globalEvent.removeByNameAndTarget(eventName, target); - } - - public static removeByTarget(target: any): void { - this._globalEvent.removeList(target); - } - - /** @internal */ - public static _initGlobalEvent(): void { - if (!this._globalEvent) { - this._globalEvent = new EventManager(); - } - } -} \ No newline at end of file diff --git a/src/kunpocc.ts b/src/kunpocc.ts index 43c4c66..7f95d6e 100644 --- a/src/kunpocc.ts +++ b/src/kunpocc.ts @@ -1,5 +1,4 @@ /** 一些全局工具 */ -export { GlobalEvent } from "./global/GlobalEvent"; export { GlobalTimer } from "./global/GlobalTimer"; export { enableDebugMode, FrameConfig, KUNPO_DEBUG } from "./global/header"; export { Platform, PlatformType } from "./global/Platform"; @@ -12,8 +11,6 @@ export * from "./tool/log"; export { MathTool } from "./tool/Math"; export { md5 } from "./tool/MD5"; export { Time } from "./tool/Time"; -/** 消息监听 */ -export { EventManager } from "./event/EventManager"; /** Http */ export * from "./net/http/HttpManager";