ZumaRole.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /** @format */
  2. import {Ball} from './Ball'
  3. import {ZumamapConfig} from '../config/ZumamapConfig'
  4. import ZumaCore from './ZumaCore'
  5. const {ccclass, property} = cc._decorator
  6. @ccclass
  7. export class ZumaRole extends cc.Component {
  8. /** 是否能发射了 */
  9. private _canShoot: boolean
  10. //各个颜色的小球是否存在
  11. private existMap: Map<number, boolean>
  12. public zumaCore: ZumaCore
  13. //当前的球
  14. public currentBall: Ball
  15. //下一个球
  16. public nextBall: Ball
  17. public ballParent: cc.Node
  18. public onLoad() {
  19. this.ballParent = cc.find('ballParent', this.node)
  20. }
  21. public init(zumaCore: ZumaCore): void {
  22. this.unscheduleAllCallbacks()
  23. this._canShoot = false
  24. this.zumaCore = zumaCore
  25. let mapCfg = ZumamapConfig[zumaCore.levelCfg.map]
  26. this.node.x = mapCfg.rolePosition[0]
  27. this.node.y = mapCfg.rolePosition[1]
  28. this.ballParent.children.length = 0
  29. this.existMap = new Map()
  30. for (let i = 0; i < this.zumaCore.curBallIDs.length; i++) {
  31. this.existMap.set(this.zumaCore.curBallIDs[i], true)
  32. }
  33. }
  34. /** 初始化展示球 */
  35. public initBall(): void {
  36. this.currentBall = this.getBall()
  37. this.ballParent.addChild(this.currentBall.node)
  38. this.currentBall.node.setPosition(cc.v2(0, -88))
  39. this.currentBall.node.scale = 0.6
  40. this.currentBall.node.zIndex = 1
  41. this.nextBall = this.getBall()
  42. this.ballParent.addChild(this.nextBall.node)
  43. this.nextBall.node.setPosition(cc.v2(0, -88))
  44. this.nextBall.node.scale = 0.6
  45. this.nextBall.node.zIndex = 0
  46. this.outBallAni(this.currentBall)
  47. }
  48. public outBallAni(ball: Ball) {
  49. cc.tween(this.currentBall.node)
  50. .to(0.3, {y: -35, scale: 1})
  51. .call(() => {
  52. this.onMoveFinished()
  53. })
  54. .start()
  55. }
  56. /** 发射的时候,从此发射器中获当前取小球 */
  57. public getCurrentBall(): Ball {
  58. this._canShoot = false
  59. let _ball = this.currentBall
  60. this.nextShoot()
  61. return _ball
  62. }
  63. /** 准备下一发 */
  64. private nextShoot(): void {
  65. this.ballParent.removeChild(this.currentBall.node)
  66. this.currentBall = this.nextBall
  67. this.currentBall.node.zIndex = 1
  68. this.nextBall = this.getBall()
  69. this.ballParent.addChild(this.nextBall.node)
  70. this.nextBall.node.x = 0
  71. this.nextBall.node.y = -88
  72. this.nextBall.node.scale = 0.6
  73. this.nextBall.node.zIndex = 0
  74. this.outBallAni(this.currentBall)
  75. }
  76. /** 随机获取小球 */
  77. private getBall(): Ball {
  78. if (Array.from(this.existMap.values()).every(v => !v) && !this.zumaCore.hasSpecialBall()) {
  79. debugger
  80. console.error('all ball has been cleared')
  81. }
  82. let ball: Ball = this.zumaCore.getBallFromPool()
  83. let newOrderArr = [...this.zumaCore.curBallIDs]
  84. newOrderArr.outOrder()
  85. let existArr = newOrderArr.filter(v => this.existMap.get(v))
  86. ball.init(existArr[0] ? existArr[0] : newOrderArr[0], this.zumaCore)
  87. return ball
  88. }
  89. /** 检查颜色,防止发射器中出现球链中没有的颜色 */
  90. public colorCleared(ballID: number): void {
  91. this.existMap.set(ballID, false)
  92. //全部颜色的球清除完毕说明游戏已结束,将发射器中的球缩回
  93. if (Array.from(this.existMap.values()).every(v => !v)) {
  94. this._canShoot = false
  95. this.clearBalls()
  96. }
  97. }
  98. /** 移动效果结束 */
  99. private onMoveFinished(): void {
  100. this._canShoot = true
  101. }
  102. /** 是否可以攻击 */
  103. get canShoot(): boolean {
  104. return this._canShoot
  105. }
  106. clearBalls() {
  107. if (this.currentBall) {
  108. this.zumaCore.putBall(this.currentBall)
  109. this.currentBall = null
  110. }
  111. if (this.nextBall) {
  112. this.zumaCore.putBall(this.nextBall)
  113. this.nextBall = null
  114. }
  115. this.ballParent.children.forEach(v => this.zumaCore.putBall(v.getComponent(Ball)))
  116. }
  117. }