/** @format */ import {Ball} from './Ball' import {ZumamapConfig} from '../config/ZumamapConfig' import ZumaCore from './ZumaCore' const {ccclass, property} = cc._decorator @ccclass export class ZumaRole extends cc.Component { /** 是否能发射了 */ private _canShoot: boolean //各个颜色的小球是否存在 private existMap: Map public zumaCore: ZumaCore //当前的球 public currentBall: Ball //下一个球 public nextBall: Ball public ballParent: cc.Node public onLoad() { this.ballParent = cc.find('ballParent', this.node) } public init(zumaCore: ZumaCore): void { this.unscheduleAllCallbacks() this._canShoot = false this.zumaCore = zumaCore let mapCfg = ZumamapConfig[zumaCore.levelCfg.map] this.node.x = mapCfg.rolePosition[0] this.node.y = mapCfg.rolePosition[1] this.ballParent.children.length = 0 this.existMap = new Map() for (let i = 0; i < this.zumaCore.curBallIDs.length; i++) { this.existMap.set(this.zumaCore.curBallIDs[i], true) } } /** 初始化展示球 */ public initBall(): void { this.currentBall = this.getBall() this.ballParent.addChild(this.currentBall.node) this.currentBall.node.setPosition(cc.v2(0, -88)) this.currentBall.node.scale = 0.6 this.currentBall.node.zIndex = 1 this.nextBall = this.getBall() this.ballParent.addChild(this.nextBall.node) this.nextBall.node.setPosition(cc.v2(0, -88)) this.nextBall.node.scale = 0.6 this.nextBall.node.zIndex = 0 this.outBallAni(this.currentBall) } public outBallAni(ball: Ball) { cc.tween(this.currentBall.node) .to(0.3, {y: -35, scale: 1}) .call(() => { this.onMoveFinished() }) .start() } /** 发射的时候,从此发射器中获当前取小球 */ public getCurrentBall(): Ball { this._canShoot = false let _ball = this.currentBall this.nextShoot() return _ball } /** 准备下一发 */ private nextShoot(): void { this.ballParent.removeChild(this.currentBall.node) this.currentBall = this.nextBall this.currentBall.node.zIndex = 1 this.nextBall = this.getBall() this.ballParent.addChild(this.nextBall.node) this.nextBall.node.x = 0 this.nextBall.node.y = -88 this.nextBall.node.scale = 0.6 this.nextBall.node.zIndex = 0 this.outBallAni(this.currentBall) } /** 随机获取小球 */ private getBall(): Ball { if (Array.from(this.existMap.values()).every(v => !v) && !this.zumaCore.hasSpecialBall()) { debugger console.error('all ball has been cleared') } let ball: Ball = this.zumaCore.getBallFromPool() let newOrderArr = [...this.zumaCore.curBallIDs] newOrderArr.outOrder() let existArr = newOrderArr.filter(v => this.existMap.get(v)) ball.init(existArr[0] ? existArr[0] : newOrderArr[0], this.zumaCore) return ball } /** 检查颜色,防止发射器中出现球链中没有的颜色 */ public colorCleared(ballID: number): void { this.existMap.set(ballID, false) //全部颜色的球清除完毕说明游戏已结束,将发射器中的球缩回 if (Array.from(this.existMap.values()).every(v => !v)) { this._canShoot = false this.clearBalls() } } /** 移动效果结束 */ private onMoveFinished(): void { this._canShoot = true } /** 是否可以攻击 */ get canShoot(): boolean { return this._canShoot } clearBalls() { if (this.currentBall) { this.zumaCore.putBall(this.currentBall) this.currentBall = null } if (this.nextBall) { this.zumaCore.putBall(this.nextBall) this.nextBall = null } this.ballParent.children.forEach(v => this.zumaCore.putBall(v.getComponent(Ball))) } }