From c0fc10ef592fa27ca9d66d8f2ac35ad53b0beec8 Mon Sep 17 00:00:00 2001 From: dgflash Date: Tue, 22 Feb 2022 21:56:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=80=92=E8=AE=A1=E6=97=B6?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=9C=A8=E6=97=B6=E9=97=B4=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98=EF=BC=8C=E6=89=A9?= =?UTF-8?q?=E5=B1=95ECS=E8=8E=B7=E5=8F=96=E6=9C=89eid=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/resources/gui/prefab/demo.prefab | 116 +++++------------- .../core/common/manager/TimerManager.ts | 4 +- assets/script/core/gui/label/LabelTime.ts | 1 + assets/script/core/libs/ECS.ts | 19 ++- .../script/game/account/bll/AccountNetData.ts | 4 +- assets/script/game/role/Role.ts | 4 +- 6 files changed, 53 insertions(+), 95 deletions(-) diff --git a/assets/resources/gui/prefab/demo.prefab b/assets/resources/gui/prefab/demo.prefab index 16bff7f..5aaa802 100644 --- a/assets/resources/gui/prefab/demo.prefab +++ b/assets/resources/gui/prefab/demo.prefab @@ -343,9 +343,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -508,9 +506,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -715,9 +711,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -880,9 +874,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1087,9 +1079,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1252,9 +1242,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1459,9 +1447,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1624,9 +1610,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1831,9 +1815,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -1996,9 +1978,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -2203,9 +2183,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -2368,9 +2346,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -2480,9 +2456,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -2632,9 +2606,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -2768,9 +2740,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3023,9 +2993,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3188,9 +3156,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3373,9 +3339,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3538,9 +3502,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3723,9 +3685,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -3888,9 +3848,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4073,9 +4031,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4238,9 +4194,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4423,9 +4377,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4588,9 +4540,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4773,9 +4723,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -4938,9 +4886,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -5050,9 +4996,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, @@ -5139,9 +5083,7 @@ }, { "__type__": "cc.PrefabInfo", - "root": { - "__id__": 1 - }, + "root": null, "asset": { "__id__": 0 }, diff --git a/assets/script/core/common/manager/TimerManager.ts b/assets/script/core/common/manager/TimerManager.ts index 3503c0a..082e1e3 100644 --- a/assets/script/core/common/manager/TimerManager.ts +++ b/assets/script/core/common/manager/TimerManager.ts @@ -26,7 +26,7 @@ export class TimerManager extends EventDispatcher { constructor(component: Component) { super(); this.component = component; - this.schedule(this.onUpdate, 1); + this.schedule(this.onUpdate.bind(this), 1); } /** * 设置服务器时间与本地时间间隔 @@ -154,7 +154,7 @@ export class TimerManager extends EventDispatcher { /** 触发倒计时完成事件 */ private timerComplete(data: any) { if (data.onComplete) data.onComplete.call(data.object); - if (data.event) this.dispatchEvent(data.event); + if (data.event) this.dispatchEvent(data.event); } /** 注册指定对象的倒计时属性更新 */ diff --git a/assets/script/core/gui/label/LabelTime.ts b/assets/script/core/gui/label/LabelTime.ts index 643e5b3..42e3614 100644 --- a/assets/script/core/gui/label/LabelTime.ts +++ b/assets/script/core/gui/label/LabelTime.ts @@ -130,6 +130,7 @@ export default class LabelTime extends Label { } private onComplete() { + this.format(); if (this.complete) this.complete(this.node); } } diff --git a/assets/script/core/libs/ECS.ts b/assets/script/core/libs/ECS.ts index 6141d0e..f15ad3e 100644 --- a/assets/script/core/libs/ECS.ts +++ b/assets/script/core/libs/ECS.ts @@ -148,6 +148,23 @@ export module ecs { */ let eid = 1; + /** 扩展:获取带 eid 自增量的实体(继承Entity方式的编码风格,可减少一定代码量) */ + export function getEntity(ctor: any): T { + let entity: any = entityPool.pop(); + if (!entity) { + entity = new ctor(); + entity.eid = eid++; // 实体id也是有限的资源 + } + + if (entity.init) + entity.init(); + else + console.error(`${ctor.name} 实体缺少 init 方法初始化默认组件`); + + eid2Entity.set(entity.eid, entity); + return entity as T; + } + /** * 创建实体 */ @@ -592,7 +609,7 @@ export module ecs { } /** - * 销毁实体,实体会被回收到实体缓存池中。 + * 销毁实体,实体会被回收到实体缓存池中。 扩展:isClearData参数 * @param isClearData 是否清除组件上的数据 */ destroy(isClearData: boolean = true) { diff --git a/assets/script/game/account/bll/AccountNetData.ts b/assets/script/game/account/bll/AccountNetData.ts index 7424d86..5455851 100644 --- a/assets/script/game/account/bll/AccountNetData.ts +++ b/assets/script/game/account/bll/AccountNetData.ts @@ -7,9 +7,9 @@ import { Message } from "../../../core/common/event/MessageManager"; import { storage } from "../../../core/common/storage/SqlUtil"; -import { oops } from "../../../core/Oops"; import { ecs } from "../../../core/libs/ECS"; import { VM } from "../../../core/libs/model-view/ViewModel"; +import { oops } from "../../../core/Oops"; import { GameEvent } from "../../common/config/GameEvent"; import { netConfig } from "../../common/net/NetConfig"; import { Role } from "../../role/Role"; @@ -70,7 +70,7 @@ export class AccountNetDataSystem extends ecs.ComblockSystem implements ecs.IEnt /** 创建角色对象(自定义逻辑) */ private createRole(e: Account, data: any) { - var role = new Role(); + var role = ecs.getEntity(Role); // 角色数据 role.RoleModel.id = data.id; diff --git a/assets/script/game/role/Role.ts b/assets/script/game/role/Role.ts index c0fb760..0fb1aac 100644 --- a/assets/script/game/role/Role.ts +++ b/assets/script/game/role/Role.ts @@ -44,9 +44,7 @@ export class Role extends ecs.Entity { RoleView!: RoleViewComp; RoleViewInfo!: RoleViewInfoComp; - constructor() { - super(); - + protected init() { // 初始化实体常住 ECS 组件,定义实体特性 this.addComponents( RoleModelComp,