123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // 战斗数据节点池
- export class BattleDataPool<T> {
- 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 = [];
- }
- }
|