Start.ts 6.1 KB

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