LoadingViewComp.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2021-07-03 16:13:17
  4. * @LastEditors: dgflash
  5. * @LastEditTime: 2022-08-10 18:07:35
  6. */
  7. import { _decorator } from "cc";
  8. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  9. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  10. import { CCVMParentComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCVMParentComp";
  11. import { UIID } from "../../common/config/GameUIConfig";
  12. const { ccclass, property } = _decorator;
  13. /** 游戏资源加载 */
  14. @ccclass('LoadingViewComp')
  15. @ecs.register('LoadingView', false)
  16. export class LoadingViewComp extends CCVMParentComp {
  17. /** VM 组件绑定数据 */
  18. data: any = {
  19. /** 加载资源当前进度 */
  20. finished: 0,
  21. /** 加载资源最大进度 */
  22. total: 0,
  23. /** 加载资源进度比例值 */
  24. progress: "0",
  25. /** 加载流程中提示文本 */
  26. prompt: ""
  27. };
  28. private progress: number = 0;
  29. reset(): void {
  30. // 获取用户信息的多语言提示文本
  31. this.data.prompt = oops.language.getLangByID("loading_load_player");
  32. // 进入自定义游戏内容界面
  33. oops.gui.open(UIID.Demo);
  34. // 关闭加载界面
  35. oops.gui.remove(UIID.Loading);
  36. }
  37. start() {
  38. this.enter();
  39. }
  40. enter() {
  41. this.loadRes();
  42. }
  43. /** 加载资源 */
  44. private async loadRes() {
  45. this.data.progress = 0;
  46. await this.loadCustom();
  47. this.loadGameRes();
  48. }
  49. /** 加载游戏本地JSON数据(自定义内容) */
  50. private loadCustom() {
  51. // 加载游戏本地JSON数据的多语言提示文本
  52. this.data.prompt = oops.language.getLangByID("loading_load_json");
  53. }
  54. /** 加载初始游戏内容资源 */
  55. private loadGameRes() {
  56. // 加载初始游戏内容资源的多语言提示文本
  57. this.data.prompt = oops.language.getLangByID("loading_load_game");
  58. oops.res.loadDir("game", this.onProgressCallback.bind(this), this.onCompleteCallback.bind(this));
  59. }
  60. /** 加载进度事件 */
  61. private onProgressCallback(finished: number, total: number, item: any) {
  62. this.data.finished = finished;
  63. this.data.total = total;
  64. var progress = finished / total;
  65. if (progress > this.progress) {
  66. this.progress = progress;
  67. this.data.progress = (progress * 100).toFixed(2);
  68. }
  69. }
  70. /** 加载完成事件 */
  71. private onCompleteCallback() {
  72. this.ent.remove(LoadingViewComp);
  73. }
  74. }