QuestDailyItem.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const QuestApi = require('../net/QuestApi');
  2. const DWTool = require("../utils/DWTool");
  3. const QuestDailyMissionType = require("../utils/GameEnum").QuestDailyMissionType;
  4. const GameModule = require('../utils/GameModule');
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. questSprite: cc.Sprite,
  9. questBtns: [cc.Node],
  10. giftFrames: {
  11. tooltip: '奖励图片素材',
  12. default: [],
  13. type: [cc.SpriteFrame]
  14. },
  15. questTitle: {
  16. tooltip: '任务标题',
  17. default: null,
  18. type: cc.Label
  19. },
  20. questDetail: {
  21. tooltip: '任务内容',
  22. default: null,
  23. type: cc.Label
  24. },
  25. progressLabel: {
  26. tooltip: '奖励数量',
  27. default: null,
  28. type: cc.Label
  29. },
  30. progressBar: cc.ProgressBar,
  31. },
  32. onLoad () {
  33. this.giftMap = ['diamond', 'coin', 'ticket']
  34. },
  35. start () {
  36. },
  37. init (parent, task, questIndex) {
  38. this.parent = parent;
  39. this.quest = parent.quest;
  40. this.task = task;
  41. this.questIndex = questIndex;
  42. this.questBtns[task.state].active = true;
  43. DWTool.loadResSpriteFrame(`./textures/quest/altas/${task.picId}`)
  44. .then((spriteFrame) => {
  45. this.questSprite.spriteFrame = spriteFrame;
  46. }).catch((err) => {
  47. console.log(err);
  48. });
  49. // 设置任务标题
  50. this.questTitle.string = task.title;
  51. // 设置任务说明内容
  52. this.questDetail.string = task.desc;
  53. // 设置任务奖励类型及数量
  54. this._setGiftBtn();
  55. //设置当前进度
  56. this._setProgress();
  57. },
  58. /**
  59. * 设置任务奖励类型及数量
  60. */
  61. _setGiftBtn () {
  62. this.questBtns.forEach(n => {
  63. n.active = false;
  64. });
  65. let btn;
  66. // 根据状态选择按钮
  67. switch (this.task.state) {
  68. case QuestDailyMissionType.NoFinished:
  69. btn = this.questBtns[1];
  70. break;
  71. case QuestDailyMissionType.CanGain:
  72. btn = this.questBtns[0];
  73. break;
  74. case QuestDailyMissionType.AlreadyGet:
  75. btn = this.questBtns[2];
  76. break;
  77. default:
  78. break;
  79. }
  80. btn.active = true;
  81. // 设置图标和数量
  82. if(this.task.state != QuestDailyMissionType.AlreadyGet) {
  83. let countNode = cc.find('/awardNode/count_label', btn);
  84. let spriteNode = cc.find('/awardNode/sprite', btn);
  85. let awardLabelNode = cc.find('/awardLabel', btn);
  86. let awardNode = cc.find('/awardNode', btn);
  87. let otherNode = cc.find('/otherLabel', btn);
  88. // this.giftItem.giftFrame = spriteFrame;
  89. var awardString = '奖励';
  90. if (this.task.state == QuestDailyMissionType.CanGain) {
  91. awardString = '获得';
  92. }
  93. let _count = 0;
  94. let _type = '';
  95. let _sprite = null;
  96. this.giftMap.forEach((value, index) => {
  97. if(this.task[value] > 0) {
  98. _count = this.task[value];
  99. _type = value;
  100. _sprite = this.giftFrames[index];
  101. }
  102. });
  103. awardNode.active = true;
  104. otherNode.active = false;
  105. spriteNode.getComponent('cc.Sprite').spriteFrame = _sprite;
  106. countNode.getComponent('cc.Label').string = `${DWTool.coinParseNoFixed(_count)}`;
  107. awardLabelNode.getComponent('cc.Label').string = awardString;
  108. }
  109. },
  110. /**
  111. * 设置任务阶段进度条
  112. * @param {number} current 当前值
  113. * @param {number} total 阶段总值
  114. */
  115. _setProgress () {
  116. let total = this.task.totalCount;
  117. let current = this.task.userCount;
  118. let _progress = (current / total).toFixed(1);
  119. this.progressBar.progress = _progress;
  120. if (current > total) {
  121. this.progressLabel.string = `${total}/${total}`;
  122. } else {
  123. this.progressLabel.string = `${current}/${total}`;
  124. }
  125. },
  126. /**
  127. * 领取按钮点击
  128. */
  129. handleGiftBtn () {
  130. QuestApi.postGetDailyTaskReward(this.task.id, (res) => {
  131. GameModule.audioMng.playGetAward();
  132. // 更新全局userInfo
  133. this.quest.updateUserInfo(this.task.coin, this.task.diamond, this.task.ticket);
  134. this.task.state = QuestDailyMissionType.AlreadyGet;
  135. // 领取成功,更新状态
  136. this.questBtns[QuestDailyMissionType.CanGain].active = false;
  137. this.questBtns[QuestDailyMissionType.AlreadyGet].active = true;
  138. // 显示领取奖品动画
  139. this.quest.showActGift({
  140. ticket: this.task.ticket,
  141. diamond: this.task.diamond,
  142. coin: this.task.coin
  143. })
  144. this.parent.updateUserDegree();
  145. }, (errCode, errMsg) => {
  146. // 领取失败
  147. Global.commonAlert.showCommonErrorAlert("领取奖励失败");
  148. });
  149. }
  150. });