修复queryall缓存信息错误问题

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

View File

@@ -421,25 +421,34 @@ 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) {
this.queryStats.indexHits++;
const indexResult = this.componentIndexManager.query(componentTypes[0]);
entities = Array.from(indexResult);
} else { } else {
const indexResult = this.componentIndexManager.queryMultiple(componentTypes, 'AND'); try {
entities = Array.from(indexResult); 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); this.addToCache(cacheKey, entities);
return { return {