import { ResourcesPath, AssetManager } from "../Tools/AssetManager";
import { DispatcherControl, DispatcherState } from "../Tools/DispatcherControl";
import { CameraControl } from "../Main/CameraControl";


export class LoadView{
    private static ins:LoadView;
    public mainscene:Laya.Scene3D;
    public maincamera:Laya.Camera;
    constructor(){
        LoadView.ins = this;
        new CameraControl(); 
        this.Init();
    }
    public static get Instance():LoadView{
        if(LoadView.ins){
            return LoadView.ins
        }
        else{
            return new LoadView();
        }
    }
    Init(){
        this.LoadScene();
        // this.LoadZombie();
    }
    //加载场景
    LoadScene(){
        Laya.Scene3D.load(ResourcesPath.MainScene,Laya.Handler.create(this,(scene)=>{
            this.mainscene = scene;
            this.maincamera = this.mainscene.getChildByName("Main Camera") as Laya.Camera;
            console.log(this.maincamera);
            // this.maincamera.transform.position = new Laya.Vector3(19.5,100,16.7);
            // console.log(this.maincamera.transform.position);
            // console.log(this.maincamera.transform.rotationEuler);
            // this.maincamera.transform.rotationEuler = new Laya.Vector3(90,0,0);
            Laya.stage.addChild(this.mainscene);
            console.log("场景加载完毕--");
            this.LoadZombie();
        }));
        
    }
    //加载僵尸
    LoadZombie(){
        Laya.loader.create(ResourcesPath.Zombie,Laya.Handler.create(this,(zombie)=>{
            AssetManager.Instance.Zombie.set(1,zombie);
        }));
        console.log("僵尸模型加载完毕--");
        this.LoadPolice();
    }
    //加载警察
    LoadPolice(){
        Laya.loader.create(ResourcesPath.Police,Laya.Handler.create(this,(police)=>{
            AssetManager.Instance.Police.set(1,police);
            console.log("警察模型加载完毕--");
            this.LoadPeople();
        }));
        
    }
    //加载平民
    LoadPeople(){
        var path = [];
        for (let i = 0; i < ResourcesPath.People.length; i++) {
            path.push(ResourcesPath.People[i]);
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>
        {
            for(var i = 0;i < ResourcesPath.People.length;i++)
            {
                var people = Laya.loader.getRes(ResourcesPath.People[i]);
                AssetManager.Instance.People.set(i + 1,people);
                Laya.loader.clearRes(ResourcesPath.People[i]);
            }
            console.log("平民模型加载完毕--");
            DispatcherControl.DispatcherTrigger(DispatcherState.LoadComplete);
        }))
        
    }
}