180 lines
4.8 KiB
TypeScript
180 lines
4.8 KiB
TypeScript
import { _decorator, assetManager, Component, director, Label, Node, PhysicsSystem, Vec3 } from 'cc';
|
|
import { RoundBall } from './RoundBall';
|
|
import { GameOverScreen } from './GameOverScreen';
|
|
import { PlayerController } from './PlayerController';
|
|
import { tgxUIAlert } from '../../core_tgx/tgx';
|
|
import { AreanNetMgr } from '../../module_arean/scripts/AreanNetMgr';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('GameManager')
|
|
export class GameManager extends Component {
|
|
|
|
/**
|
|
* 游戏结束
|
|
*/
|
|
public isGameOver: boolean = false;
|
|
|
|
myBallNum: number =10;
|
|
opBallNum: number = 10;
|
|
deadBallNum: number = 0;
|
|
rndBallNum: number = 0;
|
|
|
|
@property({
|
|
type: PlayerController,
|
|
displayName: "角色控制"
|
|
})
|
|
playerCtrl: PlayerController | null = null;
|
|
@property({
|
|
type: RoundBall,
|
|
displayName: "球控制"
|
|
})
|
|
ballCtrl: RoundBall | null = null;
|
|
|
|
|
|
|
|
@property({
|
|
type: Node,
|
|
displayName: "游戏结束界面"
|
|
})
|
|
gameOverScreen: Node | null = null;
|
|
|
|
@property({
|
|
type: Label,
|
|
displayName: "我方弹珠数量"
|
|
})
|
|
myBallNumLabel: Label | null = null;
|
|
|
|
@property({
|
|
type: Label,
|
|
displayName: "对方弹珠数量"
|
|
})
|
|
opBallNumLabel: Label | null = null;
|
|
|
|
@property({
|
|
type: Label,
|
|
displayName: "放置弹珠数量"
|
|
})
|
|
rndBallNumLabel: Label | null = null;
|
|
|
|
@property({
|
|
type: Label,
|
|
displayName: "击中弹珠数量"
|
|
})
|
|
deadBallNumLabel: Label | null = null;
|
|
|
|
|
|
start() {
|
|
PhysicsSystem.instance.enable = true;
|
|
PhysicsSystem.instance.gravity = new Vec3(0, -10, 0);
|
|
PhysicsSystem.instance.allowSleep = false;
|
|
this.gameOverScreen!.active = false;
|
|
|
|
}
|
|
|
|
|
|
AddBall() {
|
|
console.log("添加球");
|
|
if (this.playerCtrl) {
|
|
this.playerCtrl.AddBall();
|
|
}
|
|
}
|
|
public setGameOver(isWin: boolean) {
|
|
//this.gameOverScreen!.active =true;
|
|
this.gameOverScreen!.getComponent(GameOverScreen)?.setResult(isWin);
|
|
}
|
|
|
|
public RestartGame() {
|
|
|
|
this.myBallNum =10;
|
|
this.opBallNum = 10;
|
|
this.deadBallNum = 0;
|
|
this.rndBallNum = 0;
|
|
this.refreshBallNum();
|
|
|
|
if (this.playerCtrl) {
|
|
this.playerCtrl.resetGame();
|
|
}
|
|
if (this.ballCtrl) {
|
|
this.ballCtrl.resetGame();
|
|
}
|
|
this.gameOverScreen!.active = false;
|
|
}
|
|
|
|
// update(deltaTime: number) {
|
|
// if (this.playerCtrl) {
|
|
// if (this.playerCtrl.IsMoveEnd()) {
|
|
// if (this.ballCtrl && this.ballCtrl.isMoveEnd()) {
|
|
// console.log("球移动结束");
|
|
// if (this.playerCtrl.AddBallEnd()) {
|
|
// this.setGameOver(this.ballCtrl.isbegin);
|
|
// this.RestGame();
|
|
// } else {
|
|
// if (this.ballCtrl.isbegin) {
|
|
// this.setGameOver(this.ballCtrl.isbegin);
|
|
// this.RestGame();
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
refreshBallNum() {
|
|
|
|
scrollLabelToNumber(this.myBallNumLabel!,this.myBallNum);
|
|
scrollLabelToNumber(this.opBallNumLabel!,this.opBallNum);
|
|
scrollLabelToNumber(this.deadBallNumLabel!,this.deadBallNum);
|
|
scrollLabelToNumber(this.rndBallNumLabel!,this.rndBallNum);
|
|
}
|
|
|
|
changeGameScene()
|
|
{
|
|
var bundle = assetManager.getBundle('level1');
|
|
if(bundle){
|
|
director.loadScene("scene/level1Scene");
|
|
}else{
|
|
assetManager.loadBundle("level1",()=>{
|
|
director.loadScene("scene/level1Scene");
|
|
})
|
|
}
|
|
}
|
|
|
|
onClickBtnQuit() {
|
|
tgxUIAlert.show(`是否确认退出 ? `, true).onClick(async b => {
|
|
if (b) {
|
|
AreanNetMgr.inst.sendMsg_exit();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function scrollLabelToNumber(label: Label, targetNumber: number, duration: number = 0.3) {
|
|
const startNumber = parseInt(label.string); // 获取当前 Label 上的数字
|
|
const startTime = Date.now(); // 记录开始时间
|
|
|
|
const updateNumber = () => {
|
|
// 计算已经过去的时间比例
|
|
const elapsedTime = (Date.now() - startTime) / (duration * 1000);
|
|
const progress = Math.min(elapsedTime, 1); // 限制在 [0, 1] 范围内
|
|
|
|
// 计算当前数字
|
|
const currentNumber = Math.round(startNumber + (targetNumber - startNumber) * progress);
|
|
|
|
// 更新显示的数字
|
|
label.string = currentNumber.toString();
|
|
|
|
// 如果动画未结束,继续更新数字
|
|
if (progress < 1) {
|
|
requestAnimationFrame(updateNumber);
|
|
}
|
|
};
|
|
|
|
// 开始更新数字
|
|
updateNumber();
|
|
}
|
|
|
|
|
|
|