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

@ -164,7 +164,7 @@ namespace Encompass
}
/// <summary>
/// Returns an Entity containing the specified Component type.
/// Returns an Entity containing the specified Component type.
/// </summary>
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
@ -173,7 +173,7 @@ namespace Encompass
}
/// <summary>
/// Returns all Entities containing the specified Component type.
/// Returns all Entities containing the specified Component type.
/// </summary>
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
@ -250,7 +250,7 @@ namespace Encompass
}
/// <summary>
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// </summary>
protected (Guid, TComponent) ReadComponent<TComponent>() where TComponent : struct, IComponent
{
@ -488,7 +488,7 @@ namespace Encompass
}
/// <summary>
/// Overwrites Component struct data associated with the specified Component ID.
/// Overwrites Component struct data associated with the specified Component ID.
/// </summary>
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
{
@ -496,7 +496,7 @@ namespace Encompass
}
/// <summary>
/// Sets Draw Component data for the specified Component Type on the specified Entity.
/// Sets Draw Component data for the specified Component Type on the specified Entity.
/// This method must be used for the Draw Component to be readable by an OrderedRenderer.
/// If Component data for this Type already existed on the Entity, the component data is overwritten.
/// </summary>
@ -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>
/// Removes the Component with the specified ID from its Entity.
/// </summary>

View File

@ -1117,5 +1117,38 @@ namespace Tests
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();
}
}
}