RewardItem.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UIView } from '@scripts/framework/ui/UIView';
  4. import { formatMailTimeToMMDDYYYY, parseMailRewardStr } from '@scripts/utils/Helper';
  5. import { setOriginalSp, setSpriteFrame } from '@scripts/utils/ResHelper';
  6. import { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
  7. import { _decorator, Button, Component, Enum, instantiate, Label, Node, NodePool, Prefab, ProgressBar, Sprite, UIOpacity, UITransform } from 'cc';
  8. const { ccclass, property } = _decorator;
  9. export enum RewardType {
  10. Daily = 'daily',
  11. Seasonal = 'seasonal'
  12. }
  13. @ccclass('RewardItem')
  14. export class RewardItem extends Component {
  15. @property(Node)
  16. private bg_unclaim: Node = null;
  17. @property(Node)
  18. private bg_claimed: Node = null;
  19. @property(Sprite)
  20. private task_icon: Sprite = null;
  21. @property(Label)
  22. private task_detail: Label = null;
  23. @property(ProgressBar)
  24. private progress: ProgressBar = null;
  25. @property(Label)
  26. private progress_label: Label = null;
  27. @property(Node)
  28. private rewards: Node = null;
  29. @property(Node)
  30. private btn_go: Node = null;
  31. @property(Node)
  32. private btn_claim: Node = null;
  33. @property(Node)
  34. private btn_claimed: Node = null;
  35. @property(Prefab)
  36. private prop_sc: Prefab = null;
  37. @property(Prefab)
  38. private prop_gc: Prefab = null;
  39. @property(Prefab)
  40. private prop_pass_xp: Prefab = null;
  41. private _data: any = {};
  42. private _rewardType: RewardType;
  43. onLoad() {
  44. // 只绑定一次!
  45. addBtnClick(this.btn_go, this.handleBtnGoClick, this);
  46. addBtnClick(this.btn_claim, this.handleBtnClaimClick, this);
  47. addBtnClick(this.btn_claimed, this.handleBtnClaimedClick, this);
  48. }
  49. initItem(data: any = {}, rewardType: RewardType) {
  50. this._data = data;
  51. this._rewardType = rewardType;
  52. setSpriteFrame(this.bg_unclaim.getComponent(Sprite), 'textures/seasonPass/tasks/tasks_bg_claim_' + this._rewardType);
  53. setSpriteFrame(this.bg_claimed.getComponent(Sprite), 'textures/seasonPass/tasks/tasks_bg_claimed_' + this._rewardType);
  54. const { task_type, target_value, progress, reward, status } = this._data;
  55. const path_prefix = 'textures/seasonPass/tasks/detailIcon/';
  56. let path = path_prefix;
  57. switch (task_type) {
  58. // 1 签到
  59. // 2 完成指定数量的子游戏
  60. // 3 子游戏游玩次数(需指定 game_id)
  61. // 4 子游戏游玩时长(需指定 game_id)
  62. // 5 获得 SC
  63. case 1:
  64. path += 'icon_check';
  65. this.task_detail.string = `CHECK IN TODAY`;
  66. if (this._rewardType == RewardType.Seasonal) this.task_detail.string = `CHECK IN ${target_value} DAYS`;
  67. break;
  68. case 2:
  69. path += 'icon_game';
  70. this.task_detail.string = `PLAY ${target_value} SUBGAMES`;
  71. break;
  72. case 3:
  73. path += 'icon_win';
  74. this.task_detail.string = `WIN ${target_value} SUBGAMES`;
  75. break;
  76. case 4:
  77. path += 'icon_time';
  78. this.task_detail.string = `PLAY ${target_value} MINS`;
  79. break;
  80. case 5:
  81. path += 'icon_sc';
  82. this.task_detail.string = `OBTAIN SC`;
  83. break;
  84. }
  85. setOriginalSp(this.task_icon, path);
  86. this.progress.progress = progress / target_value;
  87. this.progress_label.string = `${progress}/${target_value}`;
  88. for (const [key, val] of Object.entries(reward)) {
  89. if (!this[`prop_${key}`] || val == 0) continue;
  90. const item = this.rewards.getChildByName(`prop_${key}`) ?? instantiate(this[`prop_${key}`]);
  91. item.parent = this.rewards;
  92. item.active = true;
  93. const num = item.getChildByName('num');
  94. const label = num.getComponent(Label);
  95. if (key == "sc" || key == "gc") {
  96. label.string = `${(Number(val) / 100).toFixed(2)}`;
  97. } else {
  98. label.string = `${val}`;
  99. }
  100. }
  101. this.btn_go.active = status == 0;
  102. this.btn_claim.active = status == 1;
  103. this.btn_claimed.active = status == 2;
  104. this.bg_unclaim.active = status != 2;
  105. this.bg_claimed.active = status == 2;
  106. }
  107. handleBtnGoClick() {
  108. const { task_type } = this._data;
  109. switch (task_type) {
  110. // 1 签到
  111. // 2 完成指定数量的子游戏
  112. // 3 子游戏游玩次数(需指定 game_id)
  113. // 4 子游戏游玩时长(需指定 game_id)
  114. // 5 获得 SC
  115. case 1:
  116. AppKernel.ui.open(UIViewID.CheckInView);
  117. // 监听签到变化
  118. break;
  119. case 2:
  120. case 3:
  121. case 4:
  122. case 5:
  123. AppKernel.ui.close(UIViewID.SeasonPassView);
  124. break;
  125. }
  126. }
  127. async handleBtnClaimClick() {
  128. const { id: task_id, reward } = this._data;
  129. const res = await AppKernel.stores.seasonPassStore.reqSeasonPassTasksClaim(task_id);
  130. if (res) AppKernel.ui.open(UIViewID.RewardView, { reward });
  131. }
  132. handleBtnClaimedClick() {
  133. }
  134. }