import { AssetPath } from "../Config/AssetPath";
import {PropManager} from "./PropManager";

export class CharacterPool
{
    static roledict:any = {};

    public static AddCharacter( resouceName:string,clas:any,parent:any ):Laya.Sprite3D
    {
        var skinnedmesh:Laya.Sprite3D;
        var arr:Array<Laya.Sprite3D> = CharacterPool.roledict[resouceName];
        if( arr && arr.length > 0 )
        {
            skinnedmesh = arr.shift();
            skinnedmesh.active = true;
            parent.addChild( skinnedmesh );
        }
        else
        {
            var sp:Laya.Sprite3D = Laya.Loader.getRes(AssetPath.get3DRoleModel(resouceName)) as Laya.Sprite3D;
            skinnedmesh = Laya.Sprite3D.instantiate(sp,parent, false,new Laya.Vector3( 0,0,0 )) as Laya.Sprite3D;
        }
        if(clas)
        {
            var cla:any = skinnedmesh.addComponent( clas );
            cla.resouseName = resouceName;
        }
        var _out:Laya.Quaternion = new Laya.Quaternion;
        Laya.Quaternion.createFromAxisAngle( new Laya.Vector3( 0,1,0 ),180*Math.PI/180,_out );
        skinnedmesh.transform.rotation = _out;
        return skinnedmesh;
    }

    public static ClearCharacter( sp:Laya.Sprite3D,resouceName:string,cla:Laya.Script3D ):void
    {
        if( !CharacterPool.roledict[resouceName] )
        {
            CharacterPool.roledict[resouceName] = [];
        }
        if(cla)
        {
            cla.destroy();
        }
        sp.active = false;
        sp.removeSelf();
        CharacterPool.roledict[resouceName].push( sp );
    }
}