QuestDaily.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const Api = require('../net/Api');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. questItem: {
  6. default: null,
  7. type: cc.Node,
  8. },
  9. questWrap: {
  10. tooltip: '每日任务容器',
  11. default: null,
  12. type: cc.Node
  13. },
  14. actGifts: {
  15. tooltip: '活跃度奖励节点',
  16. default: [],
  17. type: [cc.Node]
  18. },
  19. actGiftSprite: {
  20. tooltip: '活跃度奖励素材',
  21. default: [],
  22. type: [cc.SpriteFrame]
  23. },
  24. actProgress: {
  25. tooltip: '每日活跃度进度',
  26. default: null,
  27. type: cc.ProgressBar
  28. },
  29. userDegreeLabel: {
  30. tooltip: '用户当前活跃度',
  31. default: null,
  32. type: cc.Label
  33. },
  34. _userDegree: 0,
  35. userDegree: {
  36. get () {
  37. return this._userDegree
  38. },
  39. set (data) {
  40. this._userDegree = data
  41. // 设置用户当前活跃度
  42. this.actProgress.progress = data / 100
  43. this.userDegreeLabel.string = data
  44. }
  45. }
  46. },
  47. onLoad () {
  48. this.userDegree = 0
  49. this.dailyDegreeList = []
  50. },
  51. init (quest) {
  52. this.quest = quest
  53. this.questItem.active = false
  54. Api.httpGet({
  55. url: "/daily/getUserDailyInfo",
  56. data: {},
  57. success: ({dailyDegreeList, dailyTaskList, userDegree}) => {
  58. this.dailyDegreeList = dailyDegreeList.sort((a, b) => {
  59. return a.degree - b.degree
  60. })
  61. this.dailyTaskList = dailyTaskList
  62. this.userDegree = parseInt(userDegree)
  63. this._setDegreeList()
  64. this._setTaskList()
  65. }
  66. })
  67. },
  68. /**
  69. * 渲染每日任务列表
  70. */
  71. _setTaskList () {
  72. this.dailyTaskList.forEach((item, index) => {
  73. let questItem = cc.instantiate(this.questItem)
  74. this.questWrap.addChild(questItem)
  75. questItem.active = true
  76. questItem.getComponent('QuestDailyItem').init(this, item, index)
  77. })
  78. },
  79. /**
  80. * 设置活跃度奖励进度条
  81. */
  82. _setDegreeList () {
  83. this.dailyDegreeList.forEach((item, index) => {
  84. let giftFrame = this.actGiftSprite[item.state]
  85. this.actGifts[index].getComponent('cc.Sprite').spriteFrame = giftFrame
  86. })
  87. },
  88. updateStatus (task) {
  89. let _degree = this.userDegree + parseInt(task.degree)
  90. this.userDegree = _degree > 100 ? 100 : _degree
  91. this.dailyDegreeList.forEach((item, index) => {
  92. if(this.userDegree >= item.degree && item.state != 2) {
  93. item.state = 0
  94. }
  95. let giftFrame = this.actGiftSprite[item.state]
  96. this.actGifts[index].getComponent('cc.Sprite').spriteFrame = giftFrame
  97. })
  98. },
  99. /**
  100. * 领取活跃度奖励
  101. * @param {object} event
  102. * @param {number} index 活跃度奖励节点索引
  103. */
  104. handleGiftBtn (event, index) {
  105. let act = this.dailyDegreeList[index]
  106. // state = 0, 可领取状态
  107. if(act.state == 0) {
  108. Api.httpPost({
  109. url: "/daily/getDailyDegreeReward",
  110. data: {
  111. degree: act.degree
  112. },
  113. success: (res) => {
  114. // 更新全局userInfo
  115. this.quest.updateUserInfo(act.coin, act.diamond, act.ticket)
  116. // state = 2, 已领取状态
  117. act.state = 2
  118. // 获取当前活跃度对应的奖品内容并展示
  119. event.target.getComponent('cc.Sprite').spriteFrame = this.actGiftSprite[2]
  120. // 显示动画
  121. setTimeout(() => {
  122. this.quest.showActGift({
  123. ticket: act.ticket,
  124. diamond: act.diamond,
  125. coin: act.coin
  126. })
  127. }, 200);
  128. },
  129. fail: () => {
  130. // 领取失败
  131. }
  132. })
  133. }
  134. },
  135. start () {
  136. }
  137. });