TimerMgr.ts 3.6 KB

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