fix pending component type and id lookups

pull/5/head
Evan Hemsley 2019-08-11 11:47:12 -07:00
parent 37a27ee739
commit 8fd2307fbb
2 changed files with 37 additions and 11 deletions

View File

@ -43,8 +43,17 @@ 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 // add these here so entity and component lookups dont break on pending components
IDToComponent[id] = component;
componentIDToEntityID[id] = entity.ID; componentIDToEntityID[id] = entity.ID;
componentIDToType[id] = typeof(TComponent);
if (!typeToComponentIDs.ContainsKey(typeof(TComponent)))
{
typeToComponentIDs.Add(typeof(TComponent), new HashSet<Guid>());
}
typeToComponentIDs[typeof(TComponent)].Add(id);
entityIDToComponentIDs[entity.ID].Add(id);
return id; return id;
} }
@ -57,16 +66,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)
{ {
componentIDToType[componentID] = type;
if (!typeToComponentIDs.ContainsKey(type))
{
typeToComponentIDs.Add(type, new HashSet<Guid>());
}
typeToComponentIDs[type].Add(componentID);
entityIDToComponentIDs[entity.ID].Add(componentID);
if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type)) if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{ {
entityIDToComponentTypeToComponentID[entity.ID][type] = componentID; entityIDToComponentTypeToComponentID[entity.ID][type] = componentID;

View File

@ -593,6 +593,33 @@ namespace Tests
Assert.DoesNotThrow(() => world.Update(0.01)); Assert.DoesNotThrow(() => world.Update(0.01));
} }
[ReadsPending(typeof(MockComponent))]
class GetPendingComponentFromIDEngine : Engine
{
public override void Update(double dt)
{
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{
GetComponentByID<MockComponent>(mockComponentID);
}
}
}
[Test]
public void GetComponentFromID()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddAndRemoveMockComponentEngine());
worldBuilder.AddEngine(new GetPendingComponentFromIDEngine());
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))]