QuestMainItem.js 7.6 KB

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