修复querysystem的rebuildindex方法
修复位掩码不一致问题 修复未注册组件的处理
This commit is contained in:
@@ -379,8 +379,14 @@ export class QuerySystem {
|
||||
this.entityIndex.byTag.clear();
|
||||
this.entityIndex.byName.clear();
|
||||
|
||||
// 清理ArchetypeSystem和ComponentIndexManager
|
||||
this.archetypeSystem.clear();
|
||||
this.componentIndexManager.clear();
|
||||
|
||||
for (const entity of this.entities) {
|
||||
this.addEntityToIndexes(entity);
|
||||
this.componentIndexManager.addEntity(entity);
|
||||
this.archetypeSystem.addEntity(entity);
|
||||
}
|
||||
|
||||
this.indexDirty = false;
|
||||
@@ -897,15 +903,25 @@ export class QuerySystem {
|
||||
* @returns 生成的位掩码
|
||||
*/
|
||||
private createComponentMask(componentTypes: ComponentType[]): bigint {
|
||||
// 使用位掩码优化器创建掩码
|
||||
const componentNames = componentTypes.map(type => type.name);
|
||||
let mask = BigInt(0);
|
||||
let hasValidComponents = false;
|
||||
|
||||
// 确保组件类型已注册到优化器
|
||||
for (const name of componentNames) {
|
||||
this.bitMaskOptimizer.registerComponentType(name);
|
||||
for (const type of componentTypes) {
|
||||
try {
|
||||
const bitMask = ComponentRegistry.getBitMask(type);
|
||||
mask |= bitMask;
|
||||
hasValidComponents = true;
|
||||
} catch (error) {
|
||||
console.warn(`组件类型 ${type.name} 未注册,跳过`);
|
||||
}
|
||||
}
|
||||
|
||||
return this.bitMaskOptimizer.createCombinedMask(componentNames);
|
||||
// 如果没有有效的组件类型,返回一个不可能匹配的掩码
|
||||
if (!hasValidComponents) {
|
||||
return BigInt(-1); // 所有位都是1,不可能与任何实体匹配
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -260,15 +260,15 @@ describe('QuerySystem - 查询系统测试', () => {
|
||||
testEntities.push(entity);
|
||||
}
|
||||
|
||||
// 将实体添加到查询系统
|
||||
querySystem.setEntities([...entities, ...testEntities]);
|
||||
|
||||
// 添加组件
|
||||
// 先添加组件
|
||||
for (const entity of testEntities) {
|
||||
entity.addComponent(new PositionComponent(0, 0));
|
||||
entity.addComponent(new VelocityComponent(1, 1));
|
||||
}
|
||||
|
||||
// 将实体添加到查询系统
|
||||
querySystem.setEntities([...entities, ...testEntities]);
|
||||
|
||||
const startTime = performance.now();
|
||||
const result = querySystem.queryAll(PositionComponent, VelocityComponent);
|
||||
const endTime = performance.now();
|
||||
@@ -317,10 +317,7 @@ describe('QuerySystem - 查询系统测试', () => {
|
||||
testEntities.push(entity);
|
||||
}
|
||||
|
||||
// 将实体添加到查询系统
|
||||
querySystem.setEntities([...entities, ...testEntities]);
|
||||
|
||||
// 随机分配组件
|
||||
// 先随机分配组件
|
||||
for (let i = 0; i < entityCount; i++) {
|
||||
const entity = testEntities[i];
|
||||
|
||||
@@ -339,6 +336,9 @@ describe('QuerySystem - 查询系统测试', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 将实体添加到查询系统
|
||||
querySystem.setEntities([...entities, ...testEntities]);
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
// 执行复杂查询
|
||||
|
||||
Reference in New Issue
Block a user