SimulationTimer.cs 779 B

1234567891011121314151617181920212223242526272829303132
  1. using UnityEngine;
  2. namespace StinkySteak.SimulationTimer
  3. {
  4. public struct SimulationTimer
  5. {
  6. public static SimulationTimer None => default;
  7. private float _targetTime;
  8. public float TargetTime => _targetTime;
  9. public static SimulationTimer CreateFromSeconds(float duration)
  10. {
  11. return new SimulationTimer()
  12. {
  13. _targetTime = duration + Time.time
  14. };
  15. }
  16. public bool IsRunning => _targetTime > 0;
  17. public bool IsExpired()
  18. => Time.time >= _targetTime && IsRunning;
  19. public bool IsExpiredOrNotRunning()
  20. => Time.time >= _targetTime;
  21. public float RemainingSeconds
  22. => Mathf.Max(_targetTime - Time.time, 0);
  23. }
  24. }