Start.ts 5.8 KB

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