QuestMainItem.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const Api = require('../net/Api');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. questNode: cc.Node,
  6. questFrames: {
  7. tooltip: '任务图标',
  8. default: [],
  9. type: [cc.SpriteFrame],
  10. },
  11. questBtns: [cc.Node],
  12. starNode: {
  13. tooltip: '星星节点',
  14. default: [],
  15. type: [cc.Node]
  16. },
  17. giftFrames: {
  18. tooltip: '奖励图片素材',
  19. default: [],
  20. type: [cc.SpriteFrame]
  21. },
  22. starFrames: {
  23. tooltip: '星星图片素材',
  24. default: [],
  25. type: [cc.SpriteFrame]
  26. },
  27. progressLabel: {
  28. tooltip: '奖励数量',
  29. default: null,
  30. type: cc.Label
  31. },
  32. progressBar: cc.ProgressBar,
  33. questTitle: {
  34. tooltip: '任务标题',
  35. default: null,
  36. type: cc.Label
  37. },
  38. qusetMsg: {
  39. tooltip: '任务说明',
  40. default: null,
  41. type: cc.Label
  42. }
  43. },
  44. onLoad () {
  45. this.giftMap = ['diamond', 'coin', 'ticket']
  46. },
  47. start () {
  48. },
  49. init (parent, tasks, sn, questIndex) {
  50. this.parent = parent
  51. this.quest = parent.quest
  52. this.tasks = tasks
  53. this.questIndex = questIndex
  54. sn = this.sn = parseInt(sn)
  55. // 设置当前任务图标
  56. this.questNode.getComponent('cc.Sprite').spriteFrame = this.questFrames[this.questIndex]
  57. // 设置当前任务等级内容
  58. this._setTaskItem(sn)
  59. },
  60. _setTaskItem (sn) {
  61. let task = this.task = this.tasks.find(n => {
  62. if(sn == 0) {
  63. return n.sn == 1
  64. } else {
  65. return n.sn == sn
  66. }
  67. })
  68. if(sn == 5 && task.status == 2) {
  69. this._setMaxItem()
  70. } else {
  71. let level = sn == 0 ? 0 : sn - 1
  72. let nameLevelMap = ['I', 'II', 'III', 'IV', 'V']
  73. let giftType
  74. let giftCount
  75. this.giftMap.forEach(n => {
  76. if(task[n] > 0) {
  77. giftType = n
  78. giftCount = task[n]
  79. }
  80. })
  81. // 设置当前任务标题
  82. this.questTitle.string = `${task.name}(${nameLevelMap[level]})`
  83. // 设置当前任务说明内容
  84. this.qusetMsg.string = task.msg.replace('${num}', task.totalAimValue)
  85. // 设置当前任务星级
  86. this._setStarLevel(level)
  87. // 设置任务阶段进度条
  88. let aimValue = task.aimValue > task.totalAimValue ? task.totalAimValue : task.aimValue
  89. this._setProgress(aimValue, task.totalAimValue)
  90. // 设置任务领取按钮
  91. this._setGiftBtn(task.status, giftType, giftCount)
  92. }
  93. },
  94. _setMaxItem () {
  95. let task = this.tasks[4]
  96. // 设置当前任务标题
  97. this.questTitle.string = `${task.name}(V)`
  98. // 设置当前任务说明内容
  99. this.qusetMsg.string = task.msg.replace('${num}', task.totalAimValue)
  100. // 设置当前任务星级
  101. this._setStarLevel(5)
  102. // 设置任务阶段进度条
  103. let aimValue = task.aimValue > task.totalAimValue ? task.totalAimValue : task.aimValue
  104. this._setProgress(aimValue, task.totalAimValue)
  105. // 设置任务领取按钮
  106. this._setGiftBtn(2)
  107. },
  108. /**
  109. * 领取按钮点击
  110. */
  111. handleGiftBtn () {
  112. Api.httpPost({
  113. url: "/task/gain.do",
  114. data: {
  115. taskId: this.task.taskId,
  116. sn: this.task.sn
  117. },
  118. success: (res) => {
  119. // 更新全局userInfo
  120. this.quest.updateUserInfo(this.task.coin, this.task.diamond, this.task.ticket)
  121. // 显示领取奖品动画
  122. this.quest.showActGift({
  123. ticket: this.task.ticket,
  124. diamond: this.task.diamond,
  125. coin: this.task.coin
  126. })
  127. // 领取成功,更新当前按钮状态到下一阶段
  128. if(this.sn < 5) {
  129. this.sn += 1
  130. this._setTaskItem(this.sn)
  131. } else {
  132. this._setMaxItem()
  133. }
  134. },
  135. fail: () => {
  136. // 领取失败
  137. }
  138. })
  139. },
  140. /**
  141. * 设置任务领取状态按钮
  142. * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
  143. * @param {string} giftType 奖励类型[ticket: 艺人券, diamond: 钻石, coin: 金币]
  144. * @param {number} giftCount 奖励数量
  145. */
  146. _setGiftBtn (status, giftType, giftCount) {
  147. this.questBtns.forEach(n => {
  148. n.active = false
  149. })
  150. let btn
  151. // 根据状态选择按钮
  152. switch (status) {
  153. case 0:
  154. btn = this.questBtns[1]
  155. break;
  156. case 1:
  157. btn = this.questBtns[0]
  158. break;
  159. case 2:
  160. btn = this.questBtns[2]
  161. break;
  162. default:
  163. break;
  164. }
  165. btn.active = true
  166. // 设置图标和数量
  167. if(status != 2) {
  168. let countNode = cc.find('/count_label', btn)
  169. let spriteNode = cc.find('/sprite', btn)
  170. let giftSprite
  171. this.giftMap.forEach((value, index) => {
  172. if(value == giftType) {
  173. giftSprite = this.giftFrames[index]
  174. }
  175. })
  176. countNode.getComponent('cc.Label').string = `${giftCount}`
  177. spriteNode.getComponent('cc.Sprite').spriteFrame = giftSprite
  178. }
  179. },
  180. /**
  181. * 设置任务阶段进度条
  182. * @param {number} current 当前值
  183. * @param {number} total 阶段总值
  184. */
  185. _setProgress (current, total) {
  186. let _progress = (current / total).toFixed(1)
  187. console.log(_progress);
  188. this.progressBar.progress = _progress
  189. this.progressLabel.string = `${current} / ${total}`
  190. },
  191. /**
  192. * 设置当前任务星级
  193. * @param {number} level 星级
  194. */
  195. _setStarLevel (level) {
  196. this.starNode.forEach((node, index) => {
  197. if(index < level) {
  198. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1]
  199. } else {
  200. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0]
  201. }
  202. })
  203. }
  204. });