import Singleton from '../Base/Singleton' import { instantiate, Node } from 'cc' import { EntityTypeEnum } from '../Enum' import DataManager from './DataManager' export default class ObjectPoolManager extends Singleton { static get Instance() { return super.GetInstance() } private objectPool: Node = null private map: Map = new Map() private getContainerName(objectName: EntityTypeEnum) { return objectName + 'Pool' } get(objectName: EntityTypeEnum) { if (this.objectPool === null) { this.objectPool = new Node("ObjectPool") this.objectPool.setParent(DataManager.Instance.stage) } if (!this.map.has(objectName)) { this.map.set(objectName, []) const container = new Node(this.getContainerName(objectName)) container.setParent(this.objectPool) } let node: Node const nodes = this.map.get(objectName) if (!nodes.length) { const prefab = DataManager.Instance.prefabMap.get(objectName) node = instantiate(prefab) node.name = objectName node.setParent(this.objectPool.getChildByName(this.getContainerName(objectName))) } else { node = nodes.pop() } node.active = true return node } ret(object: Node) { object.active = false const objectName = object.name as EntityTypeEnum this.map.get(objectName).push(object) } }