Start.ts 6.1 KB

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