SessionTimer.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. private void SetRemaining(double value)
  64. {
  65. bool wasLow = IsLowTime;
  66. RemainingTime = value;
  67. bool expiresNow = value == 0 && !IsExpired;
  68. // Commit expiration before callbacks, so subscribers cannot revive this timer.
  69. if (expiresNow) { IsExpired = true; stopped = true; IsPaused = false; }
  70. RemainingTimeChanged?.Invoke(value);
  71. if (wasLow != IsLowTime) LowTimeChanged?.Invoke(IsLowTime);
  72. if (expiresNow) Expired?.Invoke();
  73. }
  74. }
  75. }