2019-06-16 01:55:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
{
|
|
|
|
internal class MessageManager
|
|
|
|
{
|
2019-12-05 04:50:08 +00:00
|
|
|
private readonly TimeManager timeManager;
|
2019-11-21 22:22:10 +00:00
|
|
|
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly Dictionary<Type, List<IMessage>> messageTypeToMessages = new Dictionary<Type, List<IMessage>>();
|
2019-06-16 01:55:35 +00:00
|
|
|
|
2019-08-20 02:05:18 +00:00
|
|
|
private readonly List<(IMessage, double)> delayedMessages = new List<(IMessage, double)>();
|
2019-11-21 22:22:10 +00:00
|
|
|
private readonly List<(IMessage, double)> delayedMessagesIgnoringTimeDilation = new List<(IMessage, double)>();
|
|
|
|
|
|
|
|
public MessageManager(TimeManager timeManager)
|
|
|
|
{
|
|
|
|
this.timeManager = timeManager;
|
|
|
|
}
|
2019-08-20 02:05:18 +00:00
|
|
|
|
2019-07-29 02:17:00 +00:00
|
|
|
internal void RegisterMessageType(Type messageType)
|
2019-06-16 01:55:35 +00:00
|
|
|
{
|
2019-11-21 22:22:10 +00:00
|
|
|
if (!messageTypeToMessages.ContainsKey(messageType))
|
|
|
|
{
|
2019-07-29 02:17:00 +00:00
|
|
|
messageTypeToMessages.Add(messageType, new List<IMessage>());
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 05:25:34 +00:00
|
|
|
internal void AddMessage(IMessage message)
|
|
|
|
{
|
|
|
|
var type = message.GetType();
|
|
|
|
|
|
|
|
messageTypeToMessages[type].Add(message);
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:05:18 +00:00
|
|
|
internal void AddMessageDelayed(IMessage message, double time)
|
|
|
|
{
|
|
|
|
if (!messageTypeToMessages.ContainsKey(message.GetType())) { messageTypeToMessages.Add(message.GetType(), new List<IMessage>()); }
|
|
|
|
|
|
|
|
delayedMessages.Add((message, time));
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:22:10 +00:00
|
|
|
internal void AddMessageDelayedIgnoringTimeDilation(IMessage message, double time)
|
2019-07-29 02:17:00 +00:00
|
|
|
{
|
2019-11-21 22:22:10 +00:00
|
|
|
if (!messageTypeToMessages.ContainsKey(message.GetType())) { messageTypeToMessages.Add(message.GetType(), new List<IMessage>()); }
|
|
|
|
|
|
|
|
delayedMessagesIgnoringTimeDilation.Add((message, time));
|
2019-07-29 02:17:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:55:35 +00:00
|
|
|
internal void ClearMessages()
|
|
|
|
{
|
|
|
|
foreach (var entry in messageTypeToMessages)
|
|
|
|
{
|
|
|
|
entry.Value.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:05:18 +00:00
|
|
|
internal void ProcessDelayedMessages(double dt)
|
|
|
|
{
|
|
|
|
for (int i = delayedMessages.Count - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var (message, time) = delayedMessages[i];
|
|
|
|
|
2019-11-21 22:22:10 +00:00
|
|
|
var updatedTime = time - (dt * timeManager.TimeDilationFactor);
|
2019-08-20 02:05:18 +00:00
|
|
|
|
|
|
|
if (updatedTime <= 0)
|
|
|
|
{
|
|
|
|
AddMessage(message);
|
|
|
|
delayedMessages.RemoveAt(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delayedMessages[i] = (message, updatedTime);
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 22:22:10 +00:00
|
|
|
|
|
|
|
for (int i = delayedMessagesIgnoringTimeDilation.Count - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var (message, time) = delayedMessagesIgnoringTimeDilation[i];
|
|
|
|
|
|
|
|
var updatedTime = time - dt;
|
|
|
|
|
|
|
|
if (updatedTime <= 0)
|
|
|
|
{
|
|
|
|
AddMessage(message);
|
|
|
|
delayedMessagesIgnoringTimeDilation.RemoveAt(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delayedMessagesIgnoringTimeDilation[i] = (message, updatedTime);
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:55:35 +00:00
|
|
|
internal IEnumerable<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
|
|
|
|
{
|
2019-06-22 00:23:52 +00:00
|
|
|
return messageTypeToMessages.ContainsKey(typeof(TMessage)) ?
|
|
|
|
messageTypeToMessages[typeof(TMessage)].Cast<TMessage>() :
|
|
|
|
Enumerable.Empty<TMessage>();
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|