// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import LoadingView from "../loadingView/LoadingView";

const {ccclass, property} = cc._decorator;


export default class SceneManager{

    /**
     * 场景资源
     */
    public AssetBundle:cc.AssetManager.Bundle;

    constructor(){
        
    }

    /**
     * 切换场景
     * @param sceneName
     */
    public Swicth(sceneName:string):void{
        LoadingView.single.Show();
        this.AssetBundle.loadScene(sceneName,null,this.OnProgressHandler,this.OnCompleteHandler);
    }

    private OnProgressHandler(finish: number, total: number, item: cc.AssetManager.RequestItem):void{
        LoadingView.single.UpdateProgress(finish/total);
    }

    private OnCompleteHandler(error: Error, sceneAsset: cc.SceneAsset):void{
        cc.director.runScene(sceneAsset);
        LoadingView.single.Hide();
    }

    private static instance:SceneManager;
    public static get single(){
        if(this.instance==null){
            this.instance=new SceneManager();
        }
        return this.instance;
    }
}
