InitRes.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2022-07-22 17:06:22
  4. * @LastEditors: dgflash
  5. * @LastEditTime: 2023-07-25 17:53:47
  6. */
  7. import { director } from "cc";
  8. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  9. import { AsyncQueue, NextFunction } from "../../../../../extensions/oops-plugin-framework/assets/libs/collection/AsyncQueue";
  10. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  11. import { UIID } from "../../common/config/GameUIConfig";
  12. import { Initialize } from "../Initialize";
  13. import { LoadingViewComp } from "../view/LoadingViewComp";
  14. /** 初始化游戏公共资源 */
  15. @ecs.register('InitRes')
  16. export class InitResComp extends ecs.Comp {
  17. reset() { }
  18. }
  19. export class InitResSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
  20. filter(): ecs.IMatcher {
  21. return ecs.allOf(InitResComp);
  22. }
  23. entityEnter(e: Initialize): void {
  24. var queue: AsyncQueue = new AsyncQueue();
  25. /** 加载远程资源配置 */
  26. this.loadBundle(queue);
  27. // 加载自定义资源
  28. this.loadCustom(queue);
  29. // 加载多语言包加载多语言包
  30. this.loadLanguage(queue);
  31. // 加载公共资源
  32. this.loadCommon(queue);
  33. // 加载游戏内容加载进度提示界面
  34. this.onComplete(queue, e);
  35. queue.play();
  36. }
  37. /** 加载远程资源配置 */
  38. private loadBundle(queue: AsyncQueue) {
  39. queue.push(async (next: NextFunction, params: any, args: any) => {
  40. // 设置默认加载的外部资源包名
  41. oops.res.defaultBundleName = oops.config.game.bundleName;
  42. // 加载外部资源包
  43. if (oops.config.game.bundleEnable) {
  44. console.log("启用远程资源运行游戏");
  45. await oops.res.loadBundle(oops.config.game.bundleServer, oops.config.game.bundleVersion);
  46. }
  47. else {
  48. await oops.res.loadBundle(oops.config.game.bundleName);
  49. }
  50. next();
  51. });
  52. }
  53. /** 加载自定义内容(可选) */
  54. private loadCustom(queue: AsyncQueue) {
  55. queue.push(async (next: NextFunction, params: any, args: any) => {
  56. // 加载多语言对应字体
  57. oops.res.load("language/font/" + oops.language.current, next);
  58. });
  59. }
  60. /** 加载化语言包(可选) */
  61. private loadLanguage(queue: AsyncQueue) {
  62. queue.push((next: NextFunction, params: any, args: any) => {
  63. // 设置默认语言
  64. let lan = oops.storage.get("language");
  65. if (lan == null) {
  66. lan = "zh";
  67. oops.storage.set("language", lan);
  68. }
  69. // 加载语言包资源
  70. oops.language.setLanguage(lan, next);
  71. });
  72. }
  73. /** 加载公共资源(必备) */
  74. private loadCommon(queue: AsyncQueue) {
  75. queue.push((next: NextFunction, params: any, args: any) => {
  76. oops.res.loadDir("common", next);
  77. });
  78. }
  79. /** 加载完成进入游戏内容加载界面 */
  80. private onComplete(queue: AsyncQueue, e: Initialize) {
  81. queue.complete = async () => {
  82. var node = await oops.gui.openAsync(UIID.Loading);
  83. const loadingbg = director.getScene().getChildByPath('root/gui/loadingbg')
  84. if(loadingbg)loadingbg.active = false
  85. if (node) e.add(node.getComponent(LoadingViewComp) as ecs.Comp);
  86. e.remove(InitResComp);
  87. };
  88. }
  89. }