实体存储和管理服务

This commit is contained in:
YHH
2025-10-14 23:31:09 +08:00
parent 85dad41e60
commit 1cf5641c4c
8 changed files with 492 additions and 11 deletions

View File

@@ -0,0 +1,78 @@
import { Injectable, IService, Entity } from '@esengine/ecs-framework';
import { MessageHub } from './MessageHub';
export interface EntityTreeNode {
entity: Entity;
children: EntityTreeNode[];
parent: EntityTreeNode | null;
}
/**
* 管理编辑器中的实体状态和选择
*/
@Injectable()
export class EntityStoreService implements IService {
private entities: Map<number, Entity> = new Map();
private selectedEntity: Entity | null = null;
private rootEntities: Set<number> = new Set();
constructor(private messageHub: MessageHub) {}
public dispose(): void {
this.entities.clear();
this.rootEntities.clear();
this.selectedEntity = null;
}
public addEntity(entity: Entity, parent?: Entity): void {
this.entities.set(entity.id, entity);
if (!parent) {
this.rootEntities.add(entity.id);
}
this.messageHub.publish('entity:added', { entity, parent });
}
public removeEntity(entity: Entity): void {
this.entities.delete(entity.id);
this.rootEntities.delete(entity.id);
if (this.selectedEntity?.id === entity.id) {
this.selectedEntity = null;
this.messageHub.publish('entity:selected', { entity: null });
}
this.messageHub.publish('entity:removed', { entity });
}
public selectEntity(entity: Entity | null): void {
this.selectedEntity = entity;
this.messageHub.publish('entity:selected', { entity });
}
public getSelectedEntity(): Entity | null {
return this.selectedEntity;
}
public getAllEntities(): Entity[] {
return Array.from(this.entities.values());
}
public getRootEntities(): Entity[] {
return Array.from(this.rootEntities)
.map(id => this.entities.get(id))
.filter((e): e is Entity => e !== undefined);
}
public getEntity(id: number): Entity | undefined {
return this.entities.get(id);
}
public clear(): void {
this.entities.clear();
this.rootEntities.clear();
this.selectedEntity = null;
this.messageHub.publish('entities:cleared', {});
}
}

View File

@@ -10,5 +10,6 @@ export * from './Plugins/EditorPluginManager';
export * from './Services/UIRegistry';
export * from './Services/MessageHub';
export * from './Services/SerializerRegistry';
export * from './Services/EntityStoreService';
export * from './Types/UITypes';