QuestDailyItem.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. giftNode: {
  13. tooltip: '奖励图标节点',
  14. default: null,
  15. type: cc.Node
  16. },
  17. giftCounts: {
  18. tooltip: '奖励数量',
  19. default: null,
  20. type: cc.Label
  21. },
  22. giftFrames: {
  23. tooltip: '奖励图片素材',
  24. default: [],
  25. type: [cc.SpriteFrame]
  26. },
  27. questTitle: {
  28. tooltip: '任务标题',
  29. default: null,
  30. type: cc.Label
  31. },
  32. questDetail: {
  33. tooltip: '任务内容',
  34. default: null,
  35. type: cc.Label
  36. },
  37. questAct: {
  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, task, questIndex) {
  49. this.parent = parent
  50. this.quest = parent.quest
  51. this.task = task
  52. this.questIndex = questIndex
  53. this.questBtns[task.state].active = true
  54. // 设置当前任务图标
  55. this.questNode.getComponent('cc.Sprite').spriteFrame = this.questFrames[this.questIndex]
  56. // 设置任务标题
  57. this.questTitle.string = task.title
  58. // 设置任务说明内容
  59. this.questDetail.string = task.desc
  60. // 设置任务活跃度奖励
  61. this.questAct.string = `活跃度 + ${task.degree}`
  62. // 设置任务奖励类型及数量
  63. this._setGift()
  64. },
  65. /**
  66. * 设置任务奖励类型及数量
  67. */
  68. _setGift () {
  69. let _count = 0
  70. let _type = ''
  71. let _sprite = null
  72. this.giftMap.forEach((value, index) => {
  73. if(this.task[value] > 0) {
  74. _count = this.task[value]
  75. _type = value
  76. _sprite = this.giftFrames[index]
  77. }
  78. })
  79. this.giftNode.getComponent('cc.Sprite').spriteFrame = _sprite
  80. this.giftCounts.string = `x ${_count}`
  81. },
  82. /**
  83. * 领取按钮点击
  84. */
  85. handleGiftBtn () {
  86. Api.httpPost({
  87. url: "/daily/getDailyTaskReward",
  88. data: {
  89. id: this.task.id
  90. },
  91. success: (res) => {
  92. // 更新全局userInfo
  93. this.quest.updateUserInfo(this.task.coin, this.task.diamond, this.task.ticket)
  94. // 领取成功,更新状态
  95. this.parent.updateStatus(this.task)
  96. this.questBtns[this.task.state].active = false;
  97. this.questBtns[2].active = true;
  98. // 显示领取奖品动画
  99. this.quest.showActGift({
  100. ticket: this.task.ticket,
  101. diamond: this.task.diamond,
  102. coin: this.task.coin
  103. })
  104. },
  105. fail: () => {
  106. // 领取失败
  107. }
  108. })
  109. }
  110. });