diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 22446fe..8061e86 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -43,8 +43,17 @@ namespace Encompass { var id = NextID(); 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; + componentIDToType[id] = typeof(TComponent); + + if (!typeToComponentIDs.ContainsKey(typeof(TComponent))) + { + typeToComponentIDs.Add(typeof(TComponent), new HashSet()); + } + typeToComponentIDs[typeof(TComponent)].Add(id); + entityIDToComponentIDs[entity.ID].Add(id); return id; } @@ -57,16 +66,6 @@ namespace Encompass internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component) { - componentIDToType[componentID] = type; - - if (!typeToComponentIDs.ContainsKey(type)) - { - typeToComponentIDs.Add(type, new HashSet()); - } - - typeToComponentIDs[type].Add(componentID); - - entityIDToComponentIDs[entity.ID].Add(componentID); if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type)) { entityIDToComponentTypeToComponentID[entity.ID][type] = componentID; diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 5fc58d6..a3ac290 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -593,6 +593,33 @@ namespace Tests Assert.DoesNotThrow(() => world.Update(0.01)); } + [ReadsPending(typeof(MockComponent))] + class GetPendingComponentFromIDEngine : Engine + { + public override void Update(double dt) + { + foreach (var (mockComponentID, mockComponent) in ReadComponents()) + { + GetComponentByID(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; [Reads(typeof(MockComponent))]