From bdac3d85418d02444f3c1fe5f8bd3f6e4ef3fe84 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Thu, 5 Dec 2019 16:19:35 -0800 Subject: [PATCH] fix panic when using runtime HasComponent --- encompass-cs/Collections/ComponentStore.cs | 2 +- test/ComponentTest.cs | 25 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index 4528cca..b081f99 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -33,7 +33,7 @@ namespace Encompass public bool Has(Type type, Entity entity) { - return Stores[type].Has(entity); + return Stores.ContainsKey(type) && Stores[type].Has(entity); } public TComponent Get(Entity entity) where TComponent : struct, IComponent diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 957ac91..a27ecf6 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -308,6 +308,8 @@ namespace Tests world.Update(0.01); } + static bool hasComponentRuntimeTypeResult; + [Receives(typeof(HasComponentTestMessage))] [Reads(typeof(MockComponent))] class HasComponentWithRuntimeTypeEngine : Engine @@ -316,7 +318,7 @@ namespace Tests { foreach (var hasComponentTestEngine in ReadMessages()) { - Assert.IsTrue(HasComponent(hasComponentTestEngine.entity, typeof(MockComponent))); + hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent)); } } } @@ -342,6 +344,27 @@ namespace Tests var world = worldBuilder.Build(); world.Update(0.01); + + Assert.IsTrue(hasComponentRuntimeTypeResult); + } + + [Test] + public void HasComponentWithRuntimeTypeFalseWhenNoneHaveBeenCreated() + { + var worldBuilder = new WorldBuilder(); + worldBuilder.AddEngine(new HasComponentWithRuntimeTypeEngine()); + + var entity = worldBuilder.CreateEntity(); + + HasComponentTestMessage hasComponentTestMessage; + hasComponentTestMessage.entity = entity; + worldBuilder.SendMessage(hasComponentTestMessage); + + var world = worldBuilder.Build(); + + world.Update(0.01); + + Assert.IsFalse(hasComponentRuntimeTypeResult); } struct RemoveComponentTestMessage : IMessage