diff --git a/src/ComponentManager.cs b/src/ComponentManager.cs index 4218195..d5545b2 100644 --- a/src/ComponentManager.cs +++ b/src/ComponentManager.cs @@ -49,7 +49,9 @@ namespace Encompass internal IEnumerable> GetActiveComponentsByType() where TComponent : struct, IComponent { - return activeComponents[typeof(TComponent)].Select((id) => new KeyValuePair(id, (TComponent)IDToComponent[id])); + return activeComponents.ContainsKey(typeof(TComponent)) ? + activeComponents[typeof(TComponent)].Select((id) => new KeyValuePair(id, (TComponent)IDToComponent[id])) : + Enumerable.Empty>(); } internal KeyValuePair GetActiveComponentByType() where TComponent : struct, IComponent diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 189c5ba..39360f9 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -295,5 +295,67 @@ namespace Tests Assert.Throws(() => world.Update(0.01f)); } + + static KeyValuePair pairA; + static KeyValuePair pairB; + + class SameValueComponentReadEngine : Engine + { + public override void Update(float dt) + { + var components = this.ReadComponents(); + + pairA = components.First(); + pairB = components.Last(); + } + } + + // Tests that components with identical values should be distinguishable by ID + [Test] + public void SameValueComponents() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + MockComponent componentA; + componentA.myInt = 20; + componentA.myString = "hello"; + + MockComponent componentB; + componentB.myInt = 20; + componentB.myString = "hello"; + + var entity = worldBuilder.CreateEntity(); + entity.AddComponent(componentA); + entity.AddComponent(componentB); + + var world = worldBuilder.Build(); + world.Update(0.01f); + + Assert.That(pairA, Is.Not.EqualTo(pairB)); + Assert.That(pairA.Value, Is.EqualTo(pairB.Value)); + } + + static IEnumerable> emptyComponentReadResult; + + class ReadEmptyMockComponentsEngine : Engine + { + public override void Update(float dt) + { + emptyComponentReadResult = this.ReadComponents(); + } + } + + [Test] + public void ReadComponentsOfTypeWhereNoneExist() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(); + + var world = worldBuilder.Build(); + world.Update(0.01f); + + Assert.That(emptyComponentReadResult, Is.Empty); + } } }