移除bymask

This commit is contained in:
YHH
2025-09-30 17:01:49 +08:00
parent a5e70bcd99
commit 1ac0227c90
2 changed files with 20 additions and 32 deletions

View File

@@ -187,6 +187,24 @@ export class ArchetypeSystem {
return this._allArchetypes.slice(); return this._allArchetypes.slice();
} }
/**
* 获取包含指定组件类型的所有实体
*/
public getEntitiesByComponent(componentType: ComponentType): Entity[] {
const archetypes = this._componentToArchetypes.get(componentType);
if (!archetypes || archetypes.size === 0) {
return [];
}
const entities: Entity[] = [];
for (const archetype of archetypes) {
for (const entity of archetype.entities) {
entities.push(entity);
}
}
return entities;
}
/** /**
* 清空所有数据 * 清空所有数据
*/ */

View File

@@ -46,7 +46,6 @@ export interface QueryResult {
* 实体索引结构 * 实体索引结构
*/ */
interface EntityIndex { interface EntityIndex {
byMask: Map<string, Set<Entity>>;
byComponentType: Map<ComponentType, Set<Entity>>; byComponentType: Map<ComponentType, Set<Entity>>;
byTag: Map<number, Set<Entity>>; byTag: Map<number, Set<Entity>>;
byName: Map<string, Set<Entity>>; byName: Map<string, Set<Entity>>;
@@ -106,7 +105,6 @@ export class QuerySystem {
constructor() { constructor() {
this.entityIndex = { this.entityIndex = {
byMask: new Map(),
byComponentType: new Map(), byComponentType: new Map(),
byTag: new Map(), byTag: new Map(),
byName: new Map() byName: new Map()
@@ -287,13 +285,6 @@ export class QuerySystem {
* 将实体添加到各种索引中 * 将实体添加到各种索引中
*/ */
private addEntityToIndexes(entity: Entity): void { private addEntityToIndexes(entity: Entity): void {
const mask = entity.componentMask;
// 组件掩码索引
const maskKey = BitMask64Utils.toString(mask, 16);
const maskSet = this.entityIndex.byMask.get(maskKey) || this.createAndSetMaskIndex(maskKey);
maskSet.add(entity);
// 组件类型索引 - 批量处理预获取所有相关的Set // 组件类型索引 - 批量处理预获取所有相关的Set
const components = entity.components; const components = entity.components;
for (let i = 0; i < components.length; i++) { for (let i = 0; i < components.length; i++) {
@@ -317,12 +308,6 @@ export class QuerySystem {
} }
} }
private createAndSetMaskIndex(maskKey: string): Set<Entity> {
const set = new Set<Entity>();
this.entityIndex.byMask.set(maskKey, set);
return set;
}
private createAndSetComponentIndex(componentType: ComponentType): Set<Entity> { private createAndSetComponentIndex(componentType: ComponentType): Set<Entity> {
const set = new Set<Entity>(); const set = new Set<Entity>();
this.entityIndex.byComponentType.set(componentType, set); this.entityIndex.byComponentType.set(componentType, set);
@@ -345,18 +330,6 @@ export class QuerySystem {
* 从各种索引中移除实体 * 从各种索引中移除实体
*/ */
private removeEntityFromIndexes(entity: Entity): void { private removeEntityFromIndexes(entity: Entity): void {
const mask = entity.componentMask;
// 从组件掩码索引移除
const maskKey = BitMask64Utils.toString(mask, 16);
const maskSet = this.entityIndex.byMask.get(maskKey);
if (maskSet) {
maskSet.delete(entity);
if (maskSet.size === 0) {
this.entityIndex.byMask.delete(maskKey);
}
}
// 从组件类型索引移除 // 从组件类型索引移除
for (const component of entity.components) { for (const component of entity.components) {
const componentType = component.constructor as ComponentType; const componentType = component.constructor as ComponentType;
@@ -399,7 +372,6 @@ export class QuerySystem {
* 通常在大量实体变更后调用以确保索引一致性。 * 通常在大量实体变更后调用以确保索引一致性。
*/ */
private rebuildIndexes(): void { private rebuildIndexes(): void {
this.entityIndex.byMask.clear();
this.entityIndex.byComponentType.clear(); this.entityIndex.byComponentType.clear();
this.entityIndex.byTag.clear(); this.entityIndex.byTag.clear();
this.entityIndex.byName.clear(); this.entityIndex.byName.clear();
@@ -954,7 +926,6 @@ export class QuerySystem {
public getStats(): { public getStats(): {
entityCount: number; entityCount: number;
indexStats: { indexStats: {
maskIndexSize: number;
componentIndexSize: number; componentIndexSize: number;
tagIndexSize: number; tagIndexSize: number;
nameIndexSize: number; nameIndexSize: number;
@@ -980,7 +951,6 @@ export class QuerySystem {
return { return {
entityCount: this.entities.length, entityCount: this.entities.length,
indexStats: { indexStats: {
maskIndexSize: this.entityIndex.byMask.size,
componentIndexSize: this.entityIndex.byComponentType.size, componentIndexSize: this.entityIndex.byComponentType.size,
tagIndexSize: this.entityIndex.byTag.size, tagIndexSize: this.entityIndex.byTag.size,
nameIndexSize: this.entityIndex.byName.size nameIndexSize: this.entityIndex.byName.size