// 战斗数据节点池 export class BattleDataPool { private pool: T[] = []; private creator: new () => T; private maxSize: number; constructor(creator: new()=>T,maxSize: number) { if(this.creator == null){ this.creator = creator } this.maxSize = maxSize; } // 从池中获取对象 public getObject(): T | null { if (this.pool.length > 0) { return this.pool.pop()!; } else if (this.pool.length < this.maxSize) { return new this.creator(); } return null; // 池已满且没有可用对象 } // 将对象放回池中 public putObject(obj: T): void { if (this.pool.length < this.maxSize) { this.pool.push(obj); } } /** * 设置节点池大小 * @param size 大小 */ setPoolSize(size: number) { this.maxSize = size; } /** * 清理节点池 */ clear() { this.pool = []; } }