Launcher.ts 3.15 KB
import { GameConfig } from "../GameConfig";
import { UIManager } from "../common/gameplay/managers/UIManager";
import { AnimationUtils } from "../common/utils/AnimationUtils";
import { PlotManager } from "../avg/PlotManager";
import { SDK } from "simba-sdk";
import { GameModelManager } from "./model/GameModelManager";
import MainViewPresenter from "./ui/presenter/MainViewPresenter";
import { DummyLabel, DummyNode, DummyProgressBar } from "../common/CCDummyObjects";
import { AudioManager } from "../common/gameplay/managers/AudioManager";
import { ResUtils } from "../common/utils/ResUtils";

const { ccclass, property } = cc._decorator;


@ccclass
export default class Launcher extends cc.Component {
    @property({ type: cc.ProgressBar, displayName: "进度条" })
    progressBar: cc.ProgressBar = DummyProgressBar;
    @property({ type: cc.Label, displayName: "进度展示文本" })
    progressLabel: cc.Label = DummyLabel;

    @property({ type: cc.Node, displayName: "UI根结点" })
    uiRootNode: cc.Node = DummyNode;
    @property({ type: cc.Node, displayName: "引导根结点" })
    guideRootNode: cc.Node = DummyNode;
    @property({ type: cc.Node, displayName: '转场根节点' })
    VerbRoot: cc.Node = DummyNode;

    onLoad() {
        let winSize = cc.winSize;
        if (winSize.width / winSize.height > 720 / 1280) {
            let canvas = this.node.getComponent(cc.Canvas);
            canvas.fitWidth = true;
            canvas.fitHeight = true;
        }
        //关闭fps展示
        cc.debug.setDisplayStats(GameConfig.showFPS);
        cc.macro.ENABLE_MULTI_TOUCH = false;
    }

    start() {
        this.initializeGame();
    }

    async initializeGame() {
        AudioManager.init("audio/bgm/", "audio/effect/", "audio/voice/")
        await UIManager.init(this.uiRootNode!);
        let mainViewPromise = UIManager.pushPresenter(MainViewPresenter, undefined);
        AnimationUtils.animateProgress(this.progressBar!, 0.2);
        let ret = false;
        let tryCount = 0;
        while (!ret && tryCount < 3) {
            tryCount++;
            ret = await SDK.init();
        }
        if (!ret) {
            // TODO show error alert
            console.error("SDK init failed.");
            return;
        }
        AnimationUtils.animateProgress(this.progressBar, 0.4, 1, this.progressLabel);
        tryCount = 0;
        while (1) {
            try {
                tryCount++;
                await SDK.login();
                break;
            } catch (e) {
                if (tryCount > 3) {
                    console.log("SDK login failed.");
                    return;
                }
            }
        }

        AnimationUtils.animateProgress(this.progressBar, 0.6, 1, this.progressLabel);
        await PlotManager.init();
        AnimationUtils.animateProgress(this.progressBar, 0.8, 1, this.progressLabel);
        await PlotManager.start();
        // init game data
        await GameModelManager.init();
        AnimationUtils.animateProgress(this.progressBar, 1.0, 1, this.progressLabel);
        await (await mainViewPromise).start();
        this.node.getChildByName("Loading").active = false;
    }

    async startGame() {

    }
}