TaskItem.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { _decorator, Component, Node, Label, ProgressBar, Button, Vec3, Sprite, SpriteFrame, systemEvent, SystemEventType, EventTouch, tween, UITransform, Vec2 } from 'cc';
  2. import { Data } from '../core/data/Data';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http, HttpResponseCode } from '../core/net/Http';
  5. import { Sound } from '../core/sound/Sound';
  6. import { UITween } from '../core/ui/tween/UITween';
  7. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  8. import { WindowSystem } from '../core/ui/window/WindowSystem';
  9. import { StringUtils } from '../core/utils/StringUtils';
  10. import { Utils } from '../core/utils/Utils';
  11. import { UserData } from '../data/UserData';
  12. import { Guide, GuideType } from '../guide/Guide';
  13. import { FormationData } from '../hero/formation/FormationData';
  14. import { ReportThinking } from '../ReportThinking';
  15. import { Trajectory } from '../turntable/Trajectory';
  16. import { Task } from './Task';
  17. import { TaskData } from './TaskData';
  18. import { TaskInfoData } from './TaskInfoData';
  19. const { ccclass, property, requireComponent } = _decorator;
  20. /**
  21. * 任务Item
  22. * @author 孙雄
  23. */
  24. @ccclass('TaskItem')
  25. @requireComponent(Http)
  26. export class TaskItem extends Component {
  27. @property({ type: Node, tooltip: "item内容节点" }) itemContentNode: Node;
  28. @property({ type: Label, tooltip: "奖励数量文本" }) numLabel: Label;
  29. @property({ type: Label, tooltip: "任务描述文本" }) desLabel: Label;
  30. @property({ type: Label, tooltip: "任务进度文本" }) progressLabel: Label;
  31. @property({ type: ProgressBar, tooltip: "任务进度条" }) taskPorgress: ProgressBar;
  32. @property({ type: Node, tooltip: "领取按钮节点" }) getNode: Node;
  33. @property({ type: Node, tooltip: "已领取按钮节点" }) receivedNode: Node;
  34. @property({ type: Node, tooltip: "前往节点" }) gotoNode: Node;
  35. @property({ type: Node, tooltip: "未完成节点" }) incompleteNode: Node;
  36. @property({ type: Node, tooltip: "背景节点" }) bgNode: Node;
  37. @property({ type: SpriteFrame, tooltip: "特效资源" }) effectSpriteFrame: SpriteFrame;
  38. @property({ type: UITween, tooltip: "领取1级任务特效" }) rewardTween: UITween;
  39. @property({ type: Node, tooltip: "特效开始位置节点" }) starNode: Node;
  40. @property({ type: Sound, tooltip: "飞行音效" }) flySound: Sound;
  41. private rewardType = 1;//1段任务,2段任务
  42. private data: TaskInfoData;
  43. private targetNode: Node;
  44. private effectNode: Node;
  45. private startWolrdPostion;
  46. private addNums = [];
  47. start() {
  48. this.startWolrdPostion = this.node.worldPosition.clone();
  49. }
  50. public onDataChange(data: TaskInfoData, targetNode: Node, bgNode: Node) {
  51. this.data = data;
  52. this.effectNode = bgNode;
  53. this.targetNode = targetNode;
  54. this.setView();
  55. }
  56. private setView() {
  57. if (this.data.isReceive1 && this.data.isReceive2) {//全部领取
  58. this.receivedNode.active = true;
  59. this.getNode.active = this.gotoNode.active = this.incompleteNode.active = false;
  60. this.rewardType = 2;
  61. } else {
  62. if (this.data.isReceive1) {
  63. this.rewardType = 2;
  64. } else {
  65. this.rewardType = 1;
  66. }
  67. if (this.data.count1 <= this.data.serverCount && this.rewardType == 1 || this.data.count2 <= this.data.serverCount && this.rewardType == 2) {//可领取
  68. this.getNode.active = true;
  69. this.receivedNode.active = this.gotoNode.active = this.incompleteNode.active = false;
  70. } else {
  71. if (this.data.openId != -1) {
  72. this.gotoNode.active = true;
  73. this.receivedNode.active = this.getNode.active = this.incompleteNode.active = false;
  74. } else {
  75. this.incompleteNode.active = true;
  76. this.receivedNode.active = this.getNode.active = this.gotoNode.active = false;
  77. }
  78. }
  79. }
  80. this.numLabel.string = this.data['activeValue' + this.rewardType];
  81. this.desLabel.string = StringUtils.format(this.data.des, this.data['count' + this.rewardType]);
  82. this.taskPorgress.progress = this.data.serverCount / this.data['count' + this.rewardType];
  83. this.progressLabel.string = (this.data.serverCount > this.data['count' + this.rewardType] ? this.data['count' + this.rewardType] : this.data.serverCount) + '/' + this.data['count' + this.rewardType];
  84. }
  85. /**前往 */
  86. private goto(e: EventTouch) {
  87. let windowList = WindowSystem.getWindowList();
  88. for (let i = 0; i < windowList.length; i++) {
  89. if (windowList[i].getComponent(Task)) {
  90. windowList[i].close();
  91. }
  92. }
  93. switch (this.data.openId) {
  94. case 0: {
  95. Guide.create(GuideType.Train);
  96. break;
  97. }
  98. case 5: WindowSystem.open('prefabs/ui/recruit/recruit', WindowOpenMode.NotCloseAndAdd); break;
  99. case 6: {
  100. if (DataSystem.getData(UserData).level >= 2) {
  101. WindowSystem.open('prefabs/ui/turntable/turntable', WindowOpenMode.NotCloseAndAdd);
  102. } else {
  103. WindowSystem.showTips('关卡达到2级开启');
  104. }
  105. break
  106. };
  107. case 7: {
  108. if (DataSystem.getData(UserData).level >= 3) {
  109. WindowSystem.open('prefabs/ui/expropriationGrain/expropriationGrain', WindowOpenMode.NotCloseAndAdd);
  110. } else {
  111. WindowSystem.showTips('关卡达到3级开启');
  112. }
  113. break
  114. };
  115. case 1: {
  116. if (DataSystem.getData(FormationData).has(5) && DataSystem.getData(FormationData).get(5)) {
  117. Guide.create(GuideType.Marshal);
  118. } else {
  119. WindowSystem.showTips('请先上阵统帅武将');
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. private isTouch = false;
  126. private async onGet(e: EventTouch) {
  127. if (this.isTouch) {
  128. return;
  129. }
  130. this.isTouch = true;
  131. let result = await this.getComponent(Http).send('/api/task/ReceiveTask', { id: this.data.id, rewardType: this.rewardType })
  132. this.isTouch = false;
  133. if (result && result.code == HttpResponseCode.Success) {
  134. ReportThinking.task(this.desLabel.string);
  135. this.getNode.active = false;
  136. let taskData = DataSystem.getData(TaskData);
  137. this.addNums.push(result.data);
  138. for (let i = 0; i < taskData.tasks.length; i++) {
  139. if (taskData.tasks[i].id == this.data.id) {
  140. taskData.tasks[i]['isReceive' + this.rewardType] = true;
  141. this.data['isReceive' + this.rewardType] = true;
  142. break;
  143. }
  144. }
  145. if (this.rewardType == 1) {
  146. this.rewardType = 2;
  147. this.showEffect();
  148. this.rewardTween.play();
  149. } else {
  150. this.showEffect();
  151. DataSystem.getData(TaskData).getTaskGiftNum++;
  152. }
  153. }
  154. }
  155. private itemReFrush() {
  156. this.bgNode.position = new Vec3(800, 0, 0);
  157. this.data.isReceive1 = true;
  158. DataSystem.getData(TaskData).getTaskGiftNum++;
  159. this.setView();
  160. }
  161. private showEffect() {
  162. this.flySound.play();
  163. this.scheduleOnce(() => {
  164. this.addNums.length > 0 && (DataSystem.getData(TaskData).activityNum += this.addNums.shift());
  165. }, 1);
  166. for (let i = 0; i < this.data['activeValue' + this.rewardType]; i++) {
  167. let node = new Node();
  168. node.addComponent(Sprite);
  169. node.getComponent(Sprite)!.spriteFrame = this.effectSpriteFrame;
  170. let pos = this.starNode.getWorldPosition();
  171. let targetY = 380 + this.node.position.y + this.node.worldPosition.y - this.startWolrdPostion.y;
  172. node.setPosition(new Vec3(-238, targetY));
  173. this.effectNode.addChild(node);
  174. let trajectory = node.addComponent(Trajectory);
  175. trajectory.startPoint = new Vec3(-238, targetY);
  176. trajectory.ctrl1Point = new Vec3(Utils.random_both(80, -80), Utils.random_both(0, -200));
  177. trajectory.ctrl2Point = new Vec3(Utils.random_both(50, -50), Utils.random_both(100, -100));
  178. trajectory.endPoint = this.targetNode.position;
  179. }
  180. }
  181. }