rework typed message store
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e3d8f42bd9
commit
002cff1c2e
|
@ -10,10 +10,12 @@ namespace Encompass
|
||||||
|
|
||||||
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
|
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)> _delayedStore = new List<(TMessage, double)>(128);
|
||||||
private readonly List<(TMessage, double)> _delayedStoreIgnoringTimeDilation = 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)
|
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)
|
if (message is IHasEntity entityMessage)
|
||||||
{
|
{
|
||||||
var entityID = entityMessage.Entity.ID;
|
var entityID = entityMessage.Entity.ID;
|
||||||
if (!_entityToMessage.ContainsKey(entityID)) { _entityToMessage.Add(entityID, new List<TMessage>()); }
|
if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new HashSet<int>()); }
|
||||||
_entityToMessage[entityID].Add(message);
|
_entityToIndices[entityID].Add(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(TMessage message, double time)
|
public void Add(in TMessage message, double time)
|
||||||
{
|
{
|
||||||
_delayedStore.Add((message, time));
|
_delayedStore.Add((message, time));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddIgnoringTimeDilation(TMessage message, double time)
|
public void AddIgnoringTimeDilation(in TMessage message, double time)
|
||||||
{
|
{
|
||||||
_delayedStoreIgnoringTimeDilation.Add((message, time));
|
_delayedStoreIgnoringTimeDilation.Add((message, time));
|
||||||
}
|
}
|
||||||
|
|
||||||
public TMessage First()
|
public TMessage First()
|
||||||
{
|
{
|
||||||
return _store[0];
|
return _store[_indices[0]];
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any()
|
public bool Any()
|
||||||
{
|
{
|
||||||
return _store.Count > 0;
|
return _indices.Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TMessage> All()
|
public IEnumerable<TMessage> All()
|
||||||
{
|
{
|
||||||
return _store;
|
foreach (var index in _indices)
|
||||||
|
{
|
||||||
|
yield return _store[index];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TMessage> WithEntity(int entityID)
|
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)
|
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()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
_store.Clear();
|
_indices.Clear();
|
||||||
foreach (var set in _entityToMessage.Values)
|
foreach (var set in _entityToIndices.Values)
|
||||||
{
|
{
|
||||||
set.Clear();
|
set.Clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ namespace Tests
|
||||||
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
|
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
|
||||||
{
|
{
|
||||||
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
|
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);
|
gottenComponent.Should().BeEquivalentTo(addComponentTestMessage.mockComponent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue