Convert alot of methods to use TimeSpan.

This commit is contained in:
hondacrx
2022-03-01 23:47:53 -05:00
parent 6f1b2f5cd5
commit 042bfc12e5
31 changed files with 143 additions and 127 deletions
+19 -7
View File
@@ -203,12 +203,24 @@ namespace Framework.Dynamic
}
/// <summary>
/// Delays all events. If delay is greater than or equal internal timer, delay will be 0.
/// Delays all events.
/// </summary>
/// <param name="delay">Amount of delay as TimeSpan type.</param>
public void DelayEvents(TimeSpan delay)
{
_time = (uint)(delay.TotalMilliseconds < _time ? _time - delay.TotalMilliseconds : 0);
if (Empty())
return;
MultiMap<uint, uint> delayed = new();
foreach (var pair in _eventMap.KeyValueList)
{
delayed.Add(pair.Key + (uint)delay.TotalMilliseconds, pair.Value);
_eventMap.Remove(pair.Key, pair.Value);
}
foreach (var del in delayed)
_eventMap.Add(del);
}
/// <summary>
@@ -269,17 +281,17 @@ namespace Framework.Dynamic
}
/// <summary>
/// Returns time in milliseconds until next event.
/// Returns time as TimeSpan type until next event.
/// </summary>
/// <param name="eventId">Id of the event.</param>
/// <returns>Time of next event.</returns>
public uint GetTimeUntilEvent(uint eventId)
/// <returns>Time of next event. If event is not scheduled returns <see cref="TimeSpan.MaxValue"/></returns>
public TimeSpan GetTimeUntilEvent(uint eventId)
{
foreach (var pair in _eventMap)
if (eventId == (pair.Value & 0x0000FFFF))
return pair.Key - _time;
return TimeSpan.FromMilliseconds(pair.Key - _time);
return uint.MaxValue;
return TimeSpan.MaxValue;
}
/// <summary>
+13 -11
View File
@@ -58,7 +58,7 @@ namespace Framework.Dynamic
// Reschedule non deletable events to be checked at
// the next update tick
AddEvent(Event, CalculateTime(1), false);
AddEvent(Event, CalculateTime(TimeSpan.FromMilliseconds(1)), false);
}
}
@@ -87,38 +87,40 @@ namespace Framework.Dynamic
m_events.Clear();
}
public void AddEvent(BasicEvent Event, ulong e_time, bool set_addtime = true)
public void AddEvent(BasicEvent Event, TimeSpan e_time, bool set_addtime = true)
{
if (set_addtime)
Event.m_addTime = m_time;
Event.m_execTime = e_time;
m_events.Add(e_time, Event);
Event.m_execTime = (ulong)e_time.TotalMilliseconds;
m_events.Add((ulong)e_time.TotalMilliseconds, Event);
}
public void AddEvent(Action action, ulong e_time, bool set_addtime = true) { AddEvent(new LambdaBasicEvent(action), e_time, set_addtime); }
public void AddEvent(Action action, TimeSpan e_time, bool set_addtime = true) { AddEvent(new LambdaBasicEvent(action), e_time, set_addtime); }
public void AddEventAtOffset(BasicEvent Event, TimeSpan offset) { AddEvent(Event, CalculateTime(offset)); }
public void AddEventAtOffset(BasicEvent Event, TimeSpan offset) { AddEvent(Event, CalculateTime((ulong)offset.TotalMilliseconds)); }
public void AddEventAtOffset(BasicEvent Event, TimeSpan offset, TimeSpan offset2) { AddEvent(Event, CalculateTime(RandomHelper.RandTime(offset, offset2))); }
public void AddEventAtOffset(Action action, TimeSpan offset) { AddEventAtOffset(new LambdaBasicEvent(action), offset); }
public void ModifyEventTime(BasicEvent Event, ulong newTime)
public void ModifyEventTime(BasicEvent Event, TimeSpan newTime)
{
foreach (var pair in m_events)
{
if (pair.Value != Event)
continue;
Event.m_execTime = newTime;
Event.m_execTime = (ulong)newTime.TotalMilliseconds;
m_events.Remove(pair);
m_events.Add(newTime, Event);
m_events.Add((ulong)newTime.TotalMilliseconds, Event);
break;
}
}
public ulong CalculateTime(ulong t_offset)
public TimeSpan CalculateTime(TimeSpan t_offset)
{
return (m_time + t_offset);
return TimeSpan.FromMilliseconds(m_time) + t_offset;
}
public SortedMultiMap<ulong, BasicEvent> GetEvents() { return m_events; }