CountdownTimer.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended.Timers
  4. {
  5. public class CountdownTimer : GameTimer
  6. {
  7. public CountdownTimer(double intervalSeconds)
  8. : base(intervalSeconds)
  9. {
  10. }
  11. public CountdownTimer(TimeSpan interval)
  12. : base(interval)
  13. {
  14. }
  15. public TimeSpan TimeRemaining { get; private set; }
  16. public event EventHandler TimeRemainingChanged;
  17. public event EventHandler Completed;
  18. protected override void OnStopped()
  19. {
  20. CurrentTime = TimeSpan.Zero;
  21. }
  22. protected override void OnUpdate(GameTime gameTime)
  23. {
  24. TimeRemaining = Interval - CurrentTime;
  25. TimeRemainingChanged?.Invoke(this, EventArgs.Empty);
  26. if (CurrentTime >= Interval)
  27. {
  28. State = TimerState.Completed;
  29. CurrentTime = Interval;
  30. TimeRemaining = TimeSpan.Zero;
  31. Completed?.Invoke(this, EventArgs.Empty);
  32. }
  33. }
  34. }
  35. }