NodeItem.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
  6. import { _decorator, Button, Component, Enum, instantiate, Label, Node, NodePool, Prefab, ProgressBar, Sprite, SpriteFrame, tween, UIOpacity } from 'cc';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('NodeItem')
  9. export class NodeItem extends Component {
  10. @property(SpriteFrame)
  11. private img_bg_free_node: SpriteFrame = null;
  12. @property(SpriteFrame)
  13. private img_bg_dlx_node: SpriteFrame = null;
  14. @property(Node)
  15. private dlx: Node = null;
  16. @property(Node)
  17. private progress: Node = null;
  18. @property(Node)
  19. private free: Node = null;
  20. @property(Prefab)
  21. private prop_sc: Prefab = null;
  22. @property(Prefab)
  23. private prop_gc: Prefab = null;
  24. private _data: any = {};
  25. onLoad() {
  26. addBtnClick(this.dlx, this.onDlxBtnClick, this);
  27. addBtnClick(this.free, this.onFreeBtnClick, this);
  28. }
  29. initItem(data: any = {}) {
  30. this._data = data;
  31. this.updateFree();
  32. this.updateDeluxe();
  33. this.updateProgress();
  34. }
  35. updateFree() {
  36. const { node } = this._data;
  37. if (!node) return;
  38. const { free, unlocked } = node ?? {};
  39. const { reward, claimed } = free ?? {};
  40. const rewards = this.free.getChildByName('rewards');
  41. const lock = this.free.getChildByName('lock');
  42. const highlight = this.free.getChildByName('highlight');
  43. const received = this.free.getChildByName('received');
  44. const isEmpty = Object.values(reward).every(val => val == 0);
  45. this.free.active = !isEmpty;
  46. for (const [key, val] of Object.entries(reward)) {
  47. if (!this[`prop_${key}`] || val == 0) continue;
  48. const item = rewards.getChildByName(`prop_${key}`) ?? instantiate(this[`prop_${key}`]);
  49. item.parent = rewards;
  50. item.active = true;
  51. item.getComponent(Sprite).spriteFrame = this.img_bg_free_node;
  52. const num = item.getChildByName('num');
  53. const label = num.getComponent(Label);
  54. if (key == "sc" || key == "gc") {
  55. label.string = `${(Number(val) / 100).toFixed(2)}`;
  56. } else {
  57. label.string = `${val}`;
  58. }
  59. }
  60. rewards.active = true;
  61. lock.active = unlocked == 0;
  62. highlight.active = claimed == 0 && unlocked == 1;
  63. received.active = claimed == 1;
  64. this.stopLightBlink(highlight);
  65. if (claimed == 0 && unlocked == 1) this.startLightBlink(highlight);
  66. }
  67. updateDeluxe() {
  68. const { node, is_deluxe } = this._data;
  69. if (!node) return;
  70. const { deluxe, unlocked } = node ?? {};
  71. const { reward, claimed } = deluxe ?? {};
  72. const rewards = this.dlx.getChildByName('rewards');
  73. const lock = this.dlx.getChildByName('lock');
  74. const highlight = this.dlx.getChildByName('highlight');
  75. const received = this.dlx.getChildByName('received');
  76. const isEmpty = Object.values(reward).every(val => val == 0);
  77. this.dlx.active = !isEmpty;
  78. for (const [key, val] of Object.entries(reward)) {
  79. if (!this[`prop_${key}`] || val == 0) continue;
  80. const item = rewards.getChildByName(`prop_${key}`) ?? instantiate(this[`prop_${key}`]);
  81. item.parent = rewards;
  82. item.active = true;
  83. item.getComponent(Sprite).spriteFrame = this.img_bg_dlx_node;
  84. const num = item.getChildByName('num');
  85. const label = num.getComponent(Label);
  86. if (key == "sc" || key == "gc") {
  87. label.string = `${(Number(val) / 100).toFixed(2)}`;
  88. } else {
  89. label.string = `${val}`;
  90. }
  91. }
  92. rewards.active = true;
  93. lock.active = unlocked == 0 || is_deluxe == 0;
  94. highlight.active = claimed == 0 && unlocked == 1 && is_deluxe == 1;
  95. received.active = claimed == 1 && is_deluxe == 1;
  96. this.stopLightBlink(highlight);
  97. if (claimed == 0 && unlocked == 1 && is_deluxe == 1) this.startLightBlink(highlight);
  98. }
  99. updateProgress() {
  100. const { node, current_xp, is_deluxe } = this._data;
  101. if (!node) return;
  102. const { required_xp } = node ?? {};
  103. const progressBar = this.progress.getComponent(ProgressBar);
  104. const label = this.progress.getChildByName('label').getComponent(Label);
  105. label.string = required_xp + "XP";
  106. if (current_xp == required_xp) {
  107. progressBar.progress = 0.53;
  108. } else if (current_xp > required_xp) {
  109. progressBar.progress = 1;
  110. } else if (current_xp < required_xp) {
  111. progressBar.progress = 0;
  112. } else {
  113. }
  114. }
  115. private async onDlxBtnClick() {
  116. const { reward_id, reward } = this._data?.node?.deluxe ?? {};
  117. const res = await AppKernel.stores.seasonPassStore.reqSeasonPassPrizeClaim(reward_id);
  118. if (res) AppKernel.ui.open(UIViewID.RewardView, { reward });
  119. }
  120. private async onFreeBtnClick() {
  121. const { reward_id, reward } = this._data?.node?.free ?? {};
  122. const res = await AppKernel.stores.seasonPassStore.reqSeasonPassPrizeClaim(reward_id);
  123. if (res) AppKernel.ui.open(UIViewID.RewardView, { reward });
  124. }
  125. startLightBlink(lightNode: Node) {
  126. const uiOpacity = lightNode.getComponent(UIOpacity) || lightNode.addComponent(UIOpacity);
  127. // 先重置初始透明度
  128. uiOpacity.opacity = 255;
  129. tween(uiOpacity)
  130. .to(0.6, { opacity: 0 })
  131. .to(0.6, { opacity: 255 })
  132. .union()
  133. .repeatForever() // 无限循环闪烁
  134. .start();
  135. }
  136. // 停止闪烁
  137. stopLightBlink(lightNode: Node) {
  138. const uiOpacity = lightNode.getComponent(UIOpacity);
  139. if (!uiOpacity) return;
  140. tween(uiOpacity).stop();
  141. uiOpacity.opacity = 255; // 恢复不透明
  142. }
  143. }