123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.Time
- {
- public class TimeProxy : ITimeProxy, ITimeListener<IAlarm>, ITimeListener<ITimer>
- {
- private ITimeModule _timeModule;
- private HashSet<IAlarm> _alarms;
- private HashSet<ITimer> _timers;
- public TimeProxy(ITimeModule timeModule)
- {
- _timeModule = timeModule;
- }
- #region 接口实现
- public long GetNowTime(ClockType clockTyp = ClockType.Server)
- {
- return _timeModule.GetNowTime(clockTyp);
- }
- public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
- {
- _timeModule.SetTime(timestamp, clockTyp);
- }
- public IAlarm AddAlarm(Action action, long triggerTimeStamp, ClockType clockTyp = ClockType.Server)
- {
- var alarm = _timeModule.AddAlarm(action, triggerTimeStamp, clockTyp);
- alarm.Listener = this;
- AddAlarm(alarm);
- return alarm;
- }
- public ITimer AddDelayTimer(int delay, Action action)
- {
- var timer = _timeModule.AddDelayTimer(delay, action);
- timer.Listener = this;
- AddTimer(timer);
- return timer;
- }
- public ITimer AddDelayLooperTimer(int delay, int interval, Action<int> action, int loopTimes = -1)
- {
- var timer = _timeModule.AddDelayLooperTimer(delay, interval, action, loopTimes);
- timer.Listener = this;
- AddTimer(timer);
- return timer;
- }
- public ITimer AddLooperTimer(int interval, Action<int> action, int loopTimes = -1)
- {
- var timer = _timeModule.AddLooperTimer(interval, action, loopTimes);
- timer.Listener = this;
- AddTimer(timer);
- return timer;
- }
- public void CancelAll()
- {
- if (_alarms != null)
- {
- foreach(var alarm in _alarms)
- {
- alarm.Listener = null;
- alarm.Cancel();
- }
- _alarms.Clear();
- }
- if (_timers != null)
- {
- foreach(var timer in _timers)
- {
- timer.Listener = null;
- timer.Cancel();
- }
- _timers.Clear();
- }
- }
- public void SetAllTimeScales(float timeScale)
- {
- if (_timers != null)
- {
- foreach(var timer in _timers)
- {
- timer.TimeScale = timeScale;
- }
- }
- }
- void ITimeListener<IAlarm>.OnCompleted(IAlarm alarm)
- {
- _alarms?.Remove(alarm);
- }
- void ITimeListener<ITimer>.OnCompleted(ITimer timer)
- {
- _timers?.Remove(timer);
- }
- #endregion
- private void AddAlarm(IAlarm alarm)
- {
- if (_alarms == null)
- _alarms = new HashSet<IAlarm>();
- _alarms.Add(alarm);
- }
- private void AddTimer(ITimer timer)
- {
- if (_timers == null)
- _timers = new HashSet<ITimer>();
- _timers.Add(timer);
- }
- }
- }
|