From a37183851fde0d550af0e54e11004d962a19c70a Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Wed, 2 Jul 2025 23:47:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dqueryall=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ECS/Core/QuerySystem.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ECS/Core/QuerySystem.ts b/src/ECS/Core/QuerySystem.ts index d7924233..be983988 100644 --- a/src/ECS/Core/QuerySystem.ts +++ b/src/ECS/Core/QuerySystem.ts @@ -421,25 +421,34 @@ export class QuerySystem { }; } - let entities: Entity[]; + let entities: Entity[] = []; const archetypeResult = this.archetypeSystem.queryArchetypes(componentTypes, 'AND'); if (archetypeResult.archetypes.length > 0) { this.queryStats.archetypeHits++; - entities = []; for (const archetype of archetypeResult.archetypes) { entities.push(...archetype.entities); } - } else if (componentTypes.length === 1) { - this.queryStats.indexHits++; - const indexResult = this.componentIndexManager.query(componentTypes[0]); - entities = Array.from(indexResult); } else { - const indexResult = this.componentIndexManager.queryMultiple(componentTypes, 'AND'); - entities = Array.from(indexResult); + try { + if (componentTypes.length === 1) { + this.queryStats.indexHits++; + const indexResult = this.componentIndexManager.query(componentTypes[0]); + entities = Array.from(indexResult); + } else { + const indexResult = this.componentIndexManager.queryMultiple(componentTypes, 'AND'); + entities = Array.from(indexResult); + } + } catch (error) { + entities = []; + } + } + + if (entities.length === 0 && this.entities.length > 0) { + this.queryStats.linearScans++; + entities = this.queryByLinearScan(componentTypes); } - // 缓存结果 this.addToCache(cacheKey, entities); return {