TimerMgr.ts 3.9 KB

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