fix broken entity ID lookup on pending components

pull/5/head
Evan Hemsley 2019-08-11 11:26:31 -07:00
parent ccf243ad2d
commit 162dbe52a2
3 changed files with 46 additions and 12 deletions

View File

@ -43,6 +43,8 @@ namespace Encompass
{ {
var id = NextID(); var id = NextID();
componentAddData.Add((entity, typeof(TComponent), id, component)); componentAddData.Add((entity, typeof(TComponent), id, component));
IDToComponent[id] = component; // add these here so entity lookup doesnt break on pending components
componentIDToEntityID[id] = entity.ID;
return id; return id;
} }
@ -55,7 +57,6 @@ namespace Encompass
internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component) internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{ {
IDToComponent[componentID] = component;
componentIDToType[componentID] = type; componentIDToType[componentID] = type;
if (!typeToComponentIDs.ContainsKey(type)) if (!typeToComponentIDs.ContainsKey(type))
@ -74,8 +75,6 @@ namespace Encompass
{ {
throw new MultipleComponentOfSameTypeException("Entity {0} cannot have multiple components of type {1}", entity.ID, type.Name); throw new MultipleComponentOfSameTypeException("Entity {0} cannot have multiple components of type {1}", entity.ID, type.Name);
} }
componentIDToEntityID[componentID] = entity.ID;
} }
internal void AddMarkedComponents() internal void AddMarkedComponents()

View File

@ -124,14 +124,7 @@ namespace Encompass
} }
componentMessageTypeToComponentIDs[typeof(TComponent)].Add(componentID); componentMessageTypeToComponentIDs[typeof(TComponent)].Add(componentID);
if (!entityToTypeToComponentID[entity].ContainsKey(typeof(TComponent))) entityToTypeToComponentID[entity][typeof(TComponent)] = componentID;
{
entityToTypeToComponentID[entity].Add(typeof(TComponent), componentID);
}
else
{
throw new MultipleComponentOfSameTypeException("Entity {0} cannot have multiple components of type {1}", entity.ID, typeof(TComponent).Name);
}
} }
// general component reads by type // general component reads by type

View File

@ -546,11 +546,53 @@ namespace Tests
worldBuilder.AddComponent(entity, component); worldBuilder.AddComponent(entity, component);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01);
Assert.That(entity, Is.EqualTo(entityFromComponentIDResult)); Assert.That(entity, Is.EqualTo(entityFromComponentIDResult));
} }
[Activates(typeof(MockComponent))]
[Reads(typeof(MockComponent))]
class AddAndRemoveMockComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{
var entity = GetEntityByComponentID(mockComponentID);
RemoveComponent(mockComponentID);
AddComponent(entity, new MockComponent());
}
}
}
[ReadsPending(typeof(MockComponent))]
class GetEntityFromPendingComponentIDEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{
entityFromComponentIDResult = GetEntityByComponentID(mockComponentID);
}
}
}
[Test]
public void GetEntityFromPendingComponentID()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddAndRemoveMockComponentEngine());
worldBuilder.AddEngine(new GetEntityFromPendingComponentIDEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent());
var world = worldBuilder.Build();
Assert.DoesNotThrow(() => world.Update(0.01));
}
static MockComponent mockComponentByIDResult; static MockComponent mockComponentByIDResult;
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]