12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using XGame.Framework.ThreadScheduler;
- namespace XGame.Framework.Time
- {
- public class TimeModule : ITimeModule, ITimeUpdate, IDisposable
- {
- private IClock _clock;
- private DateTime _utcStartTime;
- public void Initialize()
- {
- _utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- double nowTimestamp = (DateTime.UtcNow - _utcStartTime).TotalMilliseconds;
- _clock = new Clock((long)nowTimestamp);
- MainThreadUpdateRegister.update += Update;
- }
- #region ITimeModule接口实现
- public long GetNowTime(ClockType clockTyp = ClockType.Server)
- {
- return _clock.GetNowTime(clockTyp);
- }
- public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
- {
- _clock.SetTime(timestamp, clockTyp);
- }
- public IAlarm AddAlarm(Action action, long triggerTimestamp, ClockType clockTyp = ClockType.Server)
- {
- return _clock.AddAlarm(triggerTimestamp, action, clockTyp);
- }
- public ITimer AddDelayTimer(int delay, Action action)
- {
- return _clock.AddDelayTimer(delay, action);
- }
- public ITimer AddLooperTimer(int interval, Action action)
- {
- return _clock.AddLooperTimer(interval,interval, action);
- }
- public ITimer AddLooperTimer(int interval, Action<int> action)
- {
- return _clock.AddLooperTimer(interval, interval, action);
- }
- public ITimer AddLooperTimer(int interval, int loopTimes, Action action)
- {
- return _clock.AddLooperTimer(interval, interval, loopTimes, action);
- }
- public ITimer AddLooperTimer(int interval, int loopTimes, Action<int> action)
- {
- return _clock.AddLooperTimer(interval, interval, loopTimes, action);
- }
- #endregion
- #region IUpdate接口实现
- public void Update(int elapse)
- {
- _clock.Update(elapse);
- }
- #endregion
- public void Dispose()
- {
- MainThreadUpdateRegister.update -= Update;
- _clock.Dispose();
- _clock = null;
- }
- public ITimer AddDelayLooperTimer(int delay, int interval, Action action)
- {
- return _clock.AddLooperTimer(delay, interval, action);
- }
- public ITimer AddDelayLooperTimer(int delay, int interval, Action<int> action)
- {
- return _clock.AddLooperTimer(delay, interval, action);
- }
- public ITimer AddDelayLooperTimer(int delay, int interval, int loopTimes, Action action)
- {
- return _clock.AddLooperTimer(delay, interval, loopTimes, action);
- }
- public ITimer AddDelayLooperTimer(int delay, int interval, int loopTimes, Action<int> action)
- {
- return _clock.AddLooperTimer(delay, interval, loopTimes, action);
- }
- }
- }
|