diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index c8c0cc9..5518bba 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -97,14 +97,12 @@ namespace Encompass return false; } - public bool Remove(Entity entity, int priority) where TComponent : struct, IComponent + public void Remove(Entity entity, int priority) where TComponent : struct, IComponent { if (componentUpdateManager.Remove(entity, priority)) { drawLayerManager.UnRegisterComponentWithLayer(entity.ID); - return true; } - return false; } } } diff --git a/encompass-cs/ComponentUpdateManager.cs b/encompass-cs/ComponentUpdateManager.cs index ea6d521..49ec6eb 100644 --- a/encompass-cs/ComponentUpdateManager.cs +++ b/encompass-cs/ComponentUpdateManager.cs @@ -76,13 +76,7 @@ namespace Encompass internal bool Remove(Entity entity, int priority) where TComponent : struct, IComponent { - if (existingComponentStore.Remove(entity.ID, priority)) - { - UpToDateComponentStore.Remove(entity.ID, priority); - return true; - } - - return false; + return UpToDateComponentStore.Remove(entity.ID, priority); } public bool UpdateComponent(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent diff --git a/test/EngineTest.cs b/test/EngineTest.cs index c518f1b..a240adf 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -1539,6 +1539,58 @@ namespace Tests _components.Should().NotBeEmpty(); } + + struct MockTimerComponent : IComponent + { + public MockTimerComponent(double time) + { + Timer = time; + } + + public double Timer { get; set; } + } + + [Reads(typeof(MockTimerComponent))] + [Writes(typeof(MockTimerComponent))] + class ReadWhileRemovingComponentsEngine : Engine + { + public override void Update(double dt) + { + foreach (var (component, entity) in ReadComponentsIncludingEntity()) + { + var updatedComponent = component; + updatedComponent.Timer -= dt; + + if (updatedComponent.Timer <= 0) + { + RemoveComponent(entity); + + } + else + { + SetComponent(entity, updatedComponent); + } + } + } + } + + [Test] + public void ReadWhileRemovingComponents() + { + var worldBuilder = new WorldBuilder(); + + var entity = worldBuilder.CreateEntity(); + worldBuilder.SetComponent(entity, new MockTimerComponent(0.5)); + + var entityB = worldBuilder.CreateEntity(); + worldBuilder.SetComponent(entityB, new MockTimerComponent(0.4)); + + worldBuilder.AddEngine(new ReadWhileRemovingComponentsEngine()); + + var world = worldBuilder.Build(); + Assert.DoesNotThrow(() => world.Update(0.2)); + Assert.DoesNotThrow(() => world.Update(0.25)); + } } } }