adds ability to retrieve messages by entity

pull/5/head
Evan Hemsley 2019-12-29 20:36:23 -08:00
parent 3ef34d37ae
commit df7abf4cf3
6 changed files with 107 additions and 1 deletions

View File

@ -48,6 +48,11 @@ namespace Encompass
return Lookup<TMessage>().Any(); return Lookup<TMessage>().Any();
} }
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return Lookup<TMessage>().WithEntity(entityID);
}
public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{ {
foreach (var store in Stores.Values) foreach (var store in Stores.Values)

View File

@ -13,6 +13,7 @@ namespace Encompass
private readonly List<TMessage> store = new List<TMessage>(128); private readonly List<TMessage> store = new List<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>>();
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{ {
@ -54,6 +55,12 @@ namespace Encompass
public void Add(TMessage message) public void Add(TMessage message)
{ {
store.Add(message); store.Add(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);
}
} }
public void Add(TMessage message, double time) public void Add(TMessage message, double time)
@ -81,9 +88,18 @@ namespace Encompass
return store; return store;
} }
public IEnumerable<TMessage> WithEntity(int entityID)
{
return entityToMessage[entityID];
}
public override void Clear() public override void Clear()
{ {
store.Clear(); store.Clear();
foreach (var set in entityToMessage.Values)
{
set.Clear();
}
} }
} }
} }

View File

@ -647,6 +647,11 @@ namespace Encompass
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction); timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
} }
protected IEnumerable<TMessage> MessagesWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
{
return messageManager.WithEntity<TMessage>(entity.ID);
}
internal void CheckAndUpdateTracking(int entityID) internal void CheckAndUpdateTracking(int entityID)
{ {
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits)) if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits))

View File

@ -0,0 +1,7 @@
namespace Encompass
{
public interface IHasEntity
{
Entity Entity { get; }
}
}

View File

@ -14,7 +14,7 @@ namespace Encompass
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{ {
messageStore.AddMessage<TMessage>(message); messageStore.AddMessage(message);
} }
internal void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage internal void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
@ -51,5 +51,10 @@ namespace Encompass
{ {
return messageStore.First<TMessage>(); return messageStore.First<TMessage>();
} }
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return messageStore.WithEntity<TMessage>(entityID);
}
} }
} }

View File

@ -415,6 +415,74 @@ namespace Tests
Assert.Throws<IllegalReadException>(() => world.Update(0.01f)); Assert.Throws<IllegalReadException>(() => world.Update(0.01f));
} }
struct EntityMessage : IMessage, IHasEntity
{
public EntityMessage(Entity entity, int myInt)
{
Entity = entity;
MyInt = myInt;
}
public Entity Entity { get; }
public int MyInt { get; }
}
[Sends(typeof(EntityMessage), typeof(MockMessage))]
class EntityMessageEmitterEngine : Engine
{
private Entity _entity;
public EntityMessageEmitterEngine(Entity entity)
{
_entity = entity;
}
public override void Update(double dt)
{
SendMessage(new EntityMessage(_entity, 2));
SendMessage(new EntityMessage(_entity, 4));
SendMessage(new EntityMessage(_entity, 5));
SendMessage(new MockMessage());
}
}
static List<EntityMessage> entityMessageResults;
[Receives(typeof(EntityMessage))]
class EntityMessageReceiverEngine : Engine
{
private Entity _entity;
public EntityMessageReceiverEngine(Entity entity)
{
_entity = entity;
}
public override void Update(double dt)
{
entityMessageResults = MessagesWithEntity<EntityMessage>(_entity).ToList();
}
}
[Test]
public void MessagesWithEntity()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.AddEngine(new EntityMessageEmitterEngine(entity));
worldBuilder.AddEngine(new EntityMessageReceiverEngine(entity));
var world = worldBuilder.Build();
world.Update(0.01);
entityMessageResults.Should().HaveCount(3);
entityMessageResults.Should().ContainEquivalentOf(new EntityMessage(entity, 2));
entityMessageResults.Should().ContainEquivalentOf(new EntityMessage(entity, 4));
entityMessageResults.Should().ContainEquivalentOf(new EntityMessage(entity, 5));
}
class SomeComponentTestEngine : Engine class SomeComponentTestEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)