
import { IObjectPoolManager } from "./IObjectPoolManager";
/**
 * 全局对象池管理
 */
export class PoolManager implements IObjectPoolManager {
    public _poolMap: Map<string, cc.NodePool> = new Map<string, cc.NodePool>();

    GetCount(): number {
        throw new Error("Method not implemented.");
    }

    CreatePool(poolName: string): cc.NodePool {
        if (this._poolMap[poolName]) {
            console.warn('对象池已经存在，无需重复创建!');
            return null;
        }
        let pool = new cc.NodePool();
        this._poolMap[poolName] = pool;
        return pool;
    }

    CreatePrePool(poolName: string, prefab: cc.Prefab, maxNum: number): cc.NodePool {
        if (this._poolMap[poolName]) {
            console.warn('对象池已经存在，无需重复创建!');
            return null;
        }
        let pool = new cc.NodePool();
        this._poolMap[poolName] = pool;
        for (let i = 0; i < maxNum; i++) {
            let node = cc.instantiate(prefab);
            pool.put(node);
        }
    }

    GetPool(poolName: string): cc.NodePool {
        if (this._poolMap[poolName]) {
            return this._poolMap[poolName];
        } else {
            return this.CreatePool(poolName);
        }
    }

    ReleasePool(poolName: string) {
        if (this._poolMap[poolName]) {
            this._poolMap[poolName].clear();
        }
        // else {
        //     console.error(`对象池${poolName}不存在`);
        // }
    }

    ReleaseAllPool() {
        for (let key in this._poolMap) {
            this._poolMap[key].clear();
        }
    }

}