TimeModule.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using XGame.Framework.ThreadScheduler;
  3. namespace XGame.Framework.Time
  4. {
  5. public class TimeModule : ITimeModule, ITimeUpdate, IDisposable
  6. {
  7. private IClock _clock;
  8. private DateTime _utcStartTime;
  9. public void Initialize()
  10. {
  11. _utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  12. double nowTimestamp = (DateTime.UtcNow - _utcStartTime).TotalMilliseconds;
  13. _clock = new Clock((long)nowTimestamp);
  14. MainThreadUpdateRegister.update += Update;
  15. }
  16. #region ITimeModule接口实现
  17. public long GetNowTime(ClockType clockTyp = ClockType.Server)
  18. {
  19. return _clock.GetNowTime(clockTyp);
  20. }
  21. public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
  22. {
  23. _clock.SetTime(timestamp, clockTyp);
  24. }
  25. public IAlarm AddAlarm(Action action, long triggerTimestamp, ClockType clockTyp = ClockType.Server)
  26. {
  27. return _clock.AddAlarm(triggerTimestamp, action, clockTyp);
  28. }
  29. public ITimer AddDelayTimer(int delay, Action action)
  30. {
  31. return _clock.AddDelayTimer(delay, action);
  32. }
  33. public ITimer AddLooperTimer(int interval, Action<int> action, int loopTimes = -1)
  34. {
  35. return _clock.AddLooperTimer(interval, interval, action, loopTimes);
  36. }
  37. public ITimer AddDelayLooperTimer(int delay, int interval, Action<int> action, int loopTimes = -1)
  38. {
  39. return _clock.AddLooperTimer(delay, interval, action, loopTimes);
  40. }
  41. #endregion
  42. #region IUpdate接口实现
  43. public void Update(int elapse)
  44. {
  45. _clock.Update(elapse);
  46. }
  47. #endregion
  48. public void Dispose()
  49. {
  50. MainThreadUpdateRegister.update -= Update;
  51. _clock.Dispose();
  52. _clock = null;
  53. }
  54. }
  55. }