1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view, game, director, Scheduler, Label } from "cc";
- import { GameUtil } from "../GameUtil";
- import { AliensGlobalInstance } from "../AliensGlobalInstance";
- /** 时间管理器*/
- export class TimerMgr {
- private static _instance: TimerMgr;
- public static get Instance(): TimerMgr {
- if (this._instance == null) {
- this._instance = new TimerMgr();
- }
- return this._instance;
- }
- public static get inst(): TimerMgr {
- return this.Instance;
- }
- public countDownTime: number = 1;
- private timerId: number = 0;
- private isPaused: boolean = false; // 添加暂停标志
- constructor() {
- }
- // 开始倒计时
- public startCountdown(): void {
- if (this.isPaused) {
- this.resumeCountdown();
- return;
- }
- this.upateLbTime();
- this.timerId = setInterval(() => {
- if (!this.isPaused) {
- this.countDownTime--;
- if (this.countDownTime <= 0) {
- this.stopCountdown();
- }
- this.upateLbTime();
- }
- }, 1000); // 每秒减少一次
- Scheduler.enableForTarget(this);
- director.getScheduler().schedule(this.update, this, 0);
- }
- private upateLbTime() {
- const battleUI = AliensGlobalInstance.instance.battleUI;
- const lbTime = battleUI.getChildByPath('Times/LbTime')!;
- // lbTime.getComponent(Label).string = this.countDownTime.toString();
- const format = GameUtil.formatToTimeString(this.countDownTime);
- lbTime.getComponent(Label).string = format;
- }
- // 停止倒计时
- private stopCountdown(): void {
- if (this.timerId) {
- clearInterval(this.timerId);
- this.timerId = 0;
- }
- }
- // 暂停倒计时
- public pauseCountdown(): void {
- this.isPaused = true;
- director.getScheduler().pauseTarget(this);
- }
- // 恢复倒计时
- public resumeCountdown(): void {
- this.isPaused = false;
- director.getScheduler().resumeTarget(this);
- }
- // 获取暂停状态
- public isPausedState(): boolean {
- return this.isPaused;
- }
- // update方法,每帧调用
- public update(dt: number): void {
- }
- // 销毁时清理
- public reset(): void {
- this.stopCountdown();
- this.isPaused = false; // 重置暂停状态
- Scheduler.enableForTarget(this);
- director.getScheduler().unscheduleAllForTarget(this);
- this.countDownTime = 10; //测试
- }
- }
|