QuestDailyItem.js 3.3 KB

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