diff --git a/assets/module/common/CCComp.ts b/assets/module/common/CCComp.ts index 3320319..f37a178 100644 --- a/assets/module/common/CCComp.ts +++ b/assets/module/common/CCComp.ts @@ -7,11 +7,11 @@ import { ecs } from '../../libs/ecs/ECS'; import { ECSModel } from '../../libs/ecs/ECSModel'; +import { CCEntity } from './CCEntity'; import { GameComponent } from './GameComponent'; -import { ModuleUtil } from './ModuleUtil'; /** - * 游戏显示对象组件 + * ECS 游戏显示对象组件 * * 功能介绍: * 1. 对象拥有 cc.Component 组件功能与 ecs.Comp 组件功能 @@ -38,14 +38,14 @@ export abstract class CCComp extends GameComponent implements ecs.IComp { static compName: string; canRecycle!: boolean; - ent!: ecs.Entity; + ent!: CCEntity; tid: number = -1; /** 从父节点移除自己 */ remove() { const cct = ECSModel.compCtors[this.tid]; if (this.ent) { - ModuleUtil.removeGui(this.ent, cct); + this.ent.removeUi(cct); } else { console.error(`组件 ${this.name} 移除失败,组件未注册`); diff --git a/assets/module/common/ModuleUtil.ts b/assets/module/common/CCEntity.ts similarity index 74% rename from assets/module/common/ModuleUtil.ts rename to assets/module/common/CCEntity.ts index c2dbc53..d7f3b05 100644 --- a/assets/module/common/ModuleUtil.ts +++ b/assets/module/common/CCEntity.ts @@ -1,8 +1,8 @@ -import { Node, __private } from "cc"; -import { oops } from "../../core/Oops"; +import { __private, Node } from "cc"; import { resLoader } from "../../core/common/loader/ResLoader"; import { gui } from "../../core/gui/Gui"; import { LayerUIElement, UIParam } from "../../core/gui/layer/LayerUIElement"; +import { oops } from "../../core/Oops"; import { ViewUtil } from "../../core/utils/ViewUtil"; import { ecs } from "../../libs/ecs/ECS"; import { CompType } from "../../libs/ecs/ECSModel"; @@ -11,15 +11,29 @@ import { CCVMParentComp } from "./CCVMParentComp"; export type ECSCtor = __private.__types_globals__Constructor | __private.__types_globals__AbstractedConstructor; -export class ModuleUtil { +/** ECS 游戏模块实体 */ +export class CCEntity extends ecs.Entity { /** - * 异步添加视图层组件 - * @param ent 模块实体 + * 通过资源内存中获取预制上的组件添加到ECS实体中 + * @param ctor 界面逻辑组件 + * @param parent 显示对象父级 + * @param path 显示资源地址 + * @param bundleName 资源包名称 + */ + addPrefab(ctor: ECSCtor, parent: Node, path: string, bundleName: string = resLoader.defaultBundleName) { + const node = ViewUtil.createPrefabNode(path, bundleName); + const comp = node.getComponent(ctor)!; + this.add(comp); + node.parent = parent; + } + + /** + * 添加视图层组件 * @param ctor 界面逻辑组件 * @param params 界面参数 * @returns 界面节点 */ - static addGui(ent: ecs.Entity, ctor: ECSCtor, params?: UIParam): Promise { + addUi(ctor: ECSCtor, params?: UIParam): Promise { return new Promise(async (resolve, reject) => { //@ts-ignore const key = ctor[gui.internal.GUI_KEY]; @@ -33,7 +47,7 @@ export class ModuleUtil { let node = await oops.gui.open(key, params); const comp = node.getComponent(ctor) as ecs.Comp; - ent.add(comp); + this.add(comp); oops.gui.show(key); resolve(node); } @@ -44,12 +58,10 @@ export class ModuleUtil { } /** - * 业务实体上移除界面组件 - * @param ent 模块实体 + * 移除视图层组件 * @param ctor 界面逻辑组件 - * @param params 界面关闭参数 */ - static removeGui(ent: ecs.Entity, ctor: CompType) { + removeUi(ctor: CompType) { //@ts-ignore const key = ctor[gui.internal.GUI_KEY]; if (key) { @@ -62,28 +74,13 @@ export class ModuleUtil { const comp = node.getComponent(LayerUIElement); if (comp) { comp.onClose = () => { - if (comp.state.config.destroy) ent.remove(ctor); + if (comp.state.config.destroy) this.remove(ctor); }; oops.gui.remove(key); } } else { - ent.remove(ctor); + this.remove(ctor); } } - - /** - * 通过资源内存中获取预制上的组件添加到ECS实体中 - * @param ent 模块实体 - * @param ctor 界面逻辑组件 - * @param parent 显示对象父级 - * @param path 显示资源地址 - * @param bundleName 资源包名称 - */ - static addView(ent: ecs.Entity, ctor: ECSCtor, parent: Node, path: string, bundleName: string = resLoader.defaultBundleName) { - const node = ViewUtil.createPrefabNode(path, bundleName); - const comp = node.getComponent(ctor)!; - ent.add(comp); - node.parent = parent; - } } \ No newline at end of file diff --git a/assets/module/common/ModuleUtil.ts.meta b/assets/module/common/CCEntity.ts.meta similarity index 70% rename from assets/module/common/ModuleUtil.ts.meta rename to assets/module/common/CCEntity.ts.meta index 1a06125..7040995 100644 --- a/assets/module/common/ModuleUtil.ts.meta +++ b/assets/module/common/CCEntity.ts.meta @@ -2,7 +2,7 @@ "ver": "4.0.24", "importer": "typescript", "imported": true, - "uuid": "52a6c740-3b9b-46c5-a784-d53a6b67954a", + "uuid": "59805570-9a5d-4894-aac0-14a60c895c74", "files": [], "subMetas": {}, "userData": {} diff --git a/assets/module/common/CCVMParentComp.ts b/assets/module/common/CCVMParentComp.ts index fa9c9c2..9f6cbf4 100644 --- a/assets/module/common/CCVMParentComp.ts +++ b/assets/module/common/CCVMParentComp.ts @@ -8,10 +8,10 @@ import { ecs } from '../../libs/ecs/ECS'; import { ECSModel } from '../../libs/ecs/ECSModel'; import VMParent from '../../libs/model-view/VMParent'; -import { ModuleUtil } from './ModuleUtil'; +import { CCEntity } from './CCEntity'; /** - * 支持 MVVM 功能的游戏显示对象组件 + * 支持 MVVM 功能的 ECS 游戏显示对象组件 * * 使用方法: * 1. 对象拥有 cc.Component 组件功能与 ecs.Comp 组件功能 @@ -50,14 +50,14 @@ export abstract class CCVMParentComp extends VMParent implements ecs.IComp { static compName: string; canRecycle!: boolean; - ent!: ecs.Entity; + ent!: CCEntity; tid: number = -1; /** 从父节点移除自己 */ remove() { const cct = ECSModel.compCtors[this.tid]; if (this.ent) { - ModuleUtil.removeGui(this.ent, cct); + this.ent.removeUi(cct); } else { console.error(`组件 ${this.name} 移除失败,组件未注册`);