优化框架中自定义 type

This commit is contained in:
dgflash
2026-02-23 17:41:50 +08:00
parent aec1a6567f
commit c5ddb8c100
7 changed files with 32 additions and 17 deletions

View File

@@ -9,6 +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';
/** 游戏元素打开参数 */
export interface ElementParams {
@@ -37,7 +38,7 @@ export class GameManager {
* @param params 可选参数据
* @returns Promise<Node | null> 成功返回节点,失败返回 null
*/
async open(parent: Node | GameComponent, prefabPath: string, params?: ElementParams): Promise<Node | null> {
async open(parent: View, prefabPath: string, params?: ElementParams): Promise<Node | null> {
try {
// 简化 bundleName 获取逻辑
const bundleName = params?.bundle || resLoader.defaultBundleName;
@@ -61,7 +62,7 @@ export class GameManager {
return null;
}
node.parent = parent;
// 记录手动管理的节点,便于后续释放
this._manualNodes.add(node);
}
@@ -144,7 +145,7 @@ export class GameManager {
destroy(): void {
// 释放所有手动管理的节点
this.releaseAllManualNodes();
// 清理引用
this.root = null!;
}

View File

@@ -1,4 +1,4 @@
import type { GameComponentCtor, UICtor } from '../../module/types/Types';
import type { GameComponentCtor, UICtor } from '../../types/Types';
import type { UIConfigMap } from './layer/LayerEnum';
import type { UIConfig } from './layer/UIConfig';

View File

@@ -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, GUIView, UICtor, ViewCtor } from '../types/Types';
import type { BusinessCtor, EntityCtor, UICtor, View, ViewCtor } from '../../types/Types';
import type { CCBusiness } from './CCBusiness';
import { GameComponent } from './GameComponent';
@@ -77,14 +77,14 @@ export abstract class CCEntity extends ecs.Entity {
//#region 游戏视图层管理
/**
* 通过资源内存中获取预制上的组件添加到ECS实体中
* @param ctor 界面逻辑组件(支持 ECSView 或使用 gui.register 注册的 GUIView
* @param ctor 界面逻辑组件(支持 ECSView 或使用 gui.register 注册的 GameComponent
* @param parent 显示对象父级
* @param path 显示资源地址(可选,不传时使用 @game.prefab 装饰器注册的路径)
* @param bundleName 资源包名称(可选,不传时使用 @game.prefab 装饰器注册的包名)
*/
async addPrefab<T extends GUIView>(
async addPrefab<T extends GameComponent>(
ctor: ViewCtor<T>,
parent: Node | GameComponent,
parent: View,
path?: string,
bundleName?: string
): Promise<Node> {
@@ -117,13 +117,26 @@ export abstract class CCEntity extends ecs.Entity {
return node;
}
/**
* 移除预制体组件及其节点
* @param node 要销毁的节点或组件Node 或 GameComponent
*/
removePrefab(node: View): void {
if (node instanceof GameComponent) {
node.remove();
}
else {
node.destroy();
}
}
/**
* 添加视图层组件
* @param ctor 界面逻辑组件(支持 CCView 或使用 gui.register 注册的 GameComponent 子类)
* @param params 界面参数
* @returns 界面节点
*/
async addUi<T extends GUIView>(ctor: UICtor<T>, params?: UIParam): Promise<Node> {
async addUi<T extends GameComponent>(ctor: UICtor<T>, params?: UIParam): Promise<Node> {
const key = gui.internal.getKey(ctor);
if (!key) {
throw new Error(`${ctor.name} 界面组件未使用 gui.register 注册`);

View File

@@ -11,6 +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 { GameComponent } from './GameComponent';
/**
@@ -83,7 +84,6 @@ export abstract class CCView<T extends CCEntity> extends GameComponent implement
* 注意:子类应该显式初始化此属性
*/
protected data?: any;
//#endregion
/**
* 组件加载时调用
@@ -140,7 +140,7 @@ export abstract class CCView<T extends CCEntity> extends GameComponent implement
* @private
*/
private replaceVMPath(comp: Component, tag: string) {
// @ts-ignore - 优化:使用 any 类型避免多次类型转换
// 优化:使用 any 类型避免多次类型转换
const vmComp: any = comp;
const path: string = vmComp.watchPath;
@@ -222,6 +222,7 @@ export abstract class CCView<T extends CCEntity> extends GameComponent implement
return result;
}
//#endregion
/** 从父节点移除自己 */
remove() {
@@ -241,7 +242,7 @@ export abstract class CCView<T extends CCEntity> extends GameComponent implement
return;
}
this.ent.removeUi(cct);
this.ent.removeUi(cct as unknown as UICtor);
this.ent = null!; // 清空引用,避免内存泄漏
}
@@ -257,7 +258,6 @@ export abstract class CCView<T extends CCEntity> extends GameComponent implement
// 解除全部引用
if (this.tag) {
VM.remove(this.tag);
// @ts-ignore - 优化:显式清空引用,帮助 GC
this.tag = undefined;
}

View File

@@ -2,7 +2,7 @@
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "050d0e85-32a0-4573-b0ef-d8911bf7f13d",
"uuid": "172d5432-5689-483f-8ede-94d9fdf5c747",
"files": [],
"subMetas": {},
"userData": {}

View File

@@ -1,3 +1,4 @@
import type { Node } from 'cc';
import type { GameComponent } from 'db://oops-framework/module/common/GameComponent';
import type { ecs } from 'db://oops-framework/libs/ecs/ECS';
import type { CCView } from 'db://oops-framework/module/common/CCView';
@@ -17,14 +18,14 @@ export type GameComponentCtor<T extends GameComponent = GameComponent> = Ctor<T>
/** UI 组件构造函数类型(用于继承自 GameComponent 并使用 gui.register 注册的组件) */
export type UICtor<T extends GameComponent = GameComponent> = Ctor<T>;
/** 通用的视图组件构造函数类型(支持 ECSView 或 GUIView */
/** 通用的视图组件构造函数类型(支持 ECSView 或 GameComponent */
export type ViewCtor<T extends GameComponent | ecs.Comp = GameComponent | ecs.Comp> = Ctor<T>;
/** ECS 游戏视图组件类型(继承自 CCView用于完整的 ECS 组件) */
export type ECSView = CCView<CCEntity>;
/** GUI 视图组件类型(继承自 GameComponent使用 @gui.register 装饰器注册的组件 */
export type GUIView = GameComponent;
/** 视图节点类型Node 或 GameComponent */
export type View = Node | GameComponent;
// ==================== 实体类型 ====================