Start.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // 扩展Window接口以支持预览调试
  18. declare global {
  19. interface Window {
  20. __PREVIEW_DEBUG__?: boolean;
  21. __SOURCE_MAPS_ENABLED__?: boolean;
  22. }
  23. }
  24. const tempColor = new Color();
  25. const { ccclass, property } = _decorator;
  26. // ========== config begin =================
  27. //the first scene after preloading completes.
  28. const _FPS = 61;
  29. const _defaultModule = ModuleDef.BASIC;
  30. const _firstScene = SceneDef.LOBBY;
  31. // const _firstScene = SceneDef.LOGIN;
  32. const _preloadBundles = [ModuleDef.BASIC, ModuleDef.BALL];
  33. const _preloadScenes = [];
  34. const _preloadRes = [
  35. // { bundle: ModuleDef.BASIC, url: "ui_alert/UI_Alert", type: "prefab" },
  36. // { bundle: ModuleDef.BASIC, url: "ui_waiting/UI_Waiting", type: "prefab" },
  37. // { bundle: ModuleDef.BASIC, url: "ui_login/ui_login", type: "prefab" },
  38. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  39. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  40. { bundle: ModuleDef.BALL, url: "ui_lobby/lobby_new", type: "prefab" },
  41. ];
  42. // ========= config end =====================
  43. if (_preloadScenes.indexOf(_firstScene) == -1) {
  44. _preloadScenes.push(_firstScene);
  45. }
  46. for (let i = 0; i < _preloadScenes.length; ++i) {
  47. let sceneInfo = _preloadScenes[i];
  48. let idx = _preloadBundles.indexOf(sceneInfo.bundle);
  49. if (idx == -1) {
  50. _preloadBundles.push(sceneInfo.bundle);
  51. }
  52. _preloadRes.push({
  53. bundle: sceneInfo.bundle,
  54. url: sceneInfo.name,
  55. type: "scene",
  56. });
  57. }
  58. const _loadingText = ["Loading.", "Loading..", "Loading..."];
  59. const _totalNum = _preloadBundles.length + _preloadRes.length;
  60. @ccclass("Start")
  61. export class Start extends Component {
  62. @property(Label)
  63. txtLoading: Label;
  64. @property(Prefab)
  65. uiCanvasPrefab: Prefab;
  66. @property(Node)
  67. loadingBar: Node;
  68. @property(Label) lblTapToContinue: Label;
  69. private _percent: string = "";
  70. private _numCurrentLoaded = 0;
  71. start() {
  72. /**
  73. * @en display stats in preview mode
  74. * @zh 预览调试时,默认显示性能统计面板
  75. */
  76. if (PREVIEW) {
  77. profiler.showStats();
  78. // Enable better debugging in preview mode
  79. console.log("Preview mode enabled");
  80. // Test source map functionality
  81. setTimeout(() => {
  82. console.log("Testing source map - this should show the original file and line number");
  83. // This will help verify if source maps are working
  84. const testError = new Error("Test error for source map verification");
  85. console.error("Source map test:", testError.message);
  86. }, 2000);
  87. // Override error handling for better stack traces
  88. window.addEventListener('error', function(event) {
  89. console.error('Preview Error:', event.error);
  90. console.error('Stack:', event.error?.stack);
  91. });
  92. }
  93. tgx.ModuleContext.setDefaultModule(_defaultModule);
  94. this.lblTapToContinue.node.active = false;
  95. game.frameRate = _FPS;
  96. tgx.UIMgr.inst.setup(
  97. this.uiCanvasPrefab,
  98. GameUILayers.NUM,
  99. GameUILayerNames
  100. );
  101. this.preloadBundle(0);
  102. }
  103. onResLoaded() {
  104. this._numCurrentLoaded++;
  105. this._percent = ~~((this._numCurrentLoaded / _totalNum) * 100) + "%";
  106. }
  107. preloadBundle(idx: number) {
  108. assetManager.loadBundle(_preloadBundles[idx], null, (err, bundle) => {
  109. console.log("module:<" + _preloadBundles[idx] + ">loaded.");
  110. idx++;
  111. this.onResLoaded();
  112. if (idx < _preloadBundles.length) {
  113. this.preloadBundle(idx);
  114. } else {
  115. this.preloadRes(0);
  116. }
  117. });
  118. }
  119. preloadRes(idx: number) {
  120. let res = _preloadRes[idx];
  121. let bundle = assetManager.getBundle(res.bundle);
  122. let onComplete = () => {
  123. idx++;
  124. this.onResLoaded();
  125. if (idx < _preloadRes.length) {
  126. this.preloadRes(idx);
  127. } else {
  128. this.onPreloadingComplete();
  129. }
  130. };
  131. if (bundle) {
  132. if (res.type == "prefab") {
  133. bundle.preload(res.url, Prefab, onComplete);
  134. } else if (res.type == "scene") {
  135. bundle.preloadScene(res.url, onComplete);
  136. }
  137. }
  138. }
  139. private _isPreloadingComplete = false;
  140. onPreloadingComplete() {
  141. this._isPreloadingComplete = true;
  142. this.lblTapToContinue.node.active = true;
  143. this.loadingBar.parent.active = false;
  144. this.onBtnBgClicked();
  145. }
  146. onBtnBgClicked() {
  147. if (!this._isPreloadingComplete) {
  148. return;
  149. }
  150. director.loadScene(_firstScene.name);
  151. }
  152. update(deltaTime: number) {
  153. if (this.loadingBar.parent.active) {
  154. if (this._percent) {
  155. this.txtLoading.string = "Loading..." + this._percent;
  156. } else {
  157. let idx = Math.floor(game.totalTime / 1000) % 3;
  158. this.txtLoading.string = _loadingText[idx];
  159. }
  160. this.loadingBar.setScale(this._numCurrentLoaded / _totalNum, 1, 1);
  161. }
  162. if (this.lblTapToContinue.node.active) {
  163. tempColor.set(this.lblTapToContinue.color);
  164. let value = Math.sin(Date.now() / 50 / Math.PI);
  165. value = value * 0.5 + 0.5;
  166. tempColor.a = value * 255;
  167. this.lblTapToContinue.color = tempColor;
  168. }
  169. }
  170. }