RewardItem.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 { 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 } 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. switch (task_type) {
  56. // 1 签到
  57. // 2 完成指定数量的子游戏
  58. // 3 子游戏游玩次数(需指定 game_id)
  59. // 4 子游戏游玩时长(需指定 game_id)
  60. // 5 获得 SC
  61. case 1:
  62. setSpriteFrame(this.task_icon, 'textures/seasonPass/tasks/detailIcon/icon_check');
  63. this.task_detail.string = `CHECK IN TODAY`;
  64. if (this._rewardType == RewardType.Seasonal) this.task_detail.string = `CHECK IN ${target_value} DAYS`;
  65. break;
  66. case 2:
  67. setSpriteFrame(this.task_icon, 'textures/seasonPass/tasks/detailIcon/icon_game');
  68. this.task_detail.string = `PLAY ${target_value} SUBGAMES`;
  69. break;
  70. case 3:
  71. setSpriteFrame(this.task_icon, 'textures/seasonPass/tasks/detailIcon/icon_win');
  72. this.task_detail.string = `WIN ${target_value} SUBGAMES`;
  73. break;
  74. case 4:
  75. setSpriteFrame(this.task_icon, 'textures/seasonPass/tasks/detailIcon/icon_time');
  76. this.task_detail.string = `PLAY ${target_value} MINS`;
  77. break;
  78. case 5:
  79. setSpriteFrame(this.task_icon, 'textures/seasonPass/tasks/detailIcon/icon_sc');
  80. this.task_detail.string = `OBTAIN SC`;
  81. break;
  82. }
  83. this.progress.progress = progress / target_value;
  84. this.progress_label.string = `${progress}/${target_value}`;
  85. for (const [key, val] of Object.entries(reward)) {
  86. if (!this[`prop_${key}`] || val == 0) continue;
  87. const item = this.rewards.getChildByName(`prop_${key}`) ?? instantiate(this[`prop_${key}`]);
  88. item.parent = this.rewards;
  89. item.active = true;
  90. const num = item.getChildByName('num');
  91. const label = num.getComponent(Label);
  92. if (key == "sc" || key == "gc") {
  93. label.string = `${(Number(val) / 100).toFixed(2)}`;
  94. } else {
  95. label.string = `${val}`;
  96. }
  97. }
  98. this.btn_go.active = status == 0;
  99. this.btn_claim.active = status == 1;
  100. this.btn_claimed.active = status == 2;
  101. this.bg_unclaim.active = status != 2;
  102. this.bg_claimed.active = status == 2;
  103. }
  104. handleBtnGoClick() {
  105. const { task_type } = this._data;
  106. switch (task_type) {
  107. // 1 签到
  108. // 2 完成指定数量的子游戏
  109. // 3 子游戏游玩次数(需指定 game_id)
  110. // 4 子游戏游玩时长(需指定 game_id)
  111. // 5 获得 SC
  112. case 1:
  113. AppKernel.ui.open(UIViewID.CheckInView);
  114. // 监听签到变化
  115. break;
  116. case 2:
  117. case 3:
  118. case 4:
  119. case 5:
  120. AppKernel.ui.close(UIViewID.SeasonPassView);
  121. break;
  122. }
  123. }
  124. async handleBtnClaimClick() {
  125. const { id: task_id, reward } = this._data;
  126. const res = await AppKernel.stores.seasonPassStore.reqSeasonPassTasksClaim(task_id);
  127. if (res) AppKernel.ui.open(UIViewID.RewardView, { reward });
  128. }
  129. handleBtnClaimedClick() {
  130. }
  131. }