1. 添加 ECS 业务实体 CCEntity 对象

2. 删除 ModuleUtil.ts 文件,功能移置到 CCEntity 对象中,简化 Api 的使用
This commit is contained in:
dgflash
2025-09-11 15:39:38 +08:00
parent 51ec98f293
commit 0a9112ea64
4 changed files with 34 additions and 37 deletions

View File

@@ -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} 移除失败,组件未注册`);

View File

@@ -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<T extends ecs.Comp> = __private.__types_globals__Constructor<T> | __private.__types_globals__AbstractedConstructor<T>;
export class ModuleUtil {
/** ECS 游戏模块实体 */
export class CCEntity extends ecs.Entity {
/**
*
* @param ent
* ECS实体中
* @param ctor
* @param parent
* @param path
* @param bundleName
*/
addPrefab<T extends CCVMParentComp | CCComp>(ctor: ECSCtor<T>, 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<T extends CCVMParentComp | CCComp>(ent: ecs.Entity, ctor: ECSCtor<T>, params?: UIParam): Promise<Node> {
addUi<T extends CCVMParentComp | CCComp>(ctor: ECSCtor<T>, params?: UIParam): Promise<Node> {
return new Promise<Node>(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<ecs.IComp>) {
removeUi(ctor: CompType<ecs.IComp>) {
//@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<T extends CCVMParentComp | CCComp>(ent: ecs.Entity, ctor: ECSCtor<T>, 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;
}
}

View File

@@ -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": {}

View File

@@ -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} 移除失败,组件未注册`);