import Dictionary from "../GameUtils/Dictionary";
import { LevelManager } from "../Manager/LevelManager";
import { CameraManager } from "../Manager/CameraManager";
import { DispatcherManager, EventState } from "../GameUtils/EventManager";
import { ViewManager, ViewType } from "../View/ViewManager";
import { NodeUtil } from "../GameUtils/NodeUtil";

export class AssetManager{
    private static ins:AssetManager;
    constructor(){
        AssetManager.ins = this;
        DispatcherManager.EventOn(EventState.LoadComplete,this,this.Init);
    }
    public static get Instance():AssetManager{
        if(AssetManager.ins == null){
            return new AssetManager();
        }
        return AssetManager.ins;
    }
    public Prop:Dictionary = new Dictionary();
    public Character:Dictionary = new Dictionary();
    public mainscene:Laya.Scene3D;
    public maincamera:Laya.Camera;
    public root:Laya.Sprite3D;

    public curLevel:Laya.Sprite3D;
    public playerpos:Laya.Sprite3D;

    Init(){
        Laya.stage.addChild(this.mainscene);
        this.maincamera = this.mainscene.getChildByName("Main Camera") as Laya.Camera;
        this.maincamera.enableHDR = false;
        CameraManager.Instance.camera = this.maincamera;
        let _sprite = new Laya.Sprite3D();
        this.mainscene.addChild(_sprite);
        LevelManager.Instance.spritepool = _sprite;
        this.SetCharacterPos();
        LevelManager.Instance.RegEvent();
        LevelManager.Instance.GameStart();
        ViewManager.Instance.ShowView(ViewType.HomeView);
    }
    /**传入名字获取预制 */
    public getPref(name: string): Laya.Sprite3D {
        return this.Prop.get<Laya.Sprite3D>(name);
    }
    /**传入序号获取人物预制 */
    public getRolePref(index:number):Laya.Sprite3D{
        return this.Character.get<Laya.Sprite3D>(index);
    }
    public SetCharacterPos(){
        let _level = this.mainscene.getChildByName("level1") as Laya.Sprite3D;
        for (let i = 0; i < _level.numChildren; i++) {
            let item = _level.getChildAt(i) as Laya.Sprite3D;
            if(item.name == "player"){
                this.playerpos = item;
                // let _pref = this.getRolePref(1) as Laya.Sprite3D;
                // let _player = Laya.Sprite3D.instantiate(_pref);
                // LevelManager.Instance.spritepool.addChild(_player);
                // _player.transform.position = item.transform.position.clone();
                // _player.transform.rotation = item.transform.rotation.clone();
            }
            else if(item.name == "role"){
                let _pref = this.getRolePref(1) as Laya.Sprite3D;
                let _player = Laya.Sprite3D.instantiate(_pref);
                LevelManager.Instance.spritepool.addChild(_player);
                _player.transform.position = item.transform.position.clone();
                _player.transform.rotation = item.transform.rotation.clone();

                NodeUtil.SetColliderGroup(_player,100);
            }
        }
    }
}