import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
import { box } from './box';
import { coin } from './coin';
import { wall } from './wall';
const { ccclass, property } = _decorator;

@ccclass('pool')
export class pool extends Component {
    @property(Prefab) box_pf:Prefab = null;
    @property(Prefab) coin_pf:Prefab = null;
    @property(Prefab) wall_pf:Prefab = null;

    private box_pool:NodePool = null;
    private coin_pool:NodePool = null;
    private wall_pool:NodePool = null;
    private pool_num:number = 100;
    public init(){
        if(this.box_pool==null){
            this.box_pool = new NodePool(box);
            this.coin_pool = new NodePool(coin);
            this.wall_pool = new NodePool(wall);
            for (let index = 0; index < this.pool_num; index++) {
           
                this.box_pool.put(instantiate(this.box_pf));
    
                this.coin_pool.put(instantiate(this.coin_pf));
    
                this.wall_pool.put(instantiate(this.wall_pf));
            }

            // this.schedule(()=>{
            //     if(this.box_pool.size()<this.pool_num){
            //         let box = instantiate(this.box_pf);
            //         this.box_pool.put(box);
            //     }
            //     if(this.coin_pool.size()<this.pool_num){
            //         const coin = instantiate(this.coin_pf);
            //         this.coin_pool.put(coin);
            //     }
            //     if(this.wall_pool.size()<this.pool_num){
            //         const wall = instantiate(this.wall_pf);
            //         this.wall_pool.put(wall);
            //     }
            // },0.5)
        }
    }

    public getBoxPool():NodePool{
        return this.box_pool
    }

    public getCoinPool():NodePool{
        return this.coin_pool
    }

    public getWallPool():NodePool{
        return this.wall_pool
    }

    public putBox(node:Node){
        this.box_pool.put(node)
    }
    public putCoin(node:Node){
        this.coin_pool.put(node)
    }
    public putWall(node:Node){
        this.wall_pool.put(node)
    }
    public getBox(){
        if(this.box_pool.size()<=0){
            let node = instantiate(this.box_pf)
            this.box_pool.put(node)
        }
        return this.box_pool.get()
    }

    public getCoin(){
        if(this.coin_pool.size()<=0){
            let node = instantiate(this.coin_pf)
            this.coin_pool.put(node)
        }
        return this.coin_pool.get()
    }

    public getWall(){
        if(this.wall_pool.size()<=0){
            let node = instantiate(this.wall_pf)
            this.wall_pool.put(node)
        }
        return this.wall_pool.get()
    }
}


