ActivityItem.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { _decorator, Component, Node, SpriteFrame, Animation, Sprite, Label, Button, RichText, Quat } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { Http, HttpResponseCode } from '../core/net/Http';
  4. import { OpenWindow } from '../core/ui/window/OpenWindow';
  5. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  6. import { WindowSystem } from '../core/ui/window/WindowSystem';
  7. import { ConfigData } from '../data/ConfigData';
  8. import { Config } from '../launch/Config';
  9. import { ActivityInfoData } from './ActivityInfoData';
  10. import { TaskData } from './TaskData';
  11. const { ccclass, property, requireComponent } = _decorator;
  12. /***活跃度奖励 */
  13. @ccclass('ActivityItem')
  14. @requireComponent(Http)
  15. export class ActivityItem extends Component {
  16. @property({ type: Animation, tooltip: "光效节点" }) animation: Animation;
  17. @property({ type: Node, tooltip: "红包节点" }) redPacketNode: Node;
  18. @property({ type: Node, tooltip: "宝箱节点" }) boxNode: Node;
  19. @property({ type: Label, tooltip: "宝箱节点" }) activityLabel: Label;
  20. @property({ type: [SpriteFrame], tooltip: "红包状态资源合集" }) redPacketStates: Array<SpriteFrame> = [];
  21. @property({ type: [SpriteFrame], tooltip: "宝箱状态资源合集" }) boxStates: Array<SpriteFrame> = [];
  22. @property({ type: OpenWindow, tooltip: "宝箱领取窗口" }) boxGiftWin: OpenWindow;
  23. @property({ type: Button, tooltip: "宝箱领取窗口" }) getBoxButton: Button;
  24. @property({ type: Button, tooltip: "红包领取窗口" }) getRedpackeButton: Button;
  25. @property({ type: RichText, tooltip: "红包领取窗口" }) tipsLabel: RichText;
  26. @property({ type: Node, tooltip: "气泡节点" }) tipNode: Node;
  27. @property({ tooltip: "活跃度奖励ID" }) activityId: number = 1;
  28. @property({ type: Sprite, tooltip: "汽泡图片" }) tipsIconSprite: Sprite;
  29. @property({ type: Animation, tooltip: "宝箱可领取" }) boxFinishedAni: Animation;
  30. private type = 1;//1宝箱 ,2红包
  31. private activityConfig: any;
  32. private _state = -1;
  33. private currentNode: Node;
  34. private taskData: TaskData;
  35. /**是否领取过双倍奖励 */
  36. private isReceiveDouble = false;
  37. private isReceiveSingle = false;
  38. onLoad() {
  39. this.animation.node.active = false;
  40. this.activityConfig = DataSystem.getData(ConfigData).get('taskActive');
  41. this.taskData = DataSystem.getData(TaskData);
  42. this.type = this.activityConfig[this.activityId].icon;
  43. this.redPacketNode.active = this.type == 2;
  44. this.boxNode.active = this.type == 1;
  45. this.activityLabel.string = this.activityConfig[this.activityId].activevalue;
  46. if (this.type == 1) {
  47. this.tipsLabel.string = '<color=#4A2312>随机数量<color=#EA4421>粮草</color>或<color=#EA4421>红包币</color></color>';
  48. } else {
  49. this.tipsLabel.string = `<color=#4A2312>${this.taskData.minNum}-${this.taskData.maxNum}<color=#EA4421>的红包币</color></color>`;
  50. }
  51. this.setBoxState();
  52. }
  53. update() {
  54. DataSystem.watch(TaskData, 'activityNum') && this.setBoxState();
  55. DataSystem.watch(TaskData, 'getActiveBoxGiftNum') && this.setBoxState();
  56. }
  57. private setBoxState() {
  58. this.currentNode = this.boxNode.active ? this.boxNode : this.redPacketNode;
  59. let activevalue = this.activityConfig[this.activityId].activevalue;
  60. let nowAcitvityNum = this.taskData.activityNum;
  61. for (let i = 0; i < this.taskData.activitys.length; i++) {
  62. this.isReceiveDouble;
  63. if (this.taskData.activitys[i].id == this.activityId) {
  64. this.isReceiveDouble = this.taskData.activitys[i].isReceiveDouble;
  65. this.isReceiveSingle = this.taskData.activitys[i].isReceiveSingle;
  66. break;
  67. }
  68. }
  69. if (nowAcitvityNum >= activevalue) {
  70. if (this.activityConfig[this.activityId].isdouble) {
  71. if (!this.isReceiveDouble) {
  72. this.state = ActivityState.Finished;
  73. } else {
  74. this.state = ActivityState.Get;
  75. }
  76. } else {
  77. if (!this.isReceiveSingle) {
  78. this.state = ActivityState.Finished;
  79. } else {
  80. this.state = ActivityState.Get;
  81. }
  82. }
  83. } else {
  84. this.state = ActivityState.Lock;
  85. }
  86. }
  87. public set state(value: number) {
  88. if (this._state != value) {
  89. this._state = value;
  90. this.setShowState();
  91. }
  92. }
  93. private setShowState() {
  94. switch (this._state) {
  95. case ActivityState.Lock:
  96. this.animation.node.active = false;
  97. this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[0] : this.redPacketStates[0];
  98. // this.getBoxButton.interactable = true;
  99. // this.getRedpackeButton.interactable = true;
  100. break;
  101. case ActivityState.Finished:
  102. this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[0] : this.redPacketStates[1];
  103. this.animation.node.active = true;
  104. this.animation.play();
  105. this.getBoxButton.node.active = this.type == 1;
  106. this.getRedpackeButton.node.active = this.type == 2;
  107. // this.getBoxButton.interactable = true;
  108. // this.getRedpackeButton.interactable = true;
  109. this.boxFinishedAni.play();
  110. break;
  111. case ActivityState.Get:
  112. this.boxFinishedAni.stop();
  113. this.animation.stop();
  114. this.animation.node.active = false;
  115. this.currentNode.getComponent(Sprite).spriteFrame = this.type == 1 ? this.boxStates[1] : this.redPacketStates[2];
  116. this.currentNode.setRotation(new Quat(0, 0, 0, 0));
  117. break;
  118. }
  119. }
  120. /**领取奖励 */
  121. private onGet() {
  122. if (this._state == ActivityState.Finished) {
  123. let data: ActivityInfoData;
  124. for (let i = 0; i < this.taskData.activitys.length; i++) {
  125. if (this.taskData.activitys[i].id == this.activityId) {
  126. data = this.taskData.activitys[i];
  127. break;
  128. }
  129. }
  130. if (this.activityConfig[this.activityId].icon == 1) {//1宝箱
  131. this.boxGiftWin.open({ id: this.activityId });
  132. } else {//2红包
  133. this.getRedGift();
  134. }
  135. } else if (this._state == ActivityState.Lock) {
  136. if (this.type == 2) {
  137. this.tipsIconSprite.spriteFrame = this.redPacketStates[0];
  138. this.tipsLabel.string = `<color=#4A2312>${this.taskData.minNum}-${DataSystem.getData(TaskData).maxNum}<color=#EA4421> 的红包币</color></color>`;
  139. }
  140. this.tipNode.active = true;
  141. this.scheduleOnce(() => {
  142. this.tipNode.active = false;
  143. }, 1);
  144. } else {
  145. WindowSystem.showTips('奖励已领取');
  146. }
  147. }
  148. /*领取活跃红包 */
  149. private async getRedGift() {
  150. let result = await this.getComponent(Http).send('/api/task/OpenTaskActive', { id: this.activityId });
  151. if (result && result.code == HttpResponseCode.Success) {
  152. let type = -1;
  153. if (this.activityConfig[this.activityId].isdouble) {
  154. if (!this.isReceiveDouble && !this.isReceiveSingle) {
  155. type = 3;
  156. } else {
  157. type = 2;
  158. }
  159. } else {
  160. type = 1;
  161. }
  162. WindowSystem.open("prefabs/ui/redEnvelopes", WindowOpenMode.NotCloseAndCover, [{ type: type, num: result.data.rewardNum, id: this.activityId, isShowBtn: type == 3 }]);
  163. }
  164. }
  165. }
  166. /**0未解锁,1完成可领取,2已领取 */
  167. export enum ActivityState {
  168. Lock,
  169. Finished,
  170. Get
  171. }