TimerMgr.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (this.countDownTime <= 0) {
  44. this.stopCountdown();
  45. if(this.addTimeCount < 1){
  46. this.showCountdownPopup();
  47. }else{
  48. this.showResultPopup();
  49. }
  50. this.addTimeCount++;
  51. }
  52. this.upateLbTime();
  53. }
  54. }, 1000); // 每秒减少一次
  55. Scheduler.enableForTarget(this);
  56. director.getScheduler().schedule(this.update, this, 0);
  57. }
  58. private upateLbTime() {
  59. const battleUI = AliensGlobalInstance.instance.battleUI;
  60. const lbTime = battleUI.getChildByPath('Times/LbTime')!;
  61. // lbTime.getComponent(Label).string = this.countDownTime.toString();
  62. const format = GameUtil.formatToTimeString(this.countDownTime);
  63. lbTime.getComponent(Label).string = format;
  64. }
  65. // 停止倒计时
  66. private stopCountdown(): void {
  67. if (this.timerId) {
  68. clearInterval(this.timerId);
  69. this.timerId = 0;
  70. }
  71. }
  72. // 暂停倒计时
  73. public pauseCountdown(): void {
  74. this.isPaused = true;
  75. director.getScheduler().pauseTarget(this);
  76. }
  77. // 恢复倒计时
  78. public resumeCountdown(): void {
  79. this.isPaused = false;
  80. director.getScheduler().resumeTarget(this);
  81. }
  82. // 获取暂停状态
  83. public isPausedState(): boolean {
  84. return this.isPaused;
  85. }
  86. // update方法,每帧调用
  87. public update(dt: number): void {
  88. }
  89. //倒计时弹窗
  90. private showCountdownPopup(): void {
  91. const revive = tgxUIMgr.inst.isShowing(UI_TimeExpan);
  92. if (!revive) {
  93. tgxUIMgr.inst.showUI(UI_TimeExpan);
  94. }
  95. }
  96. //结算弹窗
  97. private showResultPopup(): void {
  98. const revive = tgxUIMgr.inst.isShowing(UI_BattleResult);
  99. if (!revive) {
  100. tgxUIMgr.inst.showUI(UI_BattleResult);
  101. }
  102. }
  103. // 销毁时清理
  104. public reset(): void {
  105. this.stopCountdown();
  106. this.isPaused = false; // 重置暂停状态
  107. Scheduler.enableForTarget(this);
  108. director.getScheduler().unscheduleAllForTarget(this);
  109. this.addTimeCount = 0;
  110. this.countDownTime = LevelManager.instance.levelModel.levelTime;
  111. this.countDownTime = 20; //测试
  112. }
  113. }