import { AssetManager, assetManager, director } from "cc"; import { tgxUIAlert, tgxUIWaiting } from "../../core_tgx/tgx"; import { ISubGame, getSubGameConf } from "./SubGameDef"; import { SceneUtil } from "../../core_tgx/base/SceneUtils"; import { GameMgr } from "./GameMgr"; import { GameServerAuthParams } from "../shared/types/GameServerAuthParams"; export class SubGameMgr { private static _inst: SubGameMgr; public static get inst(): SubGameMgr { if (!this._inst) { this._inst = new SubGameMgr(); } return this._inst; } public static current: ISubGame; public static gameMgr: GameMgr; private _gameMgrMap = {}; public async enterSubLobby(gameType: string) { await this.loadSubGame(gameType); tgxUIWaiting.show('正在加载',true); await SceneUtil.loadScene(SubGameMgr.current.entry); tgxUIWaiting.hide(); SubGameMgr.gameMgr = this._gameMgrMap[gameType]; console.log('enterSubLobby',this._gameMgrMap); } public async enterSubGame(params: GameServerAuthParams,uid:string,goBattle = false) { tgxUIWaiting.show('正在进入游戏'); await this.loadSubGame(params.gameType); if(!goBattle){ SubGameMgr.gameMgr = this._gameMgrMap[params.gameType]; let ret = await SubGameMgr.gameMgr.enterRoom(params,uid); if (ret.isSucc) { await SceneUtil.loadScene(SubGameMgr.current.game); } return ret; }else{ await SceneUtil.loadScene(SubGameMgr.current.game); } tgxUIWaiting.hide(); } public exitSubGame() { if (SubGameMgr.current) { this.unloadSubGameBundle(SubGameMgr.current); SubGameMgr.current = null; SubGameMgr.gameMgr = null; } } private async loadSubGame(subgameId: string) { let old = SubGameMgr.current; if (old && old.id == subgameId) { return; } if (subgameId) { let conf = getSubGameConf(subgameId); if (!conf) { tgxUIWaiting.hide(); tgxUIAlert.show("can't find subgame:" + subgameId); return; } tgxUIWaiting.show('正在加载'); SubGameMgr.current = conf; await this.loadSubGameBundleSync(conf.entry.bundle); tgxUIWaiting.hide(); } if (old) { this.unloadSubGameBundle(old); } } private loadSubGameBundleSync(bundleName: string) { return new Promise((resolve, reject) => { assetManager.loadBundle(bundleName, (err, bundle: AssetManager.Bundle) => { resolve(!err); }); }); } private unloadSubGameBundle(conf: ISubGame) { if (!conf) { return; } let bundle = assetManager.getBundle(conf.entry.bundle); if (bundle) { bundle.releaseAll(); assetManager.removeBundle(bundle); } } public registerGameMgr(type: string, gameMgr: GameMgr) { this._gameMgrMap[type] = gameMgr; } public getGameMgr(type: string): GameMgr { return this._gameMgrMap[type]; } }