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);
- }
- }
-
- setPoolSize(size: number) {
- this.maxSize = size;
- }
-
-
- clear() {
- this.pool = [];
- }
- }
|