Start.ts 6.0 KB

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