123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System;
- namespace XGame.Framework.Time
- {
- public class Clock : IClock
- {
- private AlarmRepository _clientAlarmRepo;
- private AlarmRepository _serverAlarmRepo;
- private TimerRepository _timerRepo;
- public Clock(long timeStamp)
- {
- var realTime = UnityEngine.Time.realtimeSinceStartup;
- _clientAlarmRepo = new AlarmRepository()
- {
- time = new TimeStamp()
- {
- realtimeSinceStartup = realTime,
- timeStamp = timeStamp
- }
- };
- _serverAlarmRepo = new AlarmRepository()
- {
- time = new TimeStamp()
- {
- realtimeSinceStartup = realTime,
- timeStamp = timeStamp
- }
- };
- _timerRepo = new TimerRepository();
- }
- private AlarmRepository GetAlarmRepository(ClockType clockTyp = ClockType.Server)
- {
- return clockTyp == ClockType.Server ? _serverAlarmRepo : _clientAlarmRepo;
- }
- public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
- {
- GetAlarmRepository(clockTyp).SetTime(timestamp);
- }
- public long GetNowTime(ClockType clockTyp = ClockType.Server)
- {
- var alarmTime = GetAlarmRepository(clockTyp).time;
- return alarmTime.timeStamp + (long)((UnityEngine.Time.realtimeSinceStartup - alarmTime.realtimeSinceStartup) * 1000);
- }
- public IAlarm AddAlarm(long triggerTimestamp, Action action, ClockType clockTyp = ClockType.Server)
- {
- return GetAlarmRepository(clockTyp).AddAlarm(action, GetNowTime(clockTyp), triggerTimestamp);
- }
- public ITimer AddDelayTimer(int delay, Action action)
- {
- return _timerRepo.AddDelayTimer(delay, action);
- }
- public ITimer AddLooperTimer(int delay, int interval, Action action)
- {
- return _timerRepo.AddLooperTimer(delay, interval, action);
- }
- public ITimer AddLooperTimer(int delay, int interval, Action<int> action)
- {
- return _timerRepo.AddLooperTimer(delay, interval, action);
- }
- public ITimer AddLooperTimer(int delay, int interval, int loopTime, Action action)
- {
- return _timerRepo.AddLooperTimer(delay, interval, loopTime, action);
- }
- public ITimer AddLooperTimer(int delay, int interval, int loopTime, Action<int> action)
- {
- return _timerRepo.AddLooperTimer(delay, interval, loopTime, action);
- }
- public void Update(int millisecond)
- {
- _clientAlarmRepo.ClockUpdate(millisecond);
- _serverAlarmRepo.ClockUpdate(millisecond);
- _timerRepo.ClockUpdate(millisecond);
- }
- public void Dispose()
- {
- _clientAlarmRepo.Dispose();
- _serverAlarmRepo.Dispose();
- _timerRepo.Dispose();
- }
- }
- }
|