fix panic when using runtime HasComponent

pull/5/head
Evan Hemsley 2019-12-05 16:19:35 -08:00
parent 2a62adee51
commit bdac3d8541
2 changed files with 25 additions and 2 deletions

View File

@ -33,7 +33,7 @@ namespace Encompass
public bool Has(Type type, Entity entity) public bool Has(Type type, Entity entity)
{ {
return Stores[type].Has(entity); return Stores.ContainsKey(type) && Stores[type].Has(entity);
} }
public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent

View File

@ -308,6 +308,8 @@ namespace Tests
world.Update(0.01); world.Update(0.01);
} }
static bool hasComponentRuntimeTypeResult;
[Receives(typeof(HasComponentTestMessage))] [Receives(typeof(HasComponentTestMessage))]
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
class HasComponentWithRuntimeTypeEngine : Engine class HasComponentWithRuntimeTypeEngine : Engine
@ -316,7 +318,7 @@ namespace Tests
{ {
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>()) foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
{ {
Assert.IsTrue(HasComponent(hasComponentTestEngine.entity, typeof(MockComponent))); hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent));
} }
} }
} }
@ -342,6 +344,27 @@ namespace Tests
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01); 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 struct RemoveComponentTestMessage : IMessage