squidGame/tgx-games-client/assets/level3/script/RoundBall.ts

130 lines
4.0 KiB
TypeScript

import { _decorator, Component, instantiate, Node, Prefab, randomRangeInt, Animation, SkeletalAnimation } from 'cc';
import { Ball } from './Ball';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;
@ccclass('RoundBall')
export class RoundBall extends Component {
@property({ type: Prefab, displayName: "小球" })
ball: Prefab | null = null;
@property({ type: Node })
public gameManagerNode: Node | null = null;
gameManager: GameManager | null = null;
public ballList: Node[] = [];
public isbegin: boolean = false;
@property({ type: Node })
public opPlayerNode: Node | null = null;
start() {
this.gameManager = this.gameManagerNode!.getComponent(GameManager);
this.resetGame();
}
resetGame() {
this.gameManager!.myBallNum += this.gameManager!.deadBallNum;
this.gameManager!.deadBallNum = 0;
this.gameManager!.rndBallNum = 0;
this.gameManager!.refreshBallNum();
this.scheduleOnce(() => {
let anim = this.opPlayerNode!.getChildByName("Node")!.getChildByName("001_skin")!.getComponent(SkeletalAnimation)!;
anim.crossFade("player_openup", 0.5);
anim.off(SkeletalAnimation.EventType.FINISHED);
anim.on(SkeletalAnimation.EventType.FINISHED, () => { anim.crossFade("player_idle1", 0.5); }, this);
}, 1);
this.scheduleOnce(() => {
this.isbegin = true;
for (let index = 0; index < this.ballList.length; index++) {
const element = this.ballList[index];
element.removeFromParent();
element.destroy();
}
this.ballList = [];
this.gameManager!.rndBallNum = Math.min(this.gameManager!.opBallNum, randomRangeInt(3, 6));
this.gameManager!.opBallNum -= this.gameManager!.rndBallNum;
this.gameManager!.refreshBallNum();
if (this.ball != null) {
for (let i = 0; i < this.gameManager!.rndBallNum; i++) {
var ball = instantiate(this.ball);
if (ball) {
let typeX = randomRangeInt(0, 2);
let typeY = randomRangeInt(0, 2);
ball.setPosition(Math.random() * 4 * (typeX == 0 ? -1 : 1), 5, Math.random() * 4 * (typeY == 0 ? -1 : 1));
this.node.addChild(ball);
this.ballList.push(ball);
ball.getComponent(Ball)?.setBodyType(1);
}
}
}
}, 1.5);
}
public isMoveEnd(): boolean {
for (let index = 0; index < this.ballList.length; index++) {
const element = this.ballList[index];
if (element.getComponent(Ball)?.isMoving) {
return false;
}
}
return true;
}
update(deltaTime: number) {
if (this.isbegin) {
let deadBallNum = 0;
let rndBallNum = 0;
for (let index = 0; index < this.ballList.length; index++) {
const element = this.ballList[index];
if (element.getComponent(Ball)?.isDead) {
deadBallNum++;
}
else {
rndBallNum++;
}
}
this.gameManager!.deadBallNum = deadBallNum;
this.gameManager!.rndBallNum = rndBallNum;
this.gameManager!.refreshBallNum();
if (this.gameManager!.rndBallNum == 0) {
this.isbegin = false;
if(this.gameManager!.opBallNum>0)
{
this.resetGame();
}
else
{
this.gameManager!.setGameOver(true);
}
}
}
}
}