import { ResourcesPath, AssetManager } from "../Tools/AssetManager";
import { EventManager, EventState } from "../Tools/EventManager";
import { GameManager } from "../Game/GameManager";

export class LoadView{
    private static ins:LoadView;
    private scene:Laya.Scene3D;
    constructor(){
        LoadView.ins = this;
        this.Init();
    }
    public static get Instance():LoadView{
        if(LoadView.ins){
            return LoadView.ins
        }
        else{
            return new LoadView();
        }
    }
    Init(){
        new ResourcesPath();
        new GameManager();
        this.LoadScene();
    }
    LoadComplete(){
        console.log("加载完毕--")
        EventManager.EventTrigger(EventState.LoadComplete);
    }
    LoadScene(){
        // let abc = 0.15*18;
        // console.log("abc---",abc);
        Laya.Scene3D.load(ResourcesPath.MainScene,Laya.Handler.create(this,(scene)=>{
            this.scene = scene;
            AssetManager.Instance.mainscene = this.scene;
            console.log("场景加载完毕")
            this.LoadEnemy();
        }))
    }
    LoadEnemy(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Enemy.length; i++) {
            path.push(ResourcesPath.Enemy[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Enemy.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Enemy[i]);
                AssetManager.Instance.Enemy.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Enemy[i]);
            }
            console.log("敌人模型资源加载完毕---");
            this.LoadPlayer();
        }))
    }
    LoadPlayer(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Player.length; i++) {
            path.push(ResourcesPath.Player[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Player.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Player[i]);
                AssetManager.Instance.Player.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Player[i]);
            }
            console.log("玩家模型资源加载完毕---");
            this.LoadHorse();
        }))
    }
    LoadHorse(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Horse.length; i++) {
            path.push(ResourcesPath.Horse[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Horse.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Horse[i]);
                AssetManager.Instance.Horse.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Horse[i]);
            }
            console.log("马模型资源加载完毕---");
            this.LoadComplete();
        }))
    }

}