import type Viewport from '@/core/engine/Viewport.ts' /** * 运行时管理器, 管理运行时的各种数据 */ export default class RuntimeManager { private viewport: Viewport // 临时存储的执行器ID集合 private readonly tmpExecutors = new Set // 货架ID -> 托盘ID private readonly storeRackMap = new Map>() init(viewport: Viewport): void { this.viewport = viewport } /** * 在指定的货架位置添加存储项 * @param rackId 货架ID * @param itemId 存储项ID */ addStoreAt(rackId: string, itemId: string): void { if (!this.storeRackMap.has(rackId)) { this.storeRackMap.set(rackId, new Set()) } this.storeRackMap.get(rackId).add(itemId) } /** * 从指定的货架位置移除存储项 * @param rackId 货架ID * @param itemId 存储项ID */ removeStoreAt(rackId: string, itemId: string): void { const rack = this.storeRackMap.get(rackId) if (rack) { rack.delete(itemId) if (rack.size === 0) { this.storeRackMap.delete(rackId) } } } /** * 获取指定货架位置的所有存储项 * @param rackId 货架ID */ getItemsByRack(rackId: string): Set | undefined { return this.storeRackMap.get(rackId) } /** * 移除指定货架 * @param rackId 货架ID */ removeRack(rackId: string): void { this.storeRackMap.delete(rackId) } /** * 清空所有存储项 */ clear() { for (const id of this.tmpExecutors) { this.viewport.entityManager.deleteEntityOnlyRuntime(id) } this.tmpExecutors.clear() this.storeRackMap.clear() } dispose(): void { this.viewport = null this.clear() } addEntity(item: ItemJson) { this.tmpExecutors.add(item.id) this.viewport.entityManager.createOrUpdateEntityOnlyRuntime(item) } removeEntity(itemId: string) { this.tmpExecutors.delete(itemId) // 从各个货架中移除 for (const [rackId, items] of this.storeRackMap.entries()) { if (items.has(itemId)) { items.delete(itemId) if (items.size === 0) { this.storeRackMap.delete(rackId) } } } this.viewport.entityManager.deleteEntityOnlyRuntime(itemId) } has(itemId: string) { return this.tmpExecutors.has(itemId) } }