LoadingScene.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { _decorator, Component, Node, Label, UITransform, tween, Vec3, director, ProgressBar, SpriteAtlas, error } from 'cc';
  2. import { ResourceLoader } from './ResourceLoader';
  3. import { sp, SpriteFrame, AudioClip, Font } from 'cc';
  4. import { GameConstant } from './GameConstant';
  5. import { Player } from './Player';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('LoadingScene')
  8. export class LoadingScene extends Component {
  9. @property(Node)
  10. progressBar!: Node;
  11. @property(Label)
  12. progressLabel!: Label;
  13. @property(Label)
  14. tipsLabel!: Label;
  15. private httpRetryCount: number = 0;
  16. private maxRetries: number = 3;
  17. private hasHttpResponse: boolean = false;
  18. start() {
  19. this.initLoader();
  20. this.startLoading();
  21. }
  22. initLoader() {
  23. const loader = ResourceLoader.instance;
  24. loader.addGroup("spine", sp.SkeletonData, 0.4);
  25. loader.addGroup("png", SpriteAtlas, 0.2);
  26. loader.addGroup("audio", AudioClip, 0.3);
  27. loader.addGroup("font", Font, 0.1);
  28. }
  29. startLoading() {
  30. const loader = ResourceLoader.instance;
  31. loader.loadAll(
  32. (percent) => {
  33. // 进度卡在 99%,等待 HTTP 回包
  34. const cappedPercent = Math.min(percent, 99);
  35. this.updateProgress(cappedPercent);
  36. },
  37. () => {
  38. this.tipsLabel.string = "加载完成,正在获取游戏信息...";
  39. this.sendHttpRequest();
  40. }
  41. );
  42. }
  43. /**
  44. * 发送游戏信息请求,失败会重试三次
  45. */
  46. private sendHttpRequest() {
  47. const url = GameConstant.HTTP_HOST + "game-api/" + GameConstant.GAMEID + "/v2/gameinfo";
  48. console.log("请求游戏信息:", url);
  49. GameConstant.httpRequest("GET", url)
  50. .then((res: any) => {
  51. console.log("HTTP 回包成功:", res);
  52. this.hasHttpResponse = true;
  53. this.updateProgress(100);
  54. const player = Player.getInstance();
  55. player.init({
  56. playerId: 1,
  57. nickname: "GUEST",
  58. score: res.dt.bl,
  59. betConfig: res.dt.cs,
  60. currencyType: res.dt.cc
  61. });
  62. this.tipsLabel.string = "游戏信息加载成功,正在进入游戏...";
  63. this.scheduleOnce(() => {
  64. director.loadScene("GameScene");
  65. }, 0.8);
  66. })
  67. .catch((err: any) => {
  68. this.httpRetryCount++;
  69. console.error(`HTTP 请求失败 (第 ${this.httpRetryCount} 次):`, err);
  70. if (this.httpRetryCount < this.maxRetries) {
  71. this.tipsLabel.string = `请求失败,正在重试 (${this.httpRetryCount}/${this.maxRetries})...`;
  72. this.scheduleOnce(() => this.sendHttpRequest(), 0.5);
  73. } else {
  74. this.tipsLabel.string = "获取游戏信息失败,请检查网络连接。";
  75. // TODO: 弹出提示框,提示用户重试或退出游戏
  76. console.error("三次重试失败,停止请求。");
  77. }
  78. });
  79. }
  80. private updateProgress(percent: number) {
  81. const progressBar = this.progressBar.parent!.getComponent(ProgressBar);
  82. if (progressBar) {
  83. progressBar.progress = percent / 100;
  84. }
  85. this.progressLabel.string = `${percent.toFixed(1)}%`;
  86. }
  87. }