SessionTimer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. namespace DevilLink2.Gameplay
  3. {
  4. /// <summary>Quarter-second active-time accumulator. ElapsedTime excludes penalties and bonuses.
  5. /// Pause preserves the fractional step; Stop freezes the timer permanently.</summary>
  6. public sealed class SessionTimer
  7. {
  8. public const double StepSeconds = .25;
  9. private double accumulator;
  10. private bool stopped;
  11. public double InitialTime { get; }
  12. public double RemainingTime { get; private set; }
  13. public double ElapsedTime { get; private set; }
  14. public bool HasTimer { get; }
  15. public bool IsRunning => !stopped && !IsPaused && !IsExpired;
  16. public bool IsPaused { get; private set; }
  17. public bool IsExpired { get; private set; }
  18. public double TimeBarMax { get; private set; }
  19. public bool IsLowTime => HasTimer && RemainingTime <= 15;
  20. public event Action Expired;
  21. public event Action<bool> LowTimeChanged;
  22. public event Action<double> RemainingTimeChanged;
  23. public SessionTimer(LevelDefinition level)
  24. {
  25. if (level == null) throw new ArgumentNullException(nameof(level));
  26. HasTimer = level.HasTimer;
  27. InitialTime = RemainingTime = TimeBarMax = level.TimeLimitSeconds;
  28. }
  29. public void Advance(double seconds)
  30. {
  31. GameplayTime.Validate(seconds, nameof(seconds));
  32. if (!IsRunning) return;
  33. double total = accumulator + seconds;
  34. GameplayTime.Validate(total, nameof(seconds));
  35. // Tolerate floating-point accumulation, without tying ticks to render frames.
  36. double ticks = Math.Floor(total / StepSeconds + 1e-9);
  37. double elapsed = ticks * StepSeconds;
  38. GameplayTime.Validate(elapsed, nameof(seconds));
  39. accumulator = Math.Max(0, total - elapsed);
  40. if (elapsed == 0) return;
  41. ElapsedTime += HasTimer ? Math.Min(RemainingTime, elapsed) : elapsed;
  42. if (HasTimer) SetRemaining(Math.Max(0, RemainingTime - elapsed));
  43. }
  44. public void Pause() { if (!stopped && !IsExpired) IsPaused = true; }
  45. public void Resume() { if (!stopped && !IsExpired) IsPaused = false; }
  46. public void Stop() { stopped = true; IsPaused = false; }
  47. public bool ApplyInvalidMatchPenalty()
  48. {
  49. if (!HasTimer || stopped || IsExpired) return false;
  50. SetRemaining(Math.Max(0, RemainingTime - 3));
  51. return true;
  52. }
  53. public bool AddTime(double seconds)
  54. {
  55. GameplayTime.Validate(seconds, nameof(seconds));
  56. if (!HasTimer || stopped || IsExpired || seconds == 0) return false;
  57. double next = RemainingTime + seconds;
  58. GameplayTime.Validate(next, nameof(seconds));
  59. TimeBarMax = Math.Max(TimeBarMax, next);
  60. SetRemaining(next);
  61. return true;
  62. }
  63. internal bool ReviveExpired(double seconds)
  64. {
  65. GameplayTime.Validate(seconds, nameof(seconds));
  66. if (!HasTimer || !IsExpired || seconds <= 0) return false;
  67. stopped = false; IsExpired = false; IsPaused = false; accumulator = 0;
  68. TimeBarMax = Math.Max(TimeBarMax, seconds); SetRemaining(seconds); return true;
  69. }
  70. private void SetRemaining(double value)
  71. {
  72. bool wasLow = IsLowTime;
  73. RemainingTime = value;
  74. bool expiresNow = value == 0 && !IsExpired;
  75. // Commit expiration before callbacks, so subscribers cannot revive this timer.
  76. if (expiresNow) { IsExpired = true; stopped = true; IsPaused = false; }
  77. RemainingTimeChanged?.Invoke(value);
  78. if (wasLow != IsLowTime) LowTimeChanged?.Invoke(IsLowTime);
  79. if (expiresNow) Expired?.Invoke();
  80. }
  81. }
  82. }