LoadQueue.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Asset, Component } from "cc";
  2. import { FrameworkConf } from "../config/FrameworkConf";
  3. import { Framework } from "../Framework";
  4. import { resLoader } from "./ResLoader";
  5. class ResQueue {
  6. private _load_timer_uuid = 0;
  7. private _load_task_quene: { priority: number, bundle: string, url: string, type: typeof Asset, completed: (error: Error, res: Asset) => void, target: Component }[] = [];
  8. private _preload_timer_uuid = 0;
  9. private _preload_task_quene: { priority: number, bundle: string, url: string, type: typeof Asset }[] = [];
  10. constructor() {
  11. this._load_timer_uuid = 0;
  12. this._load_task_quene = [];
  13. this._preload_timer_uuid = 0;
  14. this._preload_task_quene = [];
  15. }
  16. /**
  17. * 添加加载任务
  18. * @param priority 任务优先级
  19. * @param bundle 包名
  20. * @param url 路径
  21. * @param type 类型
  22. * @param completed 完成回调
  23. * @param target 任务目标
  24. */
  25. pushLoad(priority: number, bundle: string, url: string, type: typeof Asset, completed: (error: Error, res: Asset) => void, target: Component) {
  26. this._load_task_quene.push({ priority: priority, bundle: bundle, url: url, type: type, completed: completed, target: target });
  27. this._load_task_quene.sort((a, b) => {
  28. return b.priority - a.priority;
  29. });
  30. if (this._load_timer_uuid <= 0) {
  31. this._load_timer_uuid = Framework.time.schedule(() => {
  32. for (let i = 0; i < FrameworkConf.frame_load_count; ++i) {
  33. let task = this._load_task_quene.shift();
  34. if (task) {
  35. resLoader.load(task.bundle, task.url, task.type, task.completed.bind(task.target));
  36. } else {
  37. (this._load_timer_uuid > 0) && Framework.time.unschedule(this._load_timer_uuid);
  38. this._load_timer_uuid = -1;
  39. break;
  40. }
  41. }
  42. });
  43. }
  44. }
  45. /**
  46. * 添加预加载任务
  47. * @param priority 任务优先级
  48. * @param bundle 包名
  49. * @param url 路径
  50. * @param type 类型
  51. */
  52. pushPreload(priority: number, bundle: string, url: string, type: typeof Asset) {
  53. this._preload_task_quene.push({ priority: priority, bundle: bundle, url: url, type: type });
  54. this._preload_task_quene.sort((a, b) => {
  55. return b.priority - a.priority;
  56. });
  57. // if (this._preload_timer_uuid <= 0) {
  58. // this._preload_timer_uuid = Framework.time.schedule(() => {
  59. // for (let i = 0; i < FrameworkConf.frame_preload_count; ++i) {
  60. // let task = this._preload_task_quene.shift();
  61. // if (task) {
  62. // resLoader.preload(task.bundle, task.url, task.type);
  63. // } else {
  64. // (this._preload_timer_uuid > 0) && Framework.time.unschedule(this._preload_timer_uuid);
  65. // this._preload_timer_uuid = -1;
  66. // break;
  67. // }
  68. // }
  69. // });
  70. // }
  71. }
  72. // AB包总数量
  73. private totalAb: number = 0;
  74. // AB包当前已加载数量
  75. private nowAb: number = 0;
  76. // 资源当前数量
  77. private now: number = 0;
  78. // 资源总计数量
  79. private total: number = 0;
  80. /**
  81. * 加载预加载任务
  82. * @param
  83. */
  84. loadPreload(progress: Function, endFunc: Function) {
  85. this.total = this._preload_task_quene.length - 1
  86. if (this._preload_timer_uuid <= 0) {
  87. this._preload_timer_uuid = Framework.time.schedule(() => {
  88. for (let i = 0; i < FrameworkConf.frame_preload_count; ++i) {
  89. let task = this._preload_task_quene.shift();
  90. this.now++
  91. if (task) {
  92. resLoader.preload(task.bundle, task.url, task.type);
  93. progress(this.now, this.total)
  94. } else {
  95. (this._preload_timer_uuid > 0) && Framework.time.unschedule(this._preload_timer_uuid);
  96. this._preload_timer_uuid = -1;
  97. endFunc()
  98. break;
  99. }
  100. }
  101. });
  102. }
  103. }
  104. }
  105. export let LoadQueue = new ResQueue();