adds Engine.RemoveComponent<TComponent>(Entity)

pull/5/head
Evan Hemsley 2019-11-17 10:48:39 -08:00
parent bc66012c3d
commit b8d66e7642
2 changed files with 48 additions and 5 deletions

View File

@ -657,6 +657,16 @@ namespace Encompass
} }
} }
/// <summary>
/// Removes a Component with the specified type from the given Entity.
/// Note that the Engine must Read the Component type that is being removed.
/// </summary>
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
var (componentID, _) = GetComponent<TComponent>(entity);
RemoveComponent(componentID);
}
/// <summary> /// <summary>
/// Removes the Component with the specified ID from its Entity. /// Removes the Component with the specified ID from its Entity.
/// </summary> /// </summary>

View File

@ -1117,5 +1117,38 @@ namespace Tests
readEntities.Should().BeEmpty(); readEntities.Should().BeEmpty();
} }
[Reads(typeof(MockComponent))]
class RemoveComponentByTypeEngine : Engine
{
public override void Update(double dt)
{
foreach (var (_, _, entity) in ReadComponentsIncludingEntity<MockComponent>())
{
RemoveComponent<MockComponent>(entity);
}
}
}
[Test]
public void RemoveComponentByType()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ReadComponentsTestEngine());
worldBuilder.AddEngine(new RemoveComponentByTypeEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { });
var entityB = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { });
var world = worldBuilder.Build();
world.Update(0.01);
world.Update(0.01);
resultComponents.Should().BeEmpty();
}
} }
} }