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

124 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-02-07 10:49:34 +08:00
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);
}
}