TurntableItem.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import PropIntroduceNode from "../ui/PropIntroduceNode";
  8. import { Utils } from "../utils/Utils";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class TurntableItem extends cc.Component {
  12. @property(cc.Sprite)
  13. icon: cc.Sprite = null;
  14. @property(cc.Label)
  15. labNum: cc.Label = null;
  16. @property(cc.Label)
  17. labDes: cc.Label = null;
  18. @property(cc.Node)
  19. scaleNode: cc.Node = null;
  20. /** 数据 */
  21. data = null
  22. num = 0
  23. animNum = 0
  24. rewardPrefab: cc.Prefab = null
  25. start() {
  26. }
  27. async init(data) {
  28. this.data = data
  29. this.num = data.num
  30. this.node.angle = (data.index - 1) * 60
  31. let self = this
  32. let path = 'gift/turn_' + data.reward
  33. if (data.reward == 2) {
  34. if (data.moneyLevel > 0) {
  35. path = path + "_" + data.moneyLevel
  36. }
  37. }
  38. this.rewardPrefab = await Utils.loadResPromise('prefabs/TurntableRewardTip')
  39. // else if(data.reward == 1){
  40. // self.icon.node.scale = 0.8
  41. // }else if(data.reward == 3){
  42. // self.icon.node.scale = 0.6
  43. // }
  44. cc.loader.loadRes(path, cc.SpriteFrame, (err, assets) => {
  45. if (err) {
  46. cc.error(err);
  47. return;
  48. }
  49. self.icon.spriteFrame = assets;
  50. })
  51. //惊喜宝箱
  52. if (data.reward == 1) {
  53. this.labNum.string = '' + data.num
  54. }
  55. //挂机收益
  56. else if (data.reward == 2) {
  57. this.labNum.string = '' + data.num + '分钟'
  58. }
  59. //元宝
  60. else if (data.reward == 3) {
  61. this.labNum.string = '' + data.num
  62. }
  63. else if (data.reward == 4) {
  64. this.labNum.string = '' + data.num
  65. }
  66. // if ((data.index - 1) % 2 == 0) {
  67. // this.labDes.node.color = cc.color(42, 81, 190)
  68. // this.labNum.node.color = cc.color(42, 81, 190)
  69. // }
  70. // else {
  71. this.labDes.node.color = cc.color(162, 93, 93);
  72. this.labNum.node.color = cc.color(162, 93, 93);
  73. // }
  74. this.labDes.string = data.txt
  75. let str = ""
  76. if (data.reward == 2) {
  77. str = "用于购买怪物"
  78. } else if (data.reward == 3) {
  79. str = "用于提现消耗"
  80. } else if (data.reward == 4) {
  81. str = "用于升级伙伴天赋"
  82. } else {
  83. str = data.txt
  84. }
  85. this.icon.node.on(cc.Node.EventType.TOUCH_START, (touch) => {
  86. this.showPropIntroduce(str, "", cc.v3(touch.getLocation()))
  87. }, this)
  88. }
  89. updateNum(num: number) {
  90. if (num != this.num) {
  91. this.animNum = this.num
  92. this.num = num
  93. // this.labNum.string = 'x' + this.num
  94. this.unschedule(this.showAction)
  95. this.showRewardNode()
  96. // this.node.runAction(cc.sequence(cc.scaleTo(1,1.5),cc.scaleTo(1,1)))
  97. }
  98. }
  99. showRewardNode() {
  100. let rewardNode = cc.instantiate(this.rewardPrefab)
  101. rewardNode.parent = cc.find("Canvas")
  102. rewardNode.scale = 0.001
  103. let worldPos = this.icon.node.parent.convertToWorldSpaceAR(this.icon.node.position)
  104. let localPos = rewardNode.parent.convertToNodeSpaceAR(worldPos)
  105. rewardNode.position = localPos
  106. rewardNode.runAction(cc.sequence(cc.scaleTo(0.2, 1.6), cc.delayTime(0.5), cc.spawn(cc.moveTo(0.4, cc.v2(localPos.x, localPos.y + 100)), cc.fadeOut(0.4)), cc.callFunc(() => {
  107. if (cc.isValid(rewardNode)) {
  108. rewardNode.destroy()
  109. }
  110. let color = this.labNum.node.color
  111. this.scaleNode.runAction(cc.sequence(cc.callFunc(() => {
  112. this.node.parent = this.node.parent.parent.getChildByName("turntable_in")
  113. this.labNum.node.color = cc.color(250, 245, 69)// cc.Color.RED
  114. let outline = this.labNum.node.addComponent(cc.LabelOutline)
  115. outline.color = cc.Color.BLACK
  116. outline.width = 1
  117. }), cc.scaleTo(0.3, 1.6), cc.callFunc(() => {
  118. this.schedule(this.showAction, 0.015)
  119. }), cc.delayTime((this.num - this.animNum) * 0.016), cc.scaleTo(0.3, 1), cc.callFunc(() => {
  120. this.node.parent = this.node.parent.parent.getChildByName("turntable_in")
  121. this.labNum.node.color = color
  122. this.labNum.node.removeComponent(cc.LabelOutline)
  123. })))
  124. })))
  125. }
  126. showAction() {
  127. if (this.animNum < this.num) {
  128. this.animNum++
  129. } else {
  130. if (cc.isValid(this.labNum)) {
  131. // this.labNum.node.stopAllActions()
  132. // this.labNum.node.runAction(cc.scaleTo(0.3,1))
  133. // this.labNum.node.scale = 1
  134. }
  135. }
  136. if (cc.isValid(this.labNum)) {
  137. this.labNum.string = "" + this.animNum.toString() + "分钟"
  138. }
  139. // this.node.runAction(cc.repeat(cc.callFunc(()=>{
  140. // this.animNum += dur/repeats
  141. // this.labNum.string =
  142. // }),repeats))
  143. }
  144. async showPropIntroduce(name: string, desc: string, worldPos: cc.Vec3) {
  145. let canvas = cc.find("Canvas")
  146. if (cc.isValid(canvas.getChildByName("propIntroduceNode"))) {
  147. canvas.getChildByName("propIntroduceNode").destroy()
  148. }
  149. let propIntroducePrefab = await Utils.loadResPromise("prefabs/propIntroduceNode")
  150. let propIntroduceNode: cc.Node = cc.instantiate(propIntroducePrefab)
  151. propIntroduceNode.parent = cc.find("Canvas")
  152. propIntroduceNode.name = "propIntroduceNode"
  153. propIntroduceNode.position = cc.find("Canvas").convertToNodeSpaceAR(worldPos)
  154. propIntroduceNode.y += 60
  155. propIntroduceNode.x -= 60
  156. propIntroduceNode.getComponent(PropIntroduceNode).initView(name, desc)
  157. }
  158. // update (dt) {}
  159. }