InitRes.ts 3.4 KB

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