TaskWindow.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { _decorator, Component, Prefab, UITransform, instantiate } from 'cc';
  2. import { IFDataSource, InfiniteList } from '../../core/InfiniteList/InfiniteList';
  3. import { Http } from '../../core/net/Http';
  4. import { BitmapFont } from '../../core/ui/BitmapFont';
  5. import { Utils } from '../../core/utils/Utils';
  6. import { TaskActive } from './TaskActive';
  7. import { TaskWindowItem } from './TaskWindowItem';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('TaskWindow')
  10. export class TaskWindow extends Component implements IFDataSource {
  11. @property({ type: BitmapFont, tooltip: "倒计时文本" }) countDownLabel: BitmapFont;
  12. @property({ type: InfiniteList, tooltip: "任务列表" }) list: InfiniteList;
  13. @property({ type: Prefab, tooltip: "Item预制体" }) item: Prefab;
  14. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  15. @property({ type: TaskActive, tooltip: "活跃度脚本" }) taskActive: TaskActive;
  16. private endTime = 0;
  17. private prefabHeight: number = 0;
  18. private taskData = [];
  19. async start() {
  20. this.endTime = new Date().setHours(24, 0, 0, 0);
  21. let prefab = instantiate(this.item);
  22. this.prefabHeight = prefab.getComponent(UITransform).height;
  23. prefab.destroy();
  24. this.list.Init(this);
  25. await this.reloadList();
  26. }
  27. public async reloadList() {
  28. this.taskData = [];
  29. let result = await this.http.send("/api/task/taskList");
  30. if (result.code == 0) {
  31. this.taskActive.setData(result.data.active);
  32. for (let i = result.data.taskList.length - 1; i >= 0; i--) {
  33. let obj = { data: result.data.taskList[i], window: this }
  34. if (result.data.taskList[i].progress > result.data.taskList[i].num) {
  35. this.taskData.push(obj);
  36. } else {
  37. this.taskData.unshift(obj);
  38. }
  39. }
  40. this.list.Reload(true);
  41. }
  42. }
  43. update() {
  44. if (this.countDownLabel.string != Utils.formatCountDown(this.endTime - Date.now())) {
  45. this.countDownLabel.string = Utils.formatCountDown(this.endTime - Date.now());
  46. }
  47. }
  48. //#region IFDataSource
  49. public GetCellData(dataIndex: number): any {
  50. return this.taskData[dataIndex];
  51. }
  52. public GetCellNumber(): number {
  53. return this.taskData.length;
  54. }
  55. public GetCellSize(index: number): number {
  56. return this.prefabHeight;
  57. }
  58. public GetCellView(index: number): TaskWindowItem {
  59. let node = instantiate(this.item);
  60. return node.getComponent(TaskWindowItem);
  61. }
  62. public GetCellIdentifer(index: number): string {
  63. return "taskWindowItem";
  64. }
  65. //#endregion
  66. }