adds Engine.RemoveComponent<TComponent>(Entity)
parent
bc66012c3d
commit
b8d66e7642
|
@ -164,7 +164,7 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns an Entity containing the specified Component type.
|
/// Returns an Entity containing the specified Component type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
|
@ -173,7 +173,7 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns all Entities containing the specified Component type.
|
/// Returns all Entities containing the specified Component type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
|
@ -250,7 +250,7 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
protected (Guid, TComponent) ReadComponent<TComponent>() where TComponent : struct, IComponent
|
protected (Guid, TComponent) ReadComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
|
@ -488,7 +488,7 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Overwrites Component struct data associated with the specified Component ID.
|
/// Overwrites Component struct data associated with the specified Component ID.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
|
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
|
@ -496,7 +496,7 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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.
|
/// 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.
|
/// If Component data for this Type already existed on the Entity, the component data is overwritten.
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// Removes the Component with the specified ID from its Entity.
|
/// Removes the Component with the specified ID from its Entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue