import { _decorator, Component, director } from 'cc';
import { GlobalAudio } from '../../GlobalScript/GlobalAudio/GlobalAudio';
import { ImagePack } from '../../GlobalScript/ImagePack/ImagePack';
import { GameScript } from '../../GlobalScript/GameScript/GameScript';


const { ccclass, property } = _decorator;


@ccclass('LodingLogic')
export class LodingLogic extends Component {

    //Logo播放完成状态
    LogoFlag = true;
    //InitFlag
    InitFlag = false;
    //音频管理器初始化状态
    AudioManage: boolean = false;
    //NPK管理器初始化状态
    NpkManage: boolean = false;
    //Pvf管理器初始化状态
    PvfManage: boolean = false;

    NpkImgCount = 0
    NpkImgLoadCount = 0
    NpkInitFlag = false;

    start() {
        //初始化音频Map
        GlobalAudio.getInstance().Init(() => {
            this.AudioManage = true;
        });
        //初始化Npk
        ImagePack.getInstance().init(() => {
            this.NpkManage = true;

            this.NpkImgCount = ImagePack.getInstance().ReadNpkFile("!幻想模拟战", (img) => {
                this.NpkImgLoadCount++;
                if (this.NpkImgLoadCount >= this.NpkImgCount) this.NpkInitFlag = true;
            });
        });
        //初始化Pvf
        GameScript.getInstance().Init(() => {
            this.PvfManage = true;
        });


    }

    CheckInitState() {
        if (!this.NpkManage
            || !this.PvfManage)
            return false;
        else return true;
    }



    update(deltaTime: number) {
        if (this.CheckInitState() && !this.InitFlag && this.LogoFlag && this.NpkInitFlag) {

            this.scheduleOnce(function () {
                // director.loadScene("LoginGame");

                director.preloadScene("main", () => {
                    director.loadScene("main");
                });
            }, 0);

            this.InitFlag = true;
        } else {
            //每帧调用
            GameScript.getInstance().Update();

        }
    }
}


