rework typed message store
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-22 12:12:26 -07:00
parent e3d8f42bd9
commit 002cff1c2e
2 changed files with 29 additions and 16 deletions

View File

@ -10,10 +10,12 @@ namespace Encompass
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
{
private readonly List<TMessage> _store = new List<TMessage>(128);
private readonly List<int> _indices = new List<int>();
private readonly TMessage[] _store = new TMessage[128];
private readonly List<(TMessage, double)> _delayedStore = new List<(TMessage, double)>(128);
private readonly List<(TMessage, double)> _delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128);
private readonly Dictionary<int, List<TMessage>> _entityToMessage = new Dictionary<int, List<TMessage>>();
private readonly Dictionary<int, HashSet<int>> _entityToIndices = new Dictionary<int, HashSet<int>>();
private readonly IDManager _idManager = new IDManager();
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{
@ -52,56 +54,67 @@ namespace Encompass
}
}
public void Add(TMessage message)
public void Add(in TMessage message)
{
_store.Add(message);
var index = _idManager.NextID();
_indices.Add(index);
_store[index] = message;
if (message is IHasEntity entityMessage)
{
var entityID = entityMessage.Entity.ID;
if (!_entityToMessage.ContainsKey(entityID)) { _entityToMessage.Add(entityID, new List<TMessage>()); }
_entityToMessage[entityID].Add(message);
if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new HashSet<int>()); }
_entityToIndices[entityID].Add(index);
}
}
public void Add(TMessage message, double time)
public void Add(in TMessage message, double time)
{
_delayedStore.Add((message, time));
}
public void AddIgnoringTimeDilation(TMessage message, double time)
public void AddIgnoringTimeDilation(in TMessage message, double time)
{
_delayedStoreIgnoringTimeDilation.Add((message, time));
}
public TMessage First()
{
return _store[0];
return _store[_indices[0]];
}
public bool Any()
{
return _store.Count > 0;
return _indices.Count > 0;
}
public IEnumerable<TMessage> All()
{
return _store;
foreach (var index in _indices)
{
yield return _store[index];
}
}
public IEnumerable<TMessage> WithEntity(int entityID)
{
return _entityToMessage.ContainsKey(entityID) ? _entityToMessage[entityID] : System.Linq.Enumerable.Empty<TMessage>();
if (_entityToIndices.ContainsKey(entityID))
{
foreach (var index in _entityToIndices[entityID])
{
yield return _store[index];
}
}
}
public bool SomeWithEntity(int entityID)
{
return _entityToMessage.ContainsKey(entityID) && _entityToMessage[entityID].Count > 0;
return _entityToIndices.ContainsKey(entityID) && _entityToIndices[entityID].Count > 0;
}
public override void Clear()
{
_store.Clear();
foreach (var set in _entityToMessage.Values)
_indices.Clear();
foreach (var set in _entityToIndices.Values)
{
set.Clear();
}

View File

@ -48,7 +48,7 @@ namespace Tests
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
{
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
var gottenComponent = GetComponent<MockComponent>(addComponentTestMessage.entity);
ref readonly var gottenComponent = ref GetComponent<MockComponent>(addComponentTestMessage.entity);
gottenComponent.Should().BeEquivalentTo(addComponentTestMessage.mockComponent);
}
}