using System;
using System.Collections.Generic;
namespace XGame.Framework.Time
{
///
/// 闹钟仓储
///
public class AlarmRepository : IClockTick, ICalibrator, IDisposable
{
private List _aliveAlarms = new List();
public TimeStamp time;
internal IAlarm AddAlarm(Action action, long curTimeStamp, long triggerTimeStamp)
{
var alarm = new Alarm(action, curTimeStamp, triggerTimeStamp);
_aliveAlarms.Add(alarm);
return alarm;
}
public void SetTime(long timestamp)
{
time.timeStamp = timestamp;
time.realtimeSinceStartup = UnityEngine.Time.realtimeSinceStartup;
for (int i = 0; i < _aliveAlarms.Count; i++)
{
if (_aliveAlarms[i] is ICalibrator calib)
{
calib.SetTime(timestamp);
}
}
}
public void ClockUpdate(int millisecond)
{
// 客户端定时器数量较少不考虑时间轮优化
for (int i = _aliveAlarms.Count - 1; i >= 0; --i)
{
if(_aliveAlarms[i].Update(millisecond))
{
_aliveAlarms.RemoveAt(i);
}
}
}
public void Dispose()
{
time = default;
foreach (var item in _aliveAlarms)
{
item.Dispose();
}
_aliveAlarms.Clear();
}
}
}