NodeItem.ts 6.2 KB

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