mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-07 01:01:09 +08:00
1. 添加 ECS 业务实体 CCEntity 对象
2. 删除 ModuleUtil.ts 文件,功能移置到 CCEntity 对象中,简化 Api 的使用
This commit is contained in:
@@ -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} 移除失败,组件未注册`);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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": {}
|
||||
@@ -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} 移除失败,组件未注册`);
|
||||
|
||||
Reference in New Issue
Block a user