From e92c0040b5ee3c5e6651904ede10bbc859b04b1e Mon Sep 17 00:00:00 2001 From: MirageTank <1455496974@qq.com> Date: Tue, 30 Sep 2025 11:57:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=8E=9F=E5=9E=8B=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E4=BB=A5=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD=20-=20?= =?UTF-8?q?=E5=B0=86=E5=8E=9F=E5=9E=8BID=E7=B1=BB=E5=9E=8B=E4=BB=8E?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=94=B9=E4=B8=BABitMask64Data,?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=8B=BC=E6=8E=A5=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E7=9A=84=E6=80=A7=E8=83=BD=E6=8D=9F=E8=80=97,=E9=99=8D?= =?UTF-8?q?=E4=BD=8E=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8.=20-=20=E5=8E=9F?= =?UTF-8?q?=E5=9E=8BID=E7=94=9F=E6=88=90=E4=B8=8D=E5=86=8D=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E7=BB=84=E4=BB=B6=E5=90=8D=E7=A7=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/ECS/Core/ArchetypeSystem.ts | 74 +++++++++++-------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/packages/core/src/ECS/Core/ArchetypeSystem.ts b/packages/core/src/ECS/Core/ArchetypeSystem.ts index ed3a4710..4e615a1e 100644 --- a/packages/core/src/ECS/Core/ArchetypeSystem.ts +++ b/packages/core/src/ECS/Core/ArchetypeSystem.ts @@ -1,11 +1,11 @@ -import { Entity } from '../Entity'; -import { ComponentType } from './ComponentStorage'; -import { getComponentTypeName } from '../Decorators'; +import {Entity} from '../Entity'; +import {ComponentType} from './ComponentStorage'; +import {BitMask64Data, ComponentTypeManager} from "../Utils"; /** * 原型标识符 */ -export type ArchetypeId = string; +export type ArchetypeId = BitMask64Data; /** * 原型数据结构 @@ -36,7 +36,7 @@ export interface ArchetypeQueryResult { */ export class ArchetypeSystem { /** 所有原型的映射表 */ - private _archetypes = new Map(); + private _archetypes = new Map>(); /** 实体到原型的映射 */ private _entityToArchetype = new Map(); @@ -47,8 +47,8 @@ export class ArchetypeSystem { /** 实体组件类型缓存 */ private _entityComponentTypesCache = new Map(); - /** 原型ID缓存 */ - private _archetypeIdCache = new Map(); + /** 所有原型 */ + private _allArchetypes: Archetype[] = []; /** * 添加实体到原型系统 @@ -57,7 +57,7 @@ export class ArchetypeSystem { const componentTypes = this.getEntityComponentTypes(entity); const archetypeId = this.generateArchetypeId(componentTypes); - let archetype = this._archetypes.get(archetypeId); + let archetype = this.getArchetype(archetypeId); if (!archetype) { archetype = this.createArchetype(componentTypes); } @@ -103,16 +103,13 @@ export class ArchetypeSystem { return; } - const affectedComponentTypes = new Set(); - // 从旧原型中移除实体 if (currentArchetype) { currentArchetype.entities.delete(entity); - currentArchetype.componentTypes.forEach(type => affectedComponentTypes.add(type)); } // 获取或创建新原型 - let newArchetype = this._archetypes.get(newArchetypeId); + let newArchetype = this.getArchetype(newArchetypeId); if (!newArchetype) { newArchetype = this.createArchetype(newComponentTypes); } @@ -120,7 +117,6 @@ export class ArchetypeSystem { // 将实体添加到新原型 newArchetype.entities.add(entity); this._entityToArchetype.set(entity, newArchetype); - newComponentTypes.forEach(type => affectedComponentTypes.add(type)); // 更新组件索引 if (currentArchetype) { @@ -139,7 +135,7 @@ export class ArchetypeSystem { let totalEntities = 0; if (operation === 'AND') { - for (const archetype of this._archetypes.values()) { + for (const archetype of this._allArchetypes) { if (this.archetypeContainsAllComponents(archetype, componentTypes)) { matchingArchetypes.push(archetype); totalEntities += archetype.entities.size; @@ -180,7 +176,7 @@ export class ArchetypeSystem { * 获取所有原型 */ public getAllArchetypes(): Archetype[] { - return Array.from(this._archetypes.values()); + return this._allArchetypes.slice(); } /** @@ -191,9 +187,30 @@ export class ArchetypeSystem { this._entityToArchetype.clear(); this._componentToArchetypes.clear(); this._entityComponentTypesCache.clear(); - this._archetypeIdCache.clear(); + this._allArchetypes = []; } + /** + * 根据原型ID获取原型 + * @param archetypeId + * @private + */ + private getArchetype(archetypeId: ArchetypeId): Archetype | undefined { + return this._archetypes.get(archetypeId.hi)?.get(archetypeId.lo); + } + + /** + * 更新所有原型数组 + */ + private updateAllArchetypeArrays(): void { + this._allArchetypes = []; + for (const [, innerMap] of this._archetypes) { + for (const [, archetype] of innerMap) { + this._allArchetypes.push(archetype); + } + } + } + /** * 获取实体的组件类型列表 */ @@ -210,18 +227,8 @@ export class ArchetypeSystem { * 生成原型ID */ private generateArchetypeId(componentTypes: ComponentType[]): ArchetypeId { - // 创建缓存键 - const cacheKey = componentTypes - .map(type => getComponentTypeName(type)) - .sort() - .join('|'); - - let archetypeId = this._archetypeIdCache.get(cacheKey); - if (!archetypeId) { - archetypeId = cacheKey; - this._archetypeIdCache.set(cacheKey, archetypeId); - } - return archetypeId; + let entityBits = ComponentTypeManager.instance.getEntityBits(componentTypes); + return entityBits.getValue(); } /** @@ -235,8 +242,15 @@ export class ArchetypeSystem { componentTypes: [...componentTypes], entities: new Set() }; - - this._archetypes.set(id, archetype); + // 存储原型ID - 原型 + let archetypeGroup = this._archetypes.get(id.hi); + if (!archetypeGroup) { + archetypeGroup = new Map(); + this._archetypes.set(id.hi, archetypeGroup); + } + archetypeGroup.set(id.lo, archetype); + // 更新数组 + this.updateAllArchetypeArrays(); return archetype; }