CCView与CCViewVM支持泛型实体类型

This commit is contained in:
dgflash
2025-09-19 23:02:53 +08:00
parent 21472de6bc
commit d120563ac4
3 changed files with 9 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ import { CCView } from "./CCView";
import { CCViewVM } from "./CCViewVM";
export type ECSCtor<T extends ecs.Comp> = __private.__types_globals__Constructor<T> | __private.__types_globals__AbstractedConstructor<T>;
export type ECSView = CCViewVM<CCEntity> | CCView<CCEntity>;
/** ECS 游戏模块实体 */
export abstract class CCEntity extends ecs.Entity {
@@ -55,7 +56,7 @@ export abstract class CCEntity extends ecs.Entity {
* @param path 显示资源地址
* @param bundleName 资源包名称
*/
addPrefab<T extends CCViewVM | CCView>(ctor: ECSCtor<T>, parent: Node, path: string, bundleName: string = resLoader.defaultBundleName) {
addPrefab<T extends ECSView>(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);
@@ -68,7 +69,7 @@ export abstract class CCEntity extends ecs.Entity {
* @param params 界面参数
* @returns 界面节点
*/
addUi<T extends CCViewVM | CCView>(ctor: ECSCtor<T>, params?: UIParam): Promise<Node> {
addUi<T extends ECSView>(ctor: ECSCtor<T>, params?: UIParam): Promise<Node> {
return new Promise<Node>(async (resolve, reject) => {
const key = gui.internal.getKey(ctor);
if (key) {

View File

@@ -24,7 +24,7 @@ import { GameComponent } from './GameComponent';
* @example
@ccclass('RoleViewComp')
@ecs.register('RoleView', false)
export class RoleViewComp extends CCView {
export class RoleViewComp extends CCView<Role> {
@property({ type: sp.Skeleton, tooltip: '角色动画' })
spine: sp.Skeleton = null!;
@@ -33,12 +33,12 @@ export class RoleViewComp extends CCView {
}
}
*/
export abstract class CCView extends GameComponent implements ecs.IComp {
export abstract class CCView<T extends CCEntity> extends GameComponent implements ecs.IComp {
static tid: number = -1;
static compName: string;
canRecycle!: boolean;
ent!: CCEntity;
ent!: T;
tid: number = -1;
/** 从父节点移除自己 */

View File

@@ -25,7 +25,7 @@ import { CCEntity } from './CCEntity';
* @example
@ccclass('LoadingViewComp')
@ecs.register('LoadingView', false)
export class LoadingViewComp extends CCViewVM {
export class LoadingViewComp extends CCViewVM<Initialize> {
// VM 组件绑定数据
data: any = {
// 加载资源当前进度
@@ -45,12 +45,12 @@ export class LoadingViewComp extends CCViewVM {
}
}
*/
export abstract class CCViewVM extends VMParent implements ecs.IComp {
export abstract class CCViewVM<T extends CCEntity> extends VMParent implements ecs.IComp {
static tid: number = -1;
static compName: string;
canRecycle!: boolean;
ent!: CCEntity;
ent!: T;
tid: number = -1;
/** 从父节点移除自己 */