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 {