Start.ts 6.2 KB

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