修复QuerySystem中addEntities没有更新componentIndexManager/archetypeSystem索引
This commit is contained in:
@@ -191,6 +191,11 @@ export class QuerySystem {
|
|||||||
if (!existingIds.has(entity.id)) {
|
if (!existingIds.has(entity.id)) {
|
||||||
this.entities.push(entity);
|
this.entities.push(entity);
|
||||||
this.addEntityToIndexes(entity);
|
this.addEntityToIndexes(entity);
|
||||||
|
|
||||||
|
// 更新索引管理器
|
||||||
|
this.componentIndexManager.addEntity(entity);
|
||||||
|
this.archetypeSystem.addEntity(entity);
|
||||||
|
|
||||||
existingIds.add(entity.id);
|
existingIds.add(entity.id);
|
||||||
addedCount++;
|
addedCount++;
|
||||||
}
|
}
|
||||||
@@ -221,6 +226,10 @@ export class QuerySystem {
|
|||||||
// 批量更新索引
|
// 批量更新索引
|
||||||
for (const entity of entities) {
|
for (const entity of entities) {
|
||||||
this.addEntityToIndexes(entity);
|
this.addEntityToIndexes(entity);
|
||||||
|
|
||||||
|
// 更新索引管理器
|
||||||
|
this.componentIndexManager.addEntity(entity);
|
||||||
|
this.archetypeSystem.addEntity(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
|
|||||||
@@ -997,5 +997,56 @@ describe('QuerySystem - 查询系统测试', () => {
|
|||||||
expect(result.entities.length).toBe(1);
|
expect(result.entities.length).toBe(1);
|
||||||
expect(result.entities[0]).toBe(testEntity);
|
expect(result.entities[0]).toBe(testEntity);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('addEntities应该正确更新componentIndexManager和archetypeSystem', () => {
|
||||||
|
const querySystem = new QuerySystem();
|
||||||
|
|
||||||
|
// 创建带组件的实体
|
||||||
|
const entity1 = new Entity('BatchEntity1', 8001);
|
||||||
|
const entity2 = new Entity('BatchEntity2', 8002);
|
||||||
|
const entity3 = new Entity('BatchEntity3', 8003);
|
||||||
|
|
||||||
|
entity1.addComponent(new PositionComponent(10, 20));
|
||||||
|
entity2.addComponent(new PositionComponent(30, 40));
|
||||||
|
entity2.addComponent(new VelocityComponent(1, 1));
|
||||||
|
entity3.addComponent(new VelocityComponent(2, 2));
|
||||||
|
|
||||||
|
// 批量添加实体
|
||||||
|
querySystem.addEntities([entity1, entity2, entity3]);
|
||||||
|
|
||||||
|
// 测试基本查询
|
||||||
|
const positionResult = querySystem.queryAll(PositionComponent);
|
||||||
|
const velocityResult = querySystem.queryAll(VelocityComponent);
|
||||||
|
const bothResult = querySystem.queryAll(PositionComponent, VelocityComponent);
|
||||||
|
|
||||||
|
console.log('addEntities后Position查询结果:', positionResult.entities.length);
|
||||||
|
console.log('addEntities后Velocity查询结果:', velocityResult.entities.length);
|
||||||
|
console.log('addEntities后Both查询结果:', bothResult.entities.length);
|
||||||
|
|
||||||
|
// 验证查询结果是否正确
|
||||||
|
expect(positionResult.entities.length).toBe(2); // entity1, entity2
|
||||||
|
expect(velocityResult.entities.length).toBe(2); // entity2, entity3
|
||||||
|
expect(bothResult.entities.length).toBe(1); // 只有entity2
|
||||||
|
|
||||||
|
expect(positionResult.entities).toContain(entity1);
|
||||||
|
expect(positionResult.entities).toContain(entity2);
|
||||||
|
expect(velocityResult.entities).toContain(entity2);
|
||||||
|
expect(velocityResult.entities).toContain(entity3);
|
||||||
|
expect(bothResult.entities).toContain(entity2);
|
||||||
|
|
||||||
|
// 验证ArchetypeSystem是否正确工作
|
||||||
|
const entity1Archetype = querySystem.getEntityArchetype(entity1);
|
||||||
|
const entity2Archetype = querySystem.getEntityArchetype(entity2);
|
||||||
|
const entity3Archetype = querySystem.getEntityArchetype(entity3);
|
||||||
|
|
||||||
|
expect(entity1Archetype).toBeDefined();
|
||||||
|
expect(entity2Archetype).toBeDefined();
|
||||||
|
expect(entity3Archetype).toBeDefined();
|
||||||
|
|
||||||
|
// entity2应该有不同的archetype(因为有两个组件)
|
||||||
|
if (entity1Archetype && entity2Archetype) {
|
||||||
|
expect(entity1Archetype.id).not.toBe(entity2Archetype.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user