TimeProxy.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. namespace XGame.Framework.Time
  4. {
  5. public class TimeProxy : ITimeProxy, ITimeListener<IAlarm>, ITimeListener<ITimer>
  6. {
  7. private ITimeModule _timeModule;
  8. private HashSet<IAlarm> _alarms;
  9. private HashSet<ITimer> _timers;
  10. public TimeProxy(ITimeModule timeModule)
  11. {
  12. _timeModule = timeModule;
  13. }
  14. #region 接口实现
  15. public long GetNowTime(ClockType clockTyp = ClockType.Server)
  16. {
  17. return _timeModule.GetNowTime(clockTyp);
  18. }
  19. public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
  20. {
  21. _timeModule.SetTime(timestamp, clockTyp);
  22. }
  23. public IAlarm AddAlarm(Action action, long triggerTimeStamp, ClockType clockTyp = ClockType.Server)
  24. {
  25. var alarm = _timeModule.AddAlarm(action, triggerTimeStamp, clockTyp);
  26. alarm.Listener = this;
  27. AddAlarm(alarm);
  28. return alarm;
  29. }
  30. public ITimer AddDelayTimer(int delay, Action action)
  31. {
  32. var timer = _timeModule.AddDelayTimer(delay, action);
  33. timer.Listener = this;
  34. AddTimer(timer);
  35. return timer;
  36. }
  37. public ITimer AddDelayLooperTimer(int delay, int interval, Action<int> action, int loopTimes = -1)
  38. {
  39. var timer = _timeModule.AddDelayLooperTimer(delay, interval, action, loopTimes);
  40. timer.Listener = this;
  41. AddTimer(timer);
  42. return timer;
  43. }
  44. public ITimer AddLooperTimer(int interval, Action<int> action, int loopTimes = -1)
  45. {
  46. var timer = _timeModule.AddLooperTimer(interval, action, loopTimes);
  47. timer.Listener = this;
  48. AddTimer(timer);
  49. return timer;
  50. }
  51. public void CancelAll()
  52. {
  53. if (_alarms != null)
  54. {
  55. foreach(var alarm in _alarms)
  56. {
  57. alarm.Listener = null;
  58. alarm.Cancel();
  59. }
  60. _alarms.Clear();
  61. }
  62. if (_timers != null)
  63. {
  64. foreach(var timer in _timers)
  65. {
  66. timer.Listener = null;
  67. timer.Cancel();
  68. }
  69. _timers.Clear();
  70. }
  71. }
  72. public void SetAllTimeScales(float timeScale)
  73. {
  74. if (_timers != null)
  75. {
  76. foreach(var timer in _timers)
  77. {
  78. timer.TimeScale = timeScale;
  79. }
  80. }
  81. }
  82. void ITimeListener<IAlarm>.OnCompleted(IAlarm alarm)
  83. {
  84. _alarms?.Remove(alarm);
  85. }
  86. void ITimeListener<ITimer>.OnCompleted(ITimer timer)
  87. {
  88. _timers?.Remove(timer);
  89. }
  90. #endregion
  91. private void AddAlarm(IAlarm alarm)
  92. {
  93. if (_alarms == null)
  94. _alarms = new HashSet<IAlarm>();
  95. _alarms.Add(alarm);
  96. }
  97. private void AddTimer(ITimer timer)
  98. {
  99. if (_timers == null)
  100. _timers = new HashSet<ITimer>();
  101. _timers.Add(timer);
  102. }
  103. }
  104. }