GameMgr.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import GameCtr from "./GameCtr";
  2. import { cocosz } from "./CocosZ";
  3. import Constant, { PageName, GameState, PanelName } from "./Constant";
  4. import Msg from "./Msg";
  5. import { utils } from "../../common-plugin/Scripts/Utils";
  6. import YZ_Constant, { LevelStatus, YZ_Reward } from "../../common-plugin/Scripts/YZ_Constant";
  7. import YZ_LocalStorage from "../../common-plugin/Scripts/YZ_LocalStorage";
  8. // @ts-ignore
  9. const i18n = require('LanguageData');
  10. /**
  11. * 游戏管理
  12. */
  13. export default class GameMgr {
  14. private static _inst: GameMgr;
  15. public static get inst(): GameMgr {
  16. if (!GameMgr._inst) {
  17. GameMgr._inst = new GameMgr();
  18. }
  19. return GameMgr._inst;
  20. }
  21. protected _state: GameState = GameState.None;
  22. public get State() {
  23. return this._state;
  24. }
  25. public set State(value: GameState) {
  26. this._state = value;
  27. }
  28. private _gameCtr: GameCtr = new GameCtr();;
  29. public get gameCtr() {
  30. return this._gameCtr;
  31. }
  32. public gameStartTime: number = 0;
  33. private canTry: boolean = true;
  34. private canGame: boolean = true;
  35. /**
  36. * 开始游戏
  37. * @param 关卡ID
  38. */
  39. public async gameStart(levelId: number) {
  40. console.log('zh:GM.TS gameStart')
  41. YZ_LocalStorage.setItem(YZ_Constant.ST_RED_BAG_PROGRESS, '0');
  42. cocosz.curLevel = levelId;
  43. // 重置
  44. if (this.canTry) {
  45. this.canTry = false;
  46. this.gameCtr.curUseSkinId = -1;
  47. // 能否弹试用
  48. if (cocosz.isShowAd && cocosz.isADON && (cocosz.isDeBug || utils.isShowTrySkin(levelId))) {
  49. let rangeLevel = cocosz.dataMgr.getGunInfo(cocosz.dataMgr.CurRange).Level;
  50. if (Math.random() < 0.5 && (rangeLevel < 3)) {
  51. // 武器升级弹窗
  52. cocosz.uiMgr.openPanel(PanelName.UIWeaponLevelPanel);
  53. } else {
  54. // 皮肤试用弹窗
  55. cocosz.uiMgr.openPanel(PanelName.UITrySkinPanel);
  56. }
  57. }
  58. // 进入游戏
  59. else if (this.canGame) {
  60. this.canGame = false;
  61. this._loadGameScene();
  62. }
  63. }
  64. // 能否进入游戏
  65. else if (this.canGame) {
  66. this.canGame = false;
  67. this._loadGameScene();
  68. }
  69. }
  70. private _loadGameScene() {
  71. // 使用皮肤
  72. if (this.gameCtr.curUseSkinId < 0) {
  73. this.gameCtr.curUseSkinId = cocosz.dataMgr.CurSkinId;
  74. }
  75. //上报游戏开始
  76. utils.StartGame(cocosz.getLevelId().toString());
  77. //进入游戏场景
  78. cocosz.sceneMgr.loadScene("Game", () => {
  79. this.gameStartTime = new Date().getTime();
  80. this.canTry = true;
  81. this.canGame = true;
  82. this._gamePrepare();
  83. //显示过度页面
  84. cocosz.uiMgr.openPage(PageName.UIGameLoadingPage);
  85. });
  86. }
  87. /** 游戏预加载 */
  88. private _gamePrepare() {
  89. try {
  90. this.State = GameState.Prepare;
  91. //调用游戏初始化
  92. this.gameCtr.init();
  93. } catch (error) {
  94. cc.error("erro >>", error);
  95. // 资源没有准备好, 无法进入游戏
  96. cocosz.sceneMgr.loadScene("Home", () => {
  97. cocosz.uiMgr.openPage(PageName.UIHomePage);
  98. Msg.Show(i18n.t("msg.net_error"));//当前网络状态不佳,请重启游戏
  99. });
  100. }
  101. }
  102. /** 游戏胜利 */
  103. public gameSuccess() {
  104. cc.log('游戏胜利!');
  105. this.State = GameState.Success;
  106. cocosz.audioMgr.playEffectWinner();
  107. // 上报游戏时间
  108. let gameTime = Math.round((new Date().getTime() - this.gameStartTime) / 1000 / 60);
  109. utils.SendEvent("游戏时间:" + gameTime + "分钟");
  110. //保存关卡解锁数据
  111. let rewardCallFunc = (res: YZ_Reward) => {
  112. cc.log("获取激励组件奖励+" + res.rewardValue)
  113. if (res) {
  114. cocosz.dataMgr.CoinCount += res.rewardValue;
  115. cc.game.emit(Constant.E_GAME_LOGIC, { type: Constant.E_COIN_CHANGE });
  116. }
  117. };
  118. let closeCallFunc = () => {
  119. cc.log("激励组件关闭!!!!!!");
  120. cocosz.uiMgr.openPage(PageName.UIOverPage);
  121. }
  122. //显示结算前广告
  123. utils.adManager.showBeforGameOverAd(cocosz.getLevelId(), LevelStatus.GameWin, 100, closeCallFunc, rewardCallFunc);
  124. }
  125. /** 游戏失败 */
  126. public gameFailed() {
  127. cc.log('游戏失败!');
  128. this.State = GameState.Failed;
  129. cocosz.audioMgr.playEffectFailed();
  130. // 上报游戏时间
  131. let gameTime = Math.round((new Date().getTime() - this.gameStartTime) / 1000 / 60);
  132. utils.SendEvent("游戏时间:" + gameTime + "分钟");
  133. let rewardCallFunc = (res: YZ_Reward) => {
  134. cc.log("获取激励组件奖励+" + res.rewardValue)
  135. if (res) {
  136. //保存奖励值到本地
  137. cocosz.dataMgr.CoinCount += res.rewardValue;
  138. cc.game.emit(Constant.E_GAME_LOGIC, { type: Constant.E_COIN_CHANGE });
  139. }
  140. };
  141. let closeCallFunc = () => {
  142. cc.log("激励组件关闭!!!!!!");
  143. cocosz.uiMgr.openPage(PageName.UIOverPage);
  144. }
  145. utils.adManager.showBeforGameOverAd(cocosz.getLevelId(), LevelStatus.GameFail, 50, closeCallFunc, rewardCallFunc);
  146. }
  147. }