From 9603c6423b19a64dd828dff794f8321d27739c51 Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Fri, 26 Sep 2025 09:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E9=81=BF=E5=85=8D=E5=9C=A8?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E5=B8=A7=E5=86=85=E8=BF=9B=E8=A1=8C=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1query=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/ECS/Systems/EntitySystem.ts | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/core/src/ECS/Systems/EntitySystem.ts b/packages/core/src/ECS/Systems/EntitySystem.ts index b7194d90..3aacae2e 100644 --- a/packages/core/src/ECS/Systems/EntitySystem.ts +++ b/packages/core/src/ECS/Systems/EntitySystem.ts @@ -49,12 +49,13 @@ export abstract class EntitySystem implements ISystemBase { private _matcher: Matcher; private _trackedEntities: Set = new Set(); private _eventListeners: EventListenerRecord[] = []; + private _frameEntities: Entity[] | null = null; /** - * 获取系统处理的实体列表(动态查询) + * 获取系统处理的实体列表 */ public get entities(): readonly Entity[] { - return this.queryEntities(); + return this._frameEntities || []; } /** @@ -381,8 +382,6 @@ export abstract class EntitySystem implements ISystemBase { /** * 更新系统 - * - * 在每帧调用,处理系统的主要逻辑。 */ public update(): void { if (!this._enabled || !this.onCheckProcessing()) { @@ -394,11 +393,11 @@ export abstract class EntitySystem implements ISystemBase { try { this.onBegin(); - // 动态查询实体并处理 - const entities = this.queryEntities(); - entityCount = entities.length; + // 查询实体并存储到帧缓存中 + this._frameEntities = this.queryEntities(); + entityCount = this._frameEntities.length; - this.process(entities); + this.process(this._frameEntities); } finally { this._performanceMonitor.endMonitoring(this._systemName, startTime, entityCount); } @@ -406,8 +405,6 @@ export abstract class EntitySystem implements ISystemBase { /** * 后期更新系统 - * - * 在所有系统的update方法执行完毕后调用。 */ public lateUpdate(): void { if (!this._enabled || !this.onCheckProcessing()) { @@ -418,13 +415,15 @@ export abstract class EntitySystem implements ISystemBase { let entityCount = 0; try { - // 动态查询实体并处理 - const entities = this.queryEntities(); + // 使用缓存的实体列表,避免重复查询 + const entities = this._frameEntities || []; entityCount = entities.length; this.lateProcess(entities); this.onEnd(); } finally { this._performanceMonitor.endMonitoring(`${this._systemName}_Late`, startTime, entityCount); + // 清理帧缓存 + this._frameEntities = null; } }