Start.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { _decorator, assetManager, Component, director, game, Label, Prefab, Node } from 'cc';
  2. import { tgxModuleContext, tgxUIMgr } 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 { ProcessBar } from '../scripts/Component/ProcessBar';
  7. import { aa } from '../scripts/aa';
  8. import { ch_start } from '../ch/start/ch_start';
  9. const { ccclass, property } = _decorator;
  10. const _preloadBundles = [ModuleDef.EXTRA, ModuleDef.BASIC ];
  11. const _preloadRes = [
  12. { bundle: ModuleDef.BASIC, url: 'ui_game/UI_Game', type: 'prefab' },
  13. { bundle: ModuleDef.BASIC, url: 'ui_dialog/win_dialog/ui_win', type: 'prefab' },
  14. { bundle: ModuleDef.BASIC, url: 'ui_dialog/guide/Guide', type: 'prefab' },
  15. { bundle: ModuleDef.BASIC, url: 'ui_dialog/die_dialog/ui_die', type: 'prefab' },
  16. { bundle: ModuleDef.BASIC, url: 'scene/res', type: 'prefab' },
  17. ];
  18. const _loadingText = ['Loading.', 'Loading..', 'Loading...'];
  19. const _totalNum = _preloadBundles.length + _preloadRes.length + 1;
  20. const gid = 'xchst'
  21. const tdKey = "5F358EE0E2E94D078FD9E32DDA457229"
  22. @ccclass('Start')
  23. export class Start extends ch_start {
  24. @property(Label)
  25. txtLoading: Label;
  26. @property(Prefab)
  27. uiCanvasPrefab: Prefab;
  28. @property(ProcessBar)
  29. loadingBar: ProcessBar;
  30. @property(Label)
  31. titlen: Label;
  32. private _percent: string = '';
  33. private _numCurrentLoaded = 0;
  34. isinit
  35. async start() {
  36. this.isinit = 1
  37. game.frameRate = 61;
  38. tgxModuleContext.setDefaultModule(ModuleDef.BASIC);
  39. this.loadingBar.init();
  40. tgxUIMgr.inst.setup(this.uiCanvasPrefab, GameUILayers.NUM, GameUILayerNames);
  41. await this.try_init()
  42. this.preloadBundle(0);
  43. this.isinit += 1
  44. aa.data.download(() => {
  45. this.isinit -= 1
  46. this.onPreloadingComplete();
  47. })
  48. aa.info.g_name = this.conf.gname
  49. if(this.titlen){
  50. this.titlen.string = aa.info.g_name
  51. }
  52. }
  53. onResLoaded() {
  54. this._numCurrentLoaded++;
  55. this._percent = ~~(this._numCurrentLoaded / _totalNum * 100) +"";
  56. }
  57. preloadBundle(idx: number) {
  58. assetManager.loadBundle(_preloadBundles[idx], null, (err, bundle) => {
  59. console.log('module:<' + _preloadBundles[idx] + '>loaded.');
  60. idx++;
  61. this.onResLoaded();
  62. if (idx < _preloadBundles.length) {
  63. this.preloadBundle(idx);
  64. }
  65. else {
  66. this.preloadRes(0);
  67. }
  68. });
  69. }
  70. preloadRes(idx: number) {
  71. let res = _preloadRes[idx];
  72. let bundle = assetManager.getBundle(res.bundle);
  73. let onComplete = () => {
  74. idx++;
  75. this.onResLoaded();
  76. if (idx < _preloadRes.length) {
  77. this.preloadRes(idx);
  78. }
  79. else {
  80. this.isinit -= 1
  81. this.onPreloadingComplete();
  82. }
  83. }
  84. if (bundle) {
  85. if (res.type == 'prefab') {
  86. bundle.load(res.url, Prefab, onComplete);
  87. }
  88. }
  89. }
  90. onPreloadingComplete() {
  91. if (this.isinit > 0) {
  92. return
  93. }
  94. let bundle = assetManager.getBundle(ModuleDef.BASIC);
  95. bundle.preloadScene(SceneDef.MAIN_MENU, () => {
  96. this.onResLoaded();
  97. director.loadScene(SceneDef.MAIN_MENU);
  98. });
  99. }
  100. update(deltaTime: number) {
  101. if (this._percent) {
  102. this.txtLoading.string = this._percent;
  103. }
  104. else {
  105. let idx = Math.floor(game.totalTime / 1000) % 3;
  106. this.txtLoading.string = _loadingText[idx];
  107. this.txtLoading.string = '0';
  108. }
  109. this.loadingBar.set(this._numCurrentLoaded / _totalNum );
  110. }
  111. }