From 7a90ec71adf79bed895c442ca1fa62428d381fb5 Mon Sep 17 00:00:00 2001 From: dgflash Date: Tue, 23 Sep 2025 12:00:48 +0800 Subject: [PATCH] =?UTF-8?q?CCEntity=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E4=BE=8B=E5=AE=9E=E4=BD=93=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/module/common/CCEntity.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/assets/module/common/CCEntity.ts b/assets/module/common/CCEntity.ts index 3f4dc74..743accf 100644 --- a/assets/module/common/CCEntity.ts +++ b/assets/module/common/CCEntity.ts @@ -20,21 +20,39 @@ export abstract class CCEntity extends ecs.Entity { /** 单例子实体 */ private singletons: Map = null!; - /** 添加单例子实体 */ - addChildSingleton(cls: any): T { + /** + * 批量添加单例子实体 + * @param clss 单例子实体类数组 + */ + addChildSingletons(...clss: any[]) { + for (let ctor of clss) { + this.addChildSingleton(ctor); + } + } + + /** + * 添加单例子实体 + * @param cls 单例子实体类 + * @returns 单例子实体 + */ + addChildSingleton(cls: any): T { if (this.singletons == null) this.singletons = new Map(); if (this.singletons.has(cls)) { console.error(`${cls.name} 单例子实体已存在`); return null!; } - let entity = cls.create(); + let entity = ecs.getEntity(cls); this.singletons.set(cls, entity); this.addChild(entity); return entity as T; } - /** 获取单例子实体 */ - getChildSingleton(cls: any): T { + /** + * 获取单例子实体 + * @param cls 单例子实体类 + * @returns 单例子实体 + */ + getChildSingleton(cls: any): T { return this.singletons.get(cls) as T; }