Start.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. _decorator,
  3. assetManager,
  4. Component,
  5. director,
  6. game,
  7. Label,
  8. Prefab,
  9. Node,
  10. profiler,
  11. Color,
  12. } from "cc";
  13. import { GameUILayers, GameUILayerNames } from "../scripts/GameUILayers";
  14. import { ModuleDef } from "../scripts/ModuleDef";
  15. import { SceneDef } from "../scripts/SceneDef";
  16. import { PREVIEW } from "cc/env";
  17. const tempColor = new Color();
  18. const { ccclass, property } = _decorator;
  19. // ========== config begin =================
  20. //the first scene after preloading completes.
  21. const _FPS = 61;
  22. const _defaultModule = ModuleDef.BASIC;
  23. const _firstScene = SceneDef.LOBBY;
  24. // const _firstScene = SceneDef.LOGIN;
  25. const _preloadBundles = [ModuleDef.BASIC, ModuleDef.BALL];
  26. const _preloadScenes = [];
  27. const _preloadRes = [
  28. // { bundle: ModuleDef.BASIC, url: "ui_alert/UI_Alert", type: "prefab" },
  29. // { bundle: ModuleDef.BASIC, url: "ui_waiting/UI_Waiting", type: "prefab" },
  30. // { bundle: ModuleDef.BASIC, url: "ui_login/ui_login", type: "prefab" },
  31. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  32. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  33. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  34. ];
  35. // ========= config end =====================
  36. if (_preloadScenes.indexOf(_firstScene) == -1) {
  37. _preloadScenes.push(_firstScene);
  38. }
  39. for (let i = 0; i < _preloadScenes.length; ++i) {
  40. let sceneInfo = _preloadScenes[i];
  41. let idx = _preloadBundles.indexOf(sceneInfo.bundle);
  42. if (idx == -1) {
  43. _preloadBundles.push(sceneInfo.bundle);
  44. }
  45. _preloadRes.push({
  46. bundle: sceneInfo.bundle,
  47. url: sceneInfo.name,
  48. type: "scene",
  49. });
  50. }
  51. const _loadingText = ["Loading.", "Loading..", "Loading..."];
  52. const _totalNum = _preloadBundles.length + _preloadRes.length;
  53. @ccclass("Start")
  54. export class Start extends Component {
  55. @property(Label)
  56. txtLoading: Label;
  57. @property(Prefab)
  58. uiCanvasPrefab: Prefab;
  59. @property(Node)
  60. loadingBar: Node;
  61. @property(Label) lblTapToContinue: Label;
  62. private _percent: string = "";
  63. private _numCurrentLoaded = 0;
  64. start() {
  65. /**
  66. * @en display stats in preview mode
  67. * @zh 预览调试时,默认显示性能统计面板
  68. */
  69. if (PREVIEW) {
  70. profiler.showStats();
  71. }
  72. tgx.ModuleContext.setDefaultModule(_defaultModule);
  73. this.lblTapToContinue.node.active = false;
  74. game.frameRate = _FPS;
  75. tgx.UIMgr.inst.setup(
  76. this.uiCanvasPrefab,
  77. GameUILayers.NUM,
  78. GameUILayerNames
  79. );
  80. this.preloadBundle(0);
  81. }
  82. onResLoaded() {
  83. this._numCurrentLoaded++;
  84. this._percent = ~~((this._numCurrentLoaded / _totalNum) * 100) + "%";
  85. }
  86. preloadBundle(idx: number) {
  87. assetManager.loadBundle(_preloadBundles[idx], null, (err, bundle) => {
  88. console.log("module:<" + _preloadBundles[idx] + ">loaded.");
  89. idx++;
  90. this.onResLoaded();
  91. if (idx < _preloadBundles.length) {
  92. this.preloadBundle(idx);
  93. } else {
  94. this.preloadRes(0);
  95. }
  96. });
  97. }
  98. preloadRes(idx: number) {
  99. let res = _preloadRes[idx];
  100. let bundle = assetManager.getBundle(res.bundle);
  101. let onComplete = () => {
  102. idx++;
  103. this.onResLoaded();
  104. if (idx < _preloadRes.length) {
  105. this.preloadRes(idx);
  106. } else {
  107. this.onPreloadingComplete();
  108. }
  109. };
  110. if (bundle) {
  111. if (res.type == "prefab") {
  112. bundle.preload(res.url, Prefab, onComplete);
  113. } else if (res.type == "scene") {
  114. bundle.preloadScene(res.url, onComplete);
  115. }
  116. }
  117. }
  118. private _isPreloadingComplete = false;
  119. onPreloadingComplete() {
  120. this._isPreloadingComplete = true;
  121. this.lblTapToContinue.node.active = true;
  122. this.loadingBar.parent.active = false;
  123. this.onBtnBgClicked();
  124. }
  125. onBtnBgClicked() {
  126. if (!this._isPreloadingComplete) {
  127. return;
  128. }
  129. director.loadScene(_firstScene.name);
  130. }
  131. update(deltaTime: number) {
  132. if (this.loadingBar.parent.active) {
  133. if (this._percent) {
  134. this.txtLoading.string = "Loading..." + this._percent;
  135. } else {
  136. let idx = Math.floor(game.totalTime / 1000) % 3;
  137. this.txtLoading.string = _loadingText[idx];
  138. }
  139. this.loadingBar.setScale(this._numCurrentLoaded / _totalNum, 1, 1);
  140. }
  141. if (this.lblTapToContinue.node.active) {
  142. tempColor.set(this.lblTapToContinue.color);
  143. let value = Math.sin(Date.now() / 50 / Math.PI);
  144. value = value * 0.5 + 0.5;
  145. tempColor.a = value * 255;
  146. this.lblTapToContinue.color = tempColor;
  147. }
  148. }
  149. }