Start.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { _decorator, assetManager, Component, director, game, Label, Prefab, Node, AssetManager, Asset, PhysicsSystem, PhysicsSystem2D, EPhysics2DDrawFlags, AudioClip, ProgressBar } from 'cc';
  2. import { tgxModuleContext, tgxUIMgr, tgxUITips, tgxUIWaiting } from '../core_tgx/tgx';
  3. import { GameUILayers, GameUILayerNames } from '../scripts/GameUILayers';
  4. import { ModuleDef } from '../scripts/ModuleDef';
  5. import { SceneDef } from '../scripts/SceneDef';
  6. import { JsonUtil } from '../core_tgx/base/utils/JsonUtil';
  7. import { GtagMgr, GtagType } from '../core_tgx/base/GtagMgr';
  8. import { AdvertMgr } from '../core_tgx/base/ad/AdvertMgr';
  9. import { GlobalConfig } from './Config/GlobalConfig';
  10. import { GlobalMgr } from '../scripts/GlobalMgr';
  11. const { ccclass, property } = _decorator;
  12. const _preloadBundles = [ModuleDef.BASIC, ModuleDef.MODULE_ALIENS];
  13. const _preloadRes = [
  14. { bundle: ModuleDef.BASIC, url: 'ui_alert/UI_Alert', type: 'prefab' },
  15. { bundle: ModuleDef.BASIC, url: 'ui_tips/UI_Tips', type: 'prefab' },
  16. { bundle: ModuleDef.BASIC, url: 'ui_waiting/UI_Waiting', type: 'prefab' },
  17. { bundle: ModuleDef.MODULE_ALIENS, url: 'Prefabs/UI/Result/UI_BattleResult', type: 'prefab' },
  18. { bundle: ModuleDef.MODULE_ALIENS, url: 'Prefabs/UI/Gambit/UI_BattleGambit', type: 'prefab' },
  19. { bundle: ModuleDef.MODULE_ALIENS, url: 'Prefabs/UI/PowerUp/UI_PowerUp', type: 'prefab' },
  20. { bundle: ModuleDef.MODULE_ALIENS, url: 'Prefabs/UI/TimeExpan/UI_TimeExpan', type: 'prefab' },
  21. { bundle: ModuleDef.MODULE_ALIENS, url: 'Prefabs/Levels/level_1', type: 'prefab' },
  22. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/bgm1.mp3', type: 'audio' },
  23. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/dianji.mp3', type: 'audio' },
  24. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/shengli.mp3', type: 'audio' },
  25. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/shibai.mp3', type: 'audio' },
  26. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/kaiqiang.mp3', type: 'audio' },
  27. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/miaozhun.mp3', type: 'audio' },
  28. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/qiangbao.mp3', type: 'audio' },
  29. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/tance.mp3', type: 'audio' },
  30. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/jibi.mp3', type: 'audio' },
  31. { bundle: ModuleDef.MODULE_ALIENS, url: 'Audio/chaofeng.mp3', type: 'audio' },
  32. ];
  33. const _loadingText = ['Loading.', 'Loading..', 'Loading...'];
  34. const _totalNum = _preloadBundles.length + _preloadRes.length + 1;
  35. @ccclass('Start')
  36. export class Start extends Component {
  37. @property(Label)
  38. txtLoading: Label;
  39. @property(Prefab)
  40. uiCanvasPrefab: Prefab;
  41. // @property(Node)
  42. // loadingBar: Node;
  43. @property(ProgressBar)
  44. loadingBar: ProgressBar = null!;
  45. private _percent: string = '';
  46. private _numCurrentLoaded = 0;
  47. protected onLoad(): void {
  48. GlobalMgr.instance.initilize();
  49. // GlobalMgr.instance.timeTest();
  50. AdvertMgr.instance.initilize();
  51. if (!GlobalConfig.isDebug) {
  52. AdvertMgr.instance.showInterstitial();
  53. }
  54. window.addEventListener("beforeunload", () => {
  55. const timestamp = Date.now(); // 获取当前时间戳(毫秒)
  56. localStorage.setItem("lastLeaveTime", timestamp.toString());
  57. });
  58. }
  59. start() {
  60. PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
  61. EPhysics2DDrawFlags.Pair |
  62. EPhysics2DDrawFlags.CenterOfMass |
  63. EPhysics2DDrawFlags.Joint |
  64. EPhysics2DDrawFlags.Shape;
  65. PhysicsSystem2D.instance.debugDrawFlags = 1; // 启用调试绘制
  66. tgxModuleContext.setDefaultModule(ModuleDef.BASIC);
  67. game.frameRate = 60;
  68. tgxUIMgr.inst.setup(this.uiCanvasPrefab, GameUILayers.NUM, GameUILayerNames);
  69. this.preloadBundle(0);
  70. GtagMgr.inst.init();
  71. this.loadConfig();
  72. GtagMgr.inst.doGameDot(GtagType.game_start);
  73. }
  74. async loadConfig() {
  75. await this.loadCustom();
  76. }
  77. loadCustom() {
  78. return new Promise(async (resolve, reject) => {
  79. await JsonUtil.loadDirAsync();
  80. resolve(null);
  81. });
  82. }
  83. onResLoaded() {
  84. this._numCurrentLoaded++;
  85. this._percent = ~~(this._numCurrentLoaded / _totalNum * 100) + '%';
  86. }
  87. preloadBundle(idx: number) {
  88. assetManager.loadBundle(_preloadBundles[idx], null, (err, bundle) => {
  89. console.log('module:<' + _preloadBundles[idx] + '>loaded.');
  90. idx++;
  91. this.onResLoaded();
  92. if (idx < _preloadBundles.length) {
  93. this.preloadBundle(idx);
  94. }
  95. else {
  96. this.preloadRes(0);
  97. }
  98. });
  99. }
  100. preloadRes(idx: number) {
  101. let res = _preloadRes[idx];
  102. // console.log('预加载资源:' + res.url);
  103. let bundle = assetManager.getBundle(res.bundle);
  104. let onComplete = () => {
  105. idx++;
  106. this.onResLoaded();
  107. if (idx < _preloadRes.length) {
  108. this.preloadRes(idx);
  109. }
  110. else {
  111. this.onPreloadingComplete();
  112. }
  113. }
  114. if (bundle) {
  115. if (res.type == 'prefab') {
  116. bundle.preload(res.url, Prefab, onComplete);
  117. } else if (res.type == 'audio') {
  118. bundle.preload(res.url, AudioClip, onComplete);
  119. }
  120. }
  121. }
  122. onPreloadingComplete() {
  123. let bundle = assetManager.getBundle(ModuleDef.MODULE_ALIENS);
  124. bundle.preloadScene(SceneDef.ROOSTER_HOLE, () => {
  125. this.onResLoaded();
  126. // director.loadScene(SceneDef.MAIN_MENU);
  127. const info = {
  128. bundle: 'module_aliens',
  129. entryScene: 'rooster_aliens'
  130. }
  131. tgxUIWaiting.show();
  132. assetManager.loadBundle(info.bundle, (err, bundle: AssetManager.Bundle) => {
  133. if (bundle) {
  134. director.loadScene(info.entryScene, () => {
  135. tgxUIMgr.inst.hideAll();
  136. });
  137. }
  138. });
  139. });
  140. }
  141. update(deltaTime: number) {
  142. if (this._percent) {
  143. this.txtLoading.string = 'Loading...' + this._percent;
  144. }
  145. else {
  146. let idx = Math.floor(game.totalTime / 1000) % 3;
  147. this.txtLoading.string = _loadingText[idx];
  148. }
  149. // this.loadingBar.setScale(this._numCurrentLoaded / _totalNum, 1, 1);
  150. this.loadingBar.progress = this._numCurrentLoaded / _totalNum;
  151. }
  152. }