TimerMgr.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view, game, director, Scheduler, Label } from "cc";
  2. import { GameUtil } from "../GameUtil";
  3. import { AliensGlobalInstance } from "../AliensGlobalInstance";
  4. import { tgxUIMgr } from "db://assets/core_tgx/tgx";
  5. import { UI_TimeExpan, UI_BattleResult } from "db://assets/scripts/UIDef";
  6. import { UserManager } from "./UserMgr";
  7. import { LevelManager } from "./LevelMgr";
  8. import { GlobalConfig } from "db://assets/start/Config/GlobalConfig";
  9. /** 时间管理器*/
  10. export class TimerMgr {
  11. private static _instance: TimerMgr;
  12. public static get Instance(): TimerMgr {
  13. if (this._instance == null) {
  14. this._instance = new TimerMgr();
  15. }
  16. return this._instance;
  17. }
  18. public static get inst(): TimerMgr {
  19. return this.Instance;
  20. }
  21. private _countDownTime: number = 1;
  22. public get countDownTime(): number {
  23. return this._countDownTime;
  24. }
  25. public set countDownTime(value: number) {
  26. this._countDownTime = value;
  27. }
  28. private timerId: number = 0;
  29. private isPaused: boolean = false; // 添加暂停标志
  30. //加时次数
  31. private addTimeCount: number = 0;
  32. constructor() {
  33. }
  34. // 开始倒计时
  35. public startCountdown(): void {
  36. if (this.isPaused) {
  37. this.resumeCountdown();
  38. return;
  39. }
  40. this.upateLbTime();
  41. this.timerId = setInterval(() => {
  42. if (!this.isPaused) {
  43. this.countDownTime--;
  44. this.updateUsedTime();
  45. if (this.countDownTime <= 0) {
  46. this.stopCountdown();
  47. if (this.addTimeCount < 1) {
  48. this.showCountdownPopup();
  49. } else {
  50. this.showResultPopup();
  51. }
  52. this.addTimeCount++;
  53. }
  54. this.upateLbTime();
  55. }
  56. }, 1000); // 每秒减少一次
  57. Scheduler.enableForTarget(this);
  58. director.getScheduler().schedule(this.update, this, 0);
  59. }
  60. updateUsedTime() {
  61. const levelModel = LevelManager.instance.levelModel;
  62. levelModel.levelTimeUsed++;
  63. }
  64. private upateLbTime() {
  65. const battleUI = AliensGlobalInstance.instance.battleUI;
  66. const lbTime = battleUI.getChildByPath('Times/LbTime')!;
  67. // lbTime.getComponent(Label).string = this.countDownTime.toString();
  68. const format = GameUtil.formatToTimeString(this.countDownTime);
  69. lbTime.getComponent(Label).string = format;
  70. }
  71. // 停止倒计时
  72. private stopCountdown(): void {
  73. if (this.timerId) {
  74. clearInterval(this.timerId);
  75. this.timerId = 0;
  76. }
  77. }
  78. // 暂停倒计时
  79. public pauseCountdown(): void {
  80. this.isPaused = true;
  81. director.getScheduler().pauseTarget(this);
  82. }
  83. // 恢复倒计时
  84. public resumeCountdown(): void {
  85. this.isPaused = false;
  86. director.getScheduler().resumeTarget(this);
  87. }
  88. // 获取暂停状态
  89. public isPausedState(): boolean {
  90. return this.isPaused;
  91. }
  92. // update方法,每帧调用
  93. public update(dt: number): void {
  94. }
  95. //倒计时弹窗
  96. private showCountdownPopup(): void {
  97. const revive = tgxUIMgr.inst.isShowing(UI_TimeExpan);
  98. if (!revive) {
  99. tgxUIMgr.inst.showUI(UI_TimeExpan);
  100. }
  101. }
  102. //结算弹窗
  103. private showResultPopup(): void {
  104. const revive = tgxUIMgr.inst.isShowing(UI_BattleResult);
  105. if (!revive) {
  106. tgxUIMgr.inst.showUI(UI_BattleResult);
  107. }
  108. }
  109. // 销毁时清理
  110. public reset(): void {
  111. this.stopCountdown();
  112. this.isPaused = false; // 重置暂停状态
  113. Scheduler.enableForTarget(this);
  114. director.getScheduler().unscheduleAllForTarget(this);
  115. this.addTimeCount = 0;
  116. this.countDownTime = LevelManager.instance.levelModel.levelTime;
  117. if (GlobalConfig.isDebug) {
  118. this.countDownTime = 60;
  119. }
  120. }
  121. }