import { _decorator, Component, Node, Animation, Label, director } from 'cc'; import { ScenePreloadManager } from 'db://assets/scripts/ScenePreloadManager'; const { ccclass, property } = _decorator; enum GameState { Playing, Paused, GameOver } @ccclass('GameManager') export class GameManager extends Component { @property({type: Animation}) cameraAnim: Animation = null!; @property(Label) timerLabel: Label = null!; // 倒计时文本显示 @property(Node) guiNode: Node = null!; // UI节点 private gameState: GameState = GameState.Playing; private countdownDuration: number = 120; // 3分钟 = 180秒 private countdownRemaining: number = 120; private isCountdownStarted: boolean = false; private startDelay: number = 6; // 开场动画结束后延迟1.5秒开始 private isGameOver: boolean = false; start() { this.initGame(); } private initGame() { // 初始化游戏状态 this.gameState = GameState.Playing; this.countdownRemaining = this.countdownDuration; this.isCountdownStarted = false; this.isGameOver = false; // 初始化计时器显示 this.updateTimerDisplay(); this.scheduleOnce(() => { this.startCountdown(); }, this.startDelay); } private startCountdown() { this.isCountdownStarted = true; this.countdownRemaining = this.countdownDuration; this.updateTimerDisplay(); // 每秒更新一次 this.schedule(this.updateCountdown, 1); } private updateCountdown() { if (!this.isCountdownStarted || this.gameState !== GameState.Playing) return; this.countdownRemaining--; this.updateTimerDisplay(); if (this.countdownRemaining <= 0) { // 时间到,结束游戏 this.unschedule(this.updateCountdown); this.setGameOver(false); // 时间到算失败 } } private updateTimerDisplay() { if (!this.timerLabel) return; const minutes = Math.floor(this.countdownRemaining / 60); const seconds = this.countdownRemaining % 60; // 格式化为 "xx:xx" this.timerLabel.string = `${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`; } public setGameOver(isWin: boolean) { if (this.isGameOver) return; this.isGameOver = true; this.isCountdownStarted = false; this.unschedule(this.updateCountdown); this.gameState = GameState.GameOver; // 这里可以添加游戏结算逻辑 console.log(`游戏结束,${isWin ? '胜利' : '失败'},剩余时间: ${this.countdownRemaining}秒`); // 这里可以添加显示结算界面的代码 } // 可以添加其他游戏相关方法 public pauseGame() { if (this.gameState === GameState.Playing) { this.gameState = GameState.Paused; this.unschedule(this.updateCountdown); } } public resumeGame() { if (this.gameState === GameState.Paused) { this.gameState = GameState.Playing; this.schedule(this.updateCountdown, 1); } } onDestroy() { // 清理定时器 this.unscheduleAllCallbacks(); } }