ZumaRole.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. let ball: Ball = this.zumaCore.getBallFromPool()
  79. let newOrderArr = [...this.zumaCore.curBallIDs]
  80. newOrderArr.outOrder()
  81. let existArr = newOrderArr.filter(v => this.existMap.get(v))
  82. ball.init(existArr[0] ? existArr[0] : newOrderArr[0], this.zumaCore)
  83. return ball
  84. }
  85. /** 检查颜色,防止发射器中出现球链中没有的颜色 */
  86. public colorCleared(ballID: number): void {
  87. this.existMap.set(ballID, false)
  88. }
  89. /** 移动效果结束 */
  90. private onMoveFinished(): void {
  91. this._canShoot = true
  92. }
  93. /** 是否可以攻击 */
  94. get canShoot(): boolean {
  95. return this._canShoot
  96. }
  97. clearBalls() {
  98. if (this.currentBall) {
  99. this.zumaCore.putBall(this.currentBall)
  100. this.currentBall = null
  101. }
  102. if (this.nextBall) {
  103. this.zumaCore.putBall(this.nextBall)
  104. this.nextBall = null
  105. }
  106. this.ballParent.children.forEach(v => this.zumaCore.putBall(v.getComponent(Ball)))
  107. }
  108. }