import { _decorator, Component, Label, Node, Sprite, tween, Vec3 } from 'cc'; import { endPos } from '../../../ui/main/UI_Main'; import { UI_Revive } from '../../../ui/UI_Revive/UI_Revive'; import { gui } from 'db://assets/core/ui/ui'; import { UI_Lose } from '../../../ui/UI_Lose/UI_Lose'; import { sdkMe } from '../../MySdk'; import { audioManager } from '../../../Audio/AudioManager'; const { ccclass, property } = _decorator; @ccclass('Title') export class Title extends Component { @property(Number) stoneNumber: number = 0; @property(Label) stoneLabel: Label | null = null; @property(Sprite) process: Sprite | null = null; @property(Label) timeNum: Label | null = null; @property(Node) Quan: Node; @property(Node) buttomNode: Node; public totalStones: number = 200; // 总石头数量 public remainingTime: number = 0; // 剩余时间变量 private maxTime: number = 150; // 最大时间限制 private isPalyAd: boolean = false; // 最大时间限制 private isPaused: boolean = false; //设置暂停 private hasShownRevive: boolean = false; // 复活状态 start() { this.remainingTime = endPos.time; // 初始化剩余时间 this.updateProgressBar(); this.schedule(this.updateTime, 1); // 每秒更新一次倒计时 // 初始化时间显示 if (this.timeNum) { this.timeNum.string = this.FormatTime(this.remainingTime); } this.hasShownRevive = false; // 每局开始时重置状态 sdkMe.evt.on('adShow',this.stop,this) sdkMe.evt.on('adOff',this.resume,this) } protected onDisable(): void { sdkMe.evt.off('adShow',this.stop,this) sdkMe.evt.off('adOff',this.resume,this) } stop(){ this.isPalyAd = true; } resume() { this.isPalyAd = false; audioManager.resume(); } public set_stop() { this.isPaused = true; } public set_resume() { this.isPaused = false; audioManager.resume(); } addTime(seconds: number) { //const wasTimerRunning = this.schedule(this.updateTime); // 记录原定时器状态 const previousTime = this.remainingTime; // 记录原剩余时间 const newTime = this.remainingTime + seconds; this.remainingTime = Math.min(newTime, this.maxTime); // 不超过最大时间 // 立即更新显示 if (this.timeNum) { this.timeNum.string = this.FormatTime(this.remainingTime); } if ( previousTime <= 0 && // 原剩余时间已耗尽 this.remainingTime > 0 // 新剩余时间为正数 ) { this.schedule(this.updateTime, 1); } } filled(num: number) { this.stoneNumber = num; if (this.stoneLabel) { this.stoneLabel.string = '消除120块石头:' + num + '/120'; } this.updateProgressBar(); } updateProgressBar() { if (this.process) { this.process.fillRange = this.stoneNumber / 120; } } updateTime() { if (this.isPalyAd||this.isPaused) return; // 防止负数 if (this.remainingTime <= 0) return; // 防止负数 this.remainingTime--; // 更新显示 if (this.timeNum) { this.timeNum.string = this.FormatTime(this.remainingTime); } // 时间耗尽处理 if (this.remainingTime <= 0) { if (!this.hasShownRevive) { this.unschedule(this.updateTime); gui.show(UI_Revive); this.hasShownRevive = true; } else { console.log("游戏失败!!!"); gui.show(UI_Lose); } } //按钮左右摇摆 if (this.remainingTime < 90) { // 左右摇摆动画 tween(this.buttomNode) .to(0.2, { eulerAngles: new Vec3(0, 0, 10) }) .to(0.4, { eulerAngles: new Vec3(0, 0, -10) }) .to(0.2, { eulerAngles: new Vec3(0, 0, 0) }) .union() .repeat(2) // 重复2次形成完整摇摆 .start(); } } FormatTime(totalSeconds: number): string { let minutes: number = Math.floor(totalSeconds / 60); let seconds: number = totalSeconds % 60; let mm: string = minutes < 10 ? `0${minutes}` : minutes.toString(); let ss: string = seconds < 10 ? `0${seconds}` : seconds.toString(); return `${mm}:${ss}`; } public getRemainingTime(): number { return this.remainingTime; } // 当游戏重新开始时调用这个方法重置状态 public resetGameState() { this.hasShownRevive = false; } }