123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- /** @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<number, boolean>
- 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 {
- 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)
- }
- /** 移动效果结束 */
- 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)))
- }
- }
|