QuestMainItem.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const Api = require('../net/Api');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. questNode: cc.Node,
  6. questFrames: [cc.SpriteFrame],
  7. questBtns: [cc.Node],
  8. starNode: {
  9. tooltip: '星星节点',
  10. default: [],
  11. type: [cc.Node]
  12. },
  13. giftFrames: {
  14. tooltip: '奖励图片素材',
  15. default: [],
  16. type: [cc.SpriteFrame]
  17. },
  18. starFrames: {
  19. tooltip: '星星图片素材',
  20. default: [],
  21. type: [cc.SpriteFrame]
  22. },
  23. progressLabel: {
  24. tooltip: '奖励数量',
  25. default: null,
  26. type: cc.Label
  27. },
  28. progressBar: cc.ProgressBar,
  29. questTitle: {
  30. tooltip: '任务标题',
  31. default: null,
  32. type: cc.Label
  33. },
  34. },
  35. onLoad () {
  36. this.giftMap = ['diamond', 'coin', 'ticket']
  37. },
  38. start () {
  39. },
  40. init (tasks, sn) {
  41. this.tasks = tasks
  42. this.sn = parseInt(sn)
  43. let task = this.task = tasks[sn]
  44. let nameLevelMap = ['I', 'II', 'III', 'IV', 'V']
  45. let giftType
  46. let giftCount
  47. this.giftMap.forEach(n => {
  48. if(task[n] > 0) {
  49. giftType = n
  50. giftCount = task[n]
  51. }
  52. })
  53. // 设置当前任务标题
  54. this.questTitle.string = `${task.name}(${nameLevelMap[sn]}) ${task.msg + task.totalAimValue}`
  55. // 设置当前任务星级
  56. this._setStarLevel(sn)
  57. // 设置任务阶段进度条
  58. this._setProgress(task.aimValue, task.totalAimValue)
  59. // 设置任务领取按钮
  60. this._setGiftBtn(task.status, giftType, giftCount)
  61. },
  62. /**
  63. * 领取按钮点击
  64. */
  65. handleGiftBtn () {
  66. Api.httpPost({
  67. url: "/task/gain.do",
  68. data: {
  69. taskId: this.task.taskId,
  70. sn: this.task.sn
  71. },
  72. success: (res) => {
  73. // 领取成功
  74. this.sn += 1
  75. // this.init(this.tasks, this.sn)
  76. },
  77. fail: () => {
  78. // 领取失败
  79. }
  80. })
  81. },
  82. /**
  83. * 设置任务领取状态按钮
  84. * @param {number} status 任务状态[0 : 未完成, 1 : 完成可领取, 2 : 完成已领取]
  85. * @param {string} giftType 奖励类型[ticket: 艺人券, diamond: 钻石, coin: 金币]
  86. * @param {number} giftCount 奖励数量
  87. */
  88. _setGiftBtn (status, giftType, giftCount) {
  89. let btn
  90. if(status == 1) {
  91. btn = this.questBtns[0]
  92. } else {
  93. btn = this.questBtns[1]
  94. }
  95. btn.active = true
  96. let countNode = cc.find('/count_label', btn)
  97. let spriteNode = cc.find('/sprite', btn)
  98. let giftSprite
  99. this.giftMap.forEach((value, index) => {
  100. if(value == giftType) {
  101. giftSprite = this.giftFrames[index]
  102. }
  103. })
  104. countNode.getComponent('cc.Label').string = `x ${giftCount}`
  105. spriteNode.getComponent('cc.Sprite').spriteFrame = giftSprite
  106. },
  107. /**
  108. * 设置任务阶段进度条
  109. * @param {number} current 当前值
  110. * @param {number} total 阶段总值
  111. */
  112. _setProgress (current, total) {
  113. let _progress = (current / total).toFixed(1)
  114. console.log(_progress);
  115. this.progressBar.progress = _progress
  116. this.progressLabel.string = `${current} / ${total}`
  117. },
  118. /**
  119. * 设置当前任务星级
  120. * @param {number} level 星级
  121. */
  122. _setStarLevel (level) {
  123. this.starNode.forEach((node, index) => {
  124. if(index < level) {
  125. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[1]
  126. } else {
  127. node.getComponent('cc.Sprite').spriteFrame = this.starFrames[0]
  128. }
  129. })
  130. }
  131. });