Skip to content

Commit 52528ff

Browse files
committed
修复querysystem的rebuildindex方法
修复位掩码不一致问题 修复未注册组件的处理
1 parent 4a9317f commit 52528ff

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/ECS/Core/QuerySystem.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,15 @@ export class QuerySystem {
378378
this.entityIndex.byComponentType.clear();
379379
this.entityIndex.byTag.clear();
380380
this.entityIndex.byName.clear();
381+
382+
// 清理ArchetypeSystem和ComponentIndexManager
383+
this.archetypeSystem.clear();
384+
this.componentIndexManager.clear();
381385

382386
for (const entity of this.entities) {
383387
this.addEntityToIndexes(entity);
388+
this.componentIndexManager.addEntity(entity);
389+
this.archetypeSystem.addEntity(entity);
384390
}
385391

386392
this.indexDirty = false;
@@ -897,15 +903,25 @@ export class QuerySystem {
897903
* @returns 生成的位掩码
898904
*/
899905
private createComponentMask(componentTypes: ComponentType[]): bigint {
900-
// 使用位掩码优化器创建掩码
901-
const componentNames = componentTypes.map(type => type.name);
902-
903-
// 确保组件类型已注册到优化器
904-
for (const name of componentNames) {
905-
this.bitMaskOptimizer.registerComponentType(name);
906+
let mask = BigInt(0);
907+
let hasValidComponents = false;
908+
909+
for (const type of componentTypes) {
910+
try {
911+
const bitMask = ComponentRegistry.getBitMask(type);
912+
mask |= bitMask;
913+
hasValidComponents = true;
914+
} catch (error) {
915+
console.warn(`组件类型 ${type.name} 未注册,跳过`);
916+
}
906917
}
907-
908-
return this.bitMaskOptimizer.createCombinedMask(componentNames);
918+
919+
// 如果没有有效的组件类型,返回一个不可能匹配的掩码
920+
if (!hasValidComponents) {
921+
return BigInt(-1); // 所有位都是1,不可能与任何实体匹配
922+
}
923+
924+
return mask;
909925
}
910926

911927
/**

tests/ECS/Core/QuerySystem.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ describe('QuerySystem - 查询系统测试', () => {
260260
testEntities.push(entity);
261261
}
262262

263-
// 将实体添加到查询系统
264-
querySystem.setEntities([...entities, ...testEntities]);
265-
266-
// 添加组件
263+
// 先添加组件
267264
for (const entity of testEntities) {
268265
entity.addComponent(new PositionComponent(0, 0));
269266
entity.addComponent(new VelocityComponent(1, 1));
270267
}
268+
269+
// 将实体添加到查询系统
270+
querySystem.setEntities([...entities, ...testEntities]);
271271

272272
const startTime = performance.now();
273273
const result = querySystem.queryAll(PositionComponent, VelocityComponent);
@@ -317,10 +317,7 @@ describe('QuerySystem - 查询系统测试', () => {
317317
testEntities.push(entity);
318318
}
319319

320-
// 将实体添加到查询系统
321-
querySystem.setEntities([...entities, ...testEntities]);
322-
323-
// 随机分配组件
320+
// 先随机分配组件
324321
for (let i = 0; i < entityCount; i++) {
325322
const entity = testEntities[i];
326323

@@ -338,6 +335,9 @@ describe('QuerySystem - 查询系统测试', () => {
338335
entity.addComponent(new RenderComponent(true));
339336
}
340337
}
338+
339+
// 将实体添加到查询系统
340+
querySystem.setEntities([...entities, ...testEntities]);
341341

342342
const startTime = performance.now();
343343

0 commit comments

Comments
 (0)