修复queryall缓存信息错误问题

This commit is contained in:
YHH
2025-07-02 23:47:30 +08:00
parent bb19f752a1
commit a37183851f

View File

@@ -421,16 +421,17 @@ export class QuerySystem {
}; };
} }
let entities: Entity[]; let entities: Entity[] = [];
const archetypeResult = this.archetypeSystem.queryArchetypes(componentTypes, 'AND'); const archetypeResult = this.archetypeSystem.queryArchetypes(componentTypes, 'AND');
if (archetypeResult.archetypes.length > 0) { if (archetypeResult.archetypes.length > 0) {
this.queryStats.archetypeHits++; this.queryStats.archetypeHits++;
entities = [];
for (const archetype of archetypeResult.archetypes) { for (const archetype of archetypeResult.archetypes) {
entities.push(...archetype.entities); entities.push(...archetype.entities);
} }
} else if (componentTypes.length === 1) { } else {
try {
if (componentTypes.length === 1) {
this.queryStats.indexHits++; this.queryStats.indexHits++;
const indexResult = this.componentIndexManager.query(componentTypes[0]); const indexResult = this.componentIndexManager.query(componentTypes[0]);
entities = Array.from(indexResult); entities = Array.from(indexResult);
@@ -438,8 +439,16 @@ export class QuerySystem {
const indexResult = this.componentIndexManager.queryMultiple(componentTypes, 'AND'); const indexResult = this.componentIndexManager.queryMultiple(componentTypes, 'AND');
entities = Array.from(indexResult); 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); this.addToCache(cacheKey, entities);
return { return {