fix non immediate component remove

pull/5/head
Evan Hemsley 2019-12-26 21:25:24 -08:00
parent 1a17f9a8e1
commit e3343e4a00
3 changed files with 54 additions and 10 deletions

View File

@ -97,14 +97,12 @@ namespace Encompass
return false; return false;
} }
public bool Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent public void Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
{ {
if (componentUpdateManager.Remove<TComponent>(entity, priority)) if (componentUpdateManager.Remove<TComponent>(entity, priority))
{ {
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID); drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
return true;
} }
return false;
} }
} }
} }

View File

@ -76,13 +76,7 @@ namespace Encompass
internal bool Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent internal bool Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
{ {
if (existingComponentStore.Remove<TComponent>(entity.ID, priority)) return UpToDateComponentStore.Remove<TComponent>(entity.ID, priority);
{
UpToDateComponentStore.Remove<TComponent>(entity.ID, priority);
return true;
}
return false;
} }
public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent

View File

@ -1539,6 +1539,58 @@ namespace Tests
_components.Should().NotBeEmpty(); _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<MockTimerComponent>())
{
var updatedComponent = component;
updatedComponent.Timer -= dt;
if (updatedComponent.Timer <= 0)
{
RemoveComponent<MockTimerComponent>(entity);
}
else
{
SetComponent<MockTimerComponent>(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));
}
} }
} }
} }