Start.ts 6.2 KB

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