import { _decorator, Component, Node, loader, Prefab, instantiate } from 'cc';
import { GameController } from '../GameController';
import { MonsterBase } from './MonsterBase';
import { MonsterHP } from './MonsterHP';
const { ccclass, property } = _decorator;

export class MonsterHPUIPool extends Component {
    
    private static pool:MonsterHP[]=[];

    /**
     * 创建
     */
    public static Create():MonsterHP{
        let monsterHPView:MonsterHP;
        // if(this.pool.length>0){
        //     monsterHPView=this.pool.shift();
        //     // GameController.single.monsterHPUILayer.addChild(monsterHPView.node);
        //     monsterHPView.node.active=true;
        //     return monsterHPView
        // }
        let prefab:Prefab=loader.getRes("ui/components/MonsterHPUI");
        let viewNode:Node=instantiate(prefab);
        monsterHPView=viewNode.addComponent(MonsterHP);
        GameController.single.monsterHPUILayer.addChild(viewNode);
        return monsterHPView;
    }

    public static Recycle(monsterHPView:MonsterHP):void{
        GameController.single.monsterHPUILayer.removeChild(monsterHPView.node);
        monsterHPView.node.destroy();
        // monsterHPView.monster=null;
        // if(this.pool.indexOf(monsterHPView)>=0){
        //     throw new Error("重复回收")
        // }
        // this.pool.push(monsterHPView);
    }
}
