import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;    

export class modelConfig{
    constructor(model:Laya.Sprite3D){
        this.modelcur = model;
        this.modelPos = model.transform.position.clone();
        this.modelRot = model.transform.rotation.clone();
        this.modelSca = model.transform.scale.clone();
    }
    public modelcur:Laya.Sprite3D;
    public modelPos:Laya.Vector3;
    public modelRot:Laya.Quaternion;
    public modelSca:Laya.Vector3;
}
export default class GamePool  {
   //单例
   private static Ins:GamePool;
   //模型库
   private modelpool : {[Key:string]:Array<modelConfig> } = {};
   //最大存放数量
   public maxAcount = 30;
    constructor() {
        GamePool.Ins = this;
      }
    public static get Instance():GamePool{
        if(GamePool.Ins){
            return GamePool.Ins
        }
        else{
            return new GamePool();
        }
    }
/**取出模型*/
GetModel(_model:Laya.Sprite3D,title:string):Sprite3D{
   
        var poolname =title+ _model.name ;
        if(this.modelpool[poolname]!=null && this.modelpool[poolname] !=undefined){
            if(this.modelpool[poolname].length >0 ){
                var model1 = this.modelpool[poolname][0];
                this.modelpool[poolname].splice(0,1);
                if(model1.modelcur.destroyed){
                    model1.modelcur = Sprite3D.instantiate(_model);
                    if(!model1.modelcur.active){
                        model1.modelcur.active = true;
                    }
                }
                model1.modelcur.active = true;
                model1.modelcur.transform.position = _model.transform.position.clone();
                model1.modelcur.transform.rotation = _model.transform.rotation.clone();
                model1.modelcur.transform.scale = _model.transform.scale.clone();
                return model1.modelcur;
            }
            else{
                var model = Sprite3D.instantiate(_model);
                model.active = true;
                return model;
            }
        }
        else{
            var model = Sprite3D.instantiate(_model);
            model.active = true;
            return model;
        }
    }
/**回收模型*/
RecoveryModel(model:Sprite3D,title = "",modelstate = false){
        if(model.destroyed){
            return;
        }
        if(!modelstate && model.active == false){
            return;
        }
        var poolname =title+ model.name ; 
        if(this.modelpool[poolname] != null && this.modelpool[poolname] != undefined){
            var modelnum = this.modelpool[poolname];
            if( modelnum.length>this.maxAcount){
                model.destroy();
                return;
            }
            else{
                modelnum.push(new modelConfig(model));
            }
        }
        else{
            modelnum = [];
            modelnum.push(new modelConfig(model));
            this.modelpool[poolname] = modelnum;
        }
        model.transform.position = new Vector3(0,0,-10000);
        model.removeSelf();
        model.active = false;
    }
/**重置模型库*/
ResetsModelPool(){
        for(var Key in this.modelpool){
            var model = this.modelpool[Key];
            for (var i = 0; i < model.length; i++) {
                model[i].modelcur.destroy();
            }
        }
        this.modelpool = {};
    }
}