import { _decorator, Component, instantiate, Node, tween, SkeletalAnimation, Vec3 } from 'cc'; import { GameManager } from './GameManager'; import { Ball } from './Ball'; const { ccclass, property } = _decorator; @ccclass('PlayerController') export class PlayerController extends Component { @property({ type: Node, displayName: '球' }) public ball: Node | null = null; @property({ type: Node }) public gameManagerNode: Node | null = null; gameManager: GameManager | null = null; public ballList: Node[] = []; @property({ type: Node }) public playerNode: Node | null = null; start() { this.gameManager = this.gameManagerNode!.getComponent(GameManager); this.ballList = []; this.ball?.setPosition(5, 10, 10); tween(this.ball).repeatForever(tween().sequence(tween().to(3, { position: { x: -5, y: 10, z: 10 } }), tween().to(3, { position: { x: 5, y: 10, z: 10 } }))).start(); } resetGame() { for (let index = 0; index < this.ballList.length; index++) { const element = this.ballList[index]; element.removeFromParent(); element.destroy(); } this.ballList = []; } /** * 生成小球 */ public AddBall() { if (this.gameManager!.myBallNum <= 0) { this.gameManager!.setGameOver(false); return; } let ball = instantiate(this.ball); this.ball!.active = false; this.playerNode!.setPosition(this.ball!.position.x, 0, 20); let anim = this.playerNode!.getChildByName("Node")!.getChildByName("003_skin")!.getComponent(SkeletalAnimation)!; anim.crossFade("player_throw", 0.5); anim.off(SkeletalAnimation.EventType.FINISHED); anim.on(SkeletalAnimation.EventType.FINISHED, () => { anim.crossFade("player_idle1", 0.5); }, this); this.scheduleOnce(() => { if (ball) { // console.log("ball:", ball.getPosition()); this.node.addChild(ball); this.ballList.push(ball); this.gameManager!.myBallNum--; this.gameManager!.opBallNum++; this.gameManager!.refreshBallNum(); ball.getComponent(Ball)?.AddTorque(-2500); } }, 0.8); this.scheduleOnce(() => { this.ball!.active = true; }, 3); } /** * 是否结束 * @returns 是否结束 */ 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; } public AddBallEnd(): boolean { return this.gameManager!.myBallNum <= 0; } /** * 获取球球数量 * @returns 球球数量 */ public getBallCount() { return this.ballList.length; } update(deltaTime: number) { // let pos = this.ball?.getPosition(); // console.log("ball:",pos?.x,pos?.y); } }