From 79d5ee86374714b3ed5993473d067ff5d07395fb Mon Sep 17 00:00:00 2001 From: dgflash Date: Sun, 5 Apr 2026 10:00:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=BA=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/common/event/MessageManager.ts | 32 +---- assets/core/game/GameManager.ts | 2 +- assets/core/gui/Gui.ts | 2 +- assets/module/common/CCEntity.ts | 2 +- assets/module/common/CCView.ts | 2 +- assets/module/common/GameComponent.ts | 132 +++++++++--------- .../module/decorator/GamePrefabDecorator.ts | 2 +- assets/types/Event.d.ts | 27 ++++ assets/types/{Types.ts => Module.d.ts} | 0 9 files changed, 104 insertions(+), 97 deletions(-) create mode 100644 assets/types/Event.d.ts rename assets/types/{Types.ts => Module.d.ts} (100%) diff --git a/assets/core/common/event/MessageManager.ts b/assets/core/common/event/MessageManager.ts index 14f5479..742dbd6 100644 --- a/assets/core/common/event/MessageManager.ts +++ b/assets/core/common/event/MessageManager.ts @@ -1,27 +1,7 @@ import { log, warn } from 'cc'; -import type { ListenerFunc, ListenerFuncTyped } from './EventMessage'; import { EventData } from './EventData'; import { EventDataPool } from './EventDataPool'; - -/** - * 全局事件类型映射接口 - * 业务层可以通过 declare module 扩展此接口来定义强类型事件 - * - * @example - * // 在业务代码中扩展 - * declare module 'db://oops-framework/core/common/event/MessageManager' { - * interface TypedEventMap { - * [YourEvent.SomeEvent]: { data: string }; - * } - * } - */ - -export interface TypedEventMap { - // 业务层通过 declare module 扩展此接口 -} - -/** 获取 TypedEventMap 中所有事件 key 的联合类型 */ -export type EventMapKeys = keyof TypedEventMap; +import type { ListenerFunc, ListenerFuncTyped } from './EventMessage'; /** * 全局消息管理 @@ -53,7 +33,7 @@ export class MessageManager { * @param listener 处理事件的侦听器函数 * @param object 侦听函数绑定的作用域对象 */ - watch(event: K, listener: ListenerFuncTyped, object: object): void { + watch(event: K, listener: ListenerFuncTyped, object: object): void { this.on(event as string, listener as ListenerFunc, object); } @@ -63,7 +43,7 @@ export class MessageManager { * @param listener 事件触发回调方法 * @param object 侦听函数绑定的作用域对象 */ - watchOnce(event: K, listener: ListenerFuncTyped, object: object): void { + watchOnce(event: K, listener: ListenerFuncTyped, object: object): void { this.once(event as string, listener as ListenerFunc, object); } @@ -73,7 +53,7 @@ export class MessageManager { * @param listener 处理事件的侦听器函数(可选,不传则移除该事件的所有监听器) * @param object 侦听函数绑定的作用域对象(可选) */ - unwatch(event: K, listener?: ListenerFuncTyped, object?: object): void { + unwatch(event: K, listener?: ListenerFuncTyped, object?: object): void { this.off(event as string, listener as ListenerFunc, object); } @@ -83,7 +63,7 @@ export class MessageManager { * @param data 事件数据(必须完全匹配类型定义) * @note 使用此方法可获得编译时的强类型约束,参数不匹配会编译报错 */ - emit(event: K, data: TypedEventMap[K]): void { + emit(event: K, data: OopsFramework.TypedEventMap[K]): void { const list = this.events.get(event as string); if (list != null) { const eds: Array = list.concat(); @@ -101,7 +81,7 @@ export class MessageManager { * @param data 事件数据(必须完全匹配类型定义) * @note 使用此方法可获得编译时的强类型约束,参数不匹配会编译报错 */ - emitAsync(event: K, data: TypedEventMap[K]): Promise { + emitAsync(event: K, data: OopsFramework.TypedEventMap[K]): Promise { return new Promise((resolve) => { const list = this.events.get(event as string); if (list != null) { diff --git a/assets/core/game/GameManager.ts b/assets/core/game/GameManager.ts index 3f6a188..84e9e4c 100644 --- a/assets/core/game/GameManager.ts +++ b/assets/core/game/GameManager.ts @@ -9,7 +9,7 @@ import { director, isValid } from 'cc'; import { GameComponent } from '../../module/common/GameComponent'; import { resLoader } from '../common/loader/ResLoader'; import { ViewUtil } from '../utils/ViewUtil'; -import { View } from '../../types/Types'; +import { View } from '../../types/Module'; /** 游戏元素打开参数 */ export interface ElementParams { diff --git a/assets/core/gui/Gui.ts b/assets/core/gui/Gui.ts index 03ede4b..7fdc7c4 100644 --- a/assets/core/gui/Gui.ts +++ b/assets/core/gui/Gui.ts @@ -1,4 +1,4 @@ -import type { GameComponentCtor, UICtor } from '../../types/Types'; +import type { GameComponentCtor, UICtor } from '../../types/Module'; import type { UIConfigMap } from './layer/LayerEnum'; import type { UIConfig } from './layer/UIConfig'; diff --git a/assets/module/common/CCEntity.ts b/assets/module/common/CCEntity.ts index 52e3030..2c6ffd2 100644 --- a/assets/module/common/CCEntity.ts +++ b/assets/module/common/CCEntity.ts @@ -7,7 +7,7 @@ import { ViewUtil } from '../../core/utils/ViewUtil'; import { ecs } from '../../libs/ecs/ECS'; import type { ECSEntity } from '../../libs/ecs/ECSEntity'; import type { CompType } from '../../libs/ecs/ECSModel'; -import type { BusinessCtor, EntityCtor, UICtor, View, ViewCtor } from '../../types/Types'; +import type { BusinessCtor, EntityCtor, UICtor, View, ViewCtor } from '../../types/Module'; import type { CCBusiness } from './CCBusiness'; import { GameComponent } from './GameComponent'; diff --git a/assets/module/common/CCView.ts b/assets/module/common/CCView.ts index ac0ec99..f000c0f 100644 --- a/assets/module/common/CCView.ts +++ b/assets/module/common/CCView.ts @@ -11,7 +11,7 @@ import { ECSModel } from '../../libs/ecs/ECSModel'; import { VM } from '../../libs/model-view/ViewModel'; import { VMBase } from '../../libs/model-view/VMBase'; import type { CCEntity } from './CCEntity'; -import type { UICtor } from '../../types/Types'; +import type { UICtor } from '../../types/Module'; import { GameComponent } from './GameComponent'; /** diff --git a/assets/module/common/GameComponent.ts b/assets/module/common/GameComponent.ts index 09587f0..4524d06 100644 --- a/assets/module/common/GameComponent.ts +++ b/assets/module/common/GameComponent.ts @@ -12,10 +12,10 @@ import type { IAudioParams } from '../../core/common/audio/IAudio'; import { EventDispatcher } from '../../core/common/event/EventDispatcher'; import type { ListenerFunc, ListenerFuncTyped } from '../../core/common/event/EventMessage'; import { EventMessage } from '../../core/common/event/EventMessage'; -import type { TypedEventMap } from '../../core/common/event/MessageManager'; import type { AssetType, CompleteCallback, Paths, ProgressCallback } from '../../core/common/loader/ResLoader'; import { resLoader } from '../../core/common/loader/ResLoader'; import { ViewUtil } from '../../core/utils/ViewUtil'; +import type { EventMapKeys } from '../../types/Event'; const { ccclass } = _decorator; @@ -57,27 +57,16 @@ export class GameComponent extends Component { /** 标记是否已注册按钮事件 */ private _buttonEnabled = false; + //#region 强类型事件方法(提供给 Agent 自动生成用) + /** * 注册全局事件(强类型) * @param event 事件名(枚举) * @param listener 处理事件的侦听器函数 * @param object 侦听函数绑定的this对象 */ - on(event: K, listener: ListenerFuncTyped, object: any): void; - - /** - * 注册全局事件(兼容旧用法) - * @param event 事件名 - * @param listener 处理事件的侦听器函数 - * @param object 侦听函数绑定的this对象 - */ - on(event: string, listener: ListenerFunc, object: any): void; - - /** - * 注册全局事件(实现) - */ - on(event: string, listener: ListenerFunc, object: any): void { - this.event.on(event, listener, object); + watch(event: K, listener: ListenerFuncTyped, object: any): void { + this.event.on(event as string, listener as ListenerFunc, object); } /** @@ -86,60 +75,18 @@ export class GameComponent extends Component { * @param listener 事件触发回调方法 * @param object 侦听函数绑定的this对象 */ - once(event: K, listener: ListenerFuncTyped, object: any): void; - - /** - * 监听一次事件,事件响应后,该监听自动移除(兼容旧用法) - * @param event 事件名 - * @param listener 事件触发回调方法 - * @param object 侦听函数绑定的this对象 - */ - once(event: string, listener: ListenerFunc, object: any): void; - - /** - * 监听一次事件,事件响应后,该监听自动移除(实现) - */ - once(event: string, listener: ListenerFunc, object: any): void { - this.event.once(event, listener, object); + watchOnce(event: K, listener: ListenerFuncTyped, object: any): void { + this.event.once(event as string, listener as ListenerFunc, object); } /** * 移除全局事件(强类型) * @param event 事件名(枚举) + * @param listener 处理事件的侦听器函数(可选) + * @param object 侦听函数绑定的this对象(可选) */ - off(event: K): void; - - /** - * 移除全局事件(兼容旧用法) - * @param event 事件名 - */ - off(event: string): void; - - /** - * 移除全局事件(实现) - */ - off(event: string): void { - this.event.off(event); - } - - /** - * 触发全局事件(兼容旧用法) - * @param event 事件名 - * @param args 事件参数 - */ - - dispatchEvent(event: string, ...args: any[]): void { - this.event.dispatchEvent(event, ...args); - } - - /** - * 触发全局事件,支持同步与异步处理(兼容旧用法) - * @param event 事件名 - * @param args 事件参数 - */ - - dispatchEventAsync(event: string, ...args: any[]): Promise { - return this.event.dispatchEventAsync(event, ...args); + unwatch(event: K, listener?: ListenerFuncTyped, object?: any): void { + this.event.off(event as string, listener as ListenerFunc, object); } /** @@ -147,7 +94,7 @@ export class GameComponent extends Component { * @param event 事件名(枚举) * @param data 事件数据 */ - emit(event: K, data: TypedEventMap[K]): void { + emit(event: K, data: OopsFramework.TypedEventMap[K]): void { this.event.emit(event, data); } @@ -156,9 +103,62 @@ export class GameComponent extends Component { * @param event 事件名(枚举) * @param data 事件数据(必须完全匹配类型定义) */ - emitAsync(event: K, data: TypedEventMap[K]): Promise { + emitAsync(event: K, data: OopsFramework.TypedEventMap[K]): Promise { return this.event.emitAsync(event, data); } + + //#endregion + + //#region 弱类型事件方法 + /** + * 注册全局事件 + * @param event 事件名 + * @param listener 处理事件的侦听器函数 + * @param object 侦听函数绑定的this对象 + */ + on(event: string, listener: ListenerFunc, object: any): void { + this.event.on(event, listener, object); + } + + /** + * 监听一次事件,事件响应后,该监听自动移除 + * @param event 事件名 + * @param listener 事件触发回调方法 + * @param object 侦听函数绑定的this对象 + */ + once(event: string, listener: ListenerFunc, object: any): void { + this.event.once(event, listener, object); + } + + /** + * 移除全局事件 + * @param event 事件名 + * @param listener 处理事件的侦听器函数(可选) + * @param object 侦听函数绑定的this对象(可选) + */ + off(event: string, listener?: ListenerFunc, object?: object): void { + this.event.off(event, listener, object); + } + + /** + * 触发全局事件 + * @param event 事件名 + * @param args 事件参数 + */ + dispatchEvent(event: string, ...args: any[]): void { + this.event.dispatchEvent(event, ...args); + } + + /** + * 触发全局事件,支持同步与异步处理 + * @param event 事件名 + * @param args 事件参数 + */ + dispatchEventAsync(event: string, ...args: any[]): Promise { + return this.event.dispatchEventAsync(event, ...args); + } + //#endregion + //#endregion //#region 预制节点管理 diff --git a/assets/module/decorator/GamePrefabDecorator.ts b/assets/module/decorator/GamePrefabDecorator.ts index 4c7bc90..7634107 100644 --- a/assets/module/decorator/GamePrefabDecorator.ts +++ b/assets/module/decorator/GamePrefabDecorator.ts @@ -1,5 +1,5 @@ import { resLoader } from '../../core/common/loader/ResLoader'; -import { GameComponentCtor } from '../../types/Types'; +import { GameComponentCtor } from '../../types/Module'; /** * 游戏装饰器命名空间 diff --git a/assets/types/Event.d.ts b/assets/types/Event.d.ts new file mode 100644 index 0000000..86527d7 --- /dev/null +++ b/assets/types/Event.d.ts @@ -0,0 +1,27 @@ +/** + * 全局事件类型映射接口 + * 业务层可以通过 declare global 扩展 OopsFramework.TypedEventMap 来定义强类型事件 + * + * @example + * // 在业务代码中扩展 + * declare global { + * namespace OopsFramework { + * interface TypedEventMap { + * [YourEvent.SomeEvent]: { data: string }; + * } + * } + * } + */ + +declare global { + namespace OopsFramework { + interface TypedEventMap { + // 业务层通过 declare global 扩展此接口 + } + } +} + +/** 获取 TypedEventMap 中所有事件 key 的联合类型 */ +export type EventMapKeys = keyof OopsFramework.TypedEventMap; + +export { }; diff --git a/assets/types/Types.ts b/assets/types/Module.d.ts similarity index 100% rename from assets/types/Types.ts rename to assets/types/Module.d.ts