Roll.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /** @format */
  2. const {ccclass, property} = cc._decorator
  3. export enum ROLL_STATE {
  4. ready = 1,
  5. rolling,
  6. stop,
  7. }
  8. @ccclass
  9. export default class Roll extends cc.Component {
  10. curIndexArr: number[] = [1, 1, 1, 1]
  11. rightIndexArr: number[] = [1, 2, 3, 4]
  12. items1: cc.Node
  13. items2: cc.Node
  14. state: ROLL_STATE
  15. curRollIndex: number = 0
  16. upFinish: boolean = false
  17. downFinish: boolean = false
  18. spinTimes: number = 3
  19. singleRollTime: number = 0.3
  20. inversionTime: number = 0.1
  21. stopTime: number = 0.4
  22. initRoll() {
  23. this.state = ROLL_STATE.ready
  24. this.items1 = cc.find('items1', this.node)
  25. this.items2 = cc.find('items2', this.node)
  26. this.items1.stopAllActions()
  27. this.items2.stopAllActions()
  28. this.items1.y = 0
  29. this.items2.y = this.items1.height
  30. this.randomIndex(4, this.items1)
  31. this.randomIndex(4, this.items2)
  32. }
  33. startRoll() {
  34. this.state = ROLL_STATE.rolling
  35. this.curRollIndex = this.spinTimes
  36. this.aniRoll()
  37. }
  38. aniDownRoll(node) {
  39. cc.tween(node)
  40. .to(this.curRollIndex > 1 ? this.singleRollTime : this.stopTime, {y: -this.items1.height})
  41. .call(() => {
  42. this.downFinish = true
  43. this.endRoll()
  44. })
  45. .start()
  46. }
  47. aniUpRoll(node) {
  48. cc.tween(node)
  49. .to(this.curRollIndex > 1 ? this.singleRollTime : this.stopTime, {y: 0})
  50. .call(() => {
  51. this.upFinish = true
  52. this.endRoll()
  53. })
  54. .start()
  55. }
  56. aniRoll() {
  57. this.upFinish = false
  58. this.downFinish = false
  59. let upRoll = this.items1.y < this.items2.y ? this.items2 : this.items1
  60. let downRoll = this.items1.y < this.items2.y ? this.items1 : this.items2
  61. this.aniUpRoll(upRoll)
  62. this.aniDownRoll(downRoll)
  63. }
  64. endRoll() {
  65. if (this.upFinish && this.downFinish) {
  66. let upRoll = this.items1.y < this.items2.y ? this.items2 : this.items1
  67. let downRoll = this.items1.y < this.items2.y ? this.items1 : this.items2
  68. if (this.curRollIndex > 0) {
  69. if (this.curRollIndex > 1) {
  70. this.randomIndex(4, downRoll)
  71. } else if (this.curRollIndex == 1) {
  72. this.curIndexArr = this.rightIndexArr
  73. this.initItemIcon(downRoll)
  74. }
  75. downRoll.y = this.items1.height
  76. this.curRollIndex -= 1
  77. this.aniRoll()
  78. } else if (this.curRollIndex == 0) {
  79. cc.tween(upRoll)
  80. .to(this.inversionTime, {y: -5})
  81. .to(this.inversionTime, {y: 0})
  82. .call(() => {
  83. this.state = ROLL_STATE.stop
  84. })
  85. .start()
  86. }
  87. }
  88. }
  89. randomIndex(randomNum, node) {
  90. let tmp = Math.randomRangeIntArr(1, 13, randomNum)
  91. if (randomNum < 4) {
  92. for (let i = this.curIndexArr.length - 1; i >= 0; i--) {
  93. if (i >= randomNum) {
  94. this.curIndexArr[i] = this.curIndexArr[i - 1]
  95. } else {
  96. this.curIndexArr[i] = tmp[i]
  97. }
  98. }
  99. } else {
  100. this.curIndexArr = tmp
  101. }
  102. this.initItemIcon(node)
  103. }
  104. initItemIcon(node) {
  105. for (let i = 0; i < node.children.length; i++) {
  106. let item = node.children[i]
  107. }
  108. }
  109. }