QuestMainItem.js 6.9 KB

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