Start.ts 6.3 KB

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