Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing component Add event, and raise Remove event when destroying entities. #196

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/Arch.Tests/EventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Arch.Tests;

/// <summary>
/// The <see cref="EventTest"/> class
/// adds several methods for checking if events are fired correctly upon entity modifcation.
/// adds several methods for checking if events are fired correctly upon entity modification.
/// </summary>
[TestFixture]
public sealed class EventTest
Expand Down Expand Up @@ -67,6 +67,30 @@ public void AddSingle()
_asserter.Clear();
}

[Test]
public void AddSingleFromArchetype()
{
using var world = World.Create();
world.SubscribeComponentAdded((in Entity entity, ref EventTestComponentOne _) => _asserter.CompOneAdded.Add(entity));
world.SubscribeComponentAdded((in Entity entity, ref EventTestComponentTwo _) => _asserter.CompTwoAdded.Add(entity));

Span<ComponentType> archetype = stackalloc ComponentType[] { typeof(EventTestComponentOne) };

// Create entity to check if created and add event were fired
var entity = world.Create(archetype);

_asserter.AssertEvents(compOneAdded: 1);
That(_asserter.CompOneAdded, Does.Contain(entity));
_asserter.Clear();

// Add another component to see if add was fired
world.Add<EventTestComponentTwo>(entity);

_asserter.AssertEvents(compTwoAdded: 1);
That(_asserter.CompTwoAdded, Does.Contain(entity));
_asserter.Clear();
}

[Test]
public void AddSingleObject()
{
Expand Down Expand Up @@ -226,6 +250,62 @@ public void RemoveMultiple()
_asserter.Clear();
}

[Test]
public void DestroyDoesRaiseComponentRemove()
{
using var world = World.Create();
world.SubscribeComponentRemoved((in Entity entity,ref EventTestComponentOne _) => _asserter.CompOneRemoved.Add(entity));
world.SubscribeComponentRemoved((in Entity entity, ref EventTestComponentTwo _) => _asserter.CompTwoRemoved.Add(entity));

var entity = world.Create<EventTestComponentOne, EventTestComponentTwo>();
world.Destroy(entity);

_asserter.AssertEvents(compOneRemoved: 1, compTwoRemoved:1);
That(_asserter.CompOneRemoved, Does.Contain(entity));
That(_asserter.CompTwoRemoved, Does.Contain(entity));
_asserter.Clear();
}

[Test]
public void DestroyUsingQueryDoesRaiseComponentRemove()
{
using var world = World.Create();
world.SubscribeComponentRemoved((in Entity entity,ref EventTestComponentOne _) => _asserter.CompOneRemoved.Add(entity));
world.SubscribeComponentRemoved((in Entity entity, ref EventTestComponentTwo _) => _asserter.CompTwoRemoved.Add(entity));

var entity = world.Create<EventTestComponentOne, EventTestComponentTwo>();

world.Destroy(new QueryDescription().WithExclusive<EventTestComponentOne, EventTestComponentTwo>());

_asserter.AssertEvents(compOneRemoved: 1, compTwoRemoved:1);
That(_asserter.CompOneRemoved, Does.Contain(entity));
That(_asserter.CompTwoRemoved, Does.Contain(entity));
_asserter.Clear();
}

[Test]
public void RemoveCompThenDestroyDoesNotRaiseCompRemoveTwice()
{
using var world = World.Create();
world.SubscribeComponentRemoved((in Entity entity,ref EventTestComponentOne _) => _asserter.CompOneRemoved.Add(entity));
world.SubscribeComponentRemoved((in Entity entity, ref EventTestComponentTwo _) => _asserter.CompTwoRemoved.Add(entity));

var entity = world.Create<EventTestComponentOne, EventTestComponentTwo>();
world.Remove<EventTestComponentOne, EventTestComponentTwo>(entity);

_asserter.AssertEvents(compOneRemoved: 1, compTwoRemoved: 1);
That(_asserter.CompOneRemoved, Does.Contain(entity));
That(_asserter.CompTwoRemoved, Does.Contain(entity));
_asserter.Clear();

world.Destroy(entity);

_asserter.AssertEvents(compOneRemoved: 0, compTwoRemoved: 0);
That(_asserter.CompOneRemoved, Does.Not.Contain(entity));
That(_asserter.CompTwoRemoved, Does.Not.Contain(entity));
_asserter.Clear();
}

private class EventAsserter
{
public readonly List<Entity> Created = new();
Expand Down
28 changes: 28 additions & 0 deletions src/Arch/Core/World.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.Contracts;
using System.Threading;
using Arch.Core.Extensions;
using Arch.Core.Extensions.Internal;
using Arch.Core.Utils;
using Collections.Pooled;
Expand Down Expand Up @@ -297,6 +298,14 @@ public Entity Create(Span<ComponentType> types)
EntityInfo.Add(entity.Id, recycled.Version, archetype, slot);
Size++;
OnEntityCreated(entity);

#if EVENTS
foreach (ref var type in types)
{
OnComponentAdded(entity, type);
}
#endif

return entity;
}

Expand Down Expand Up @@ -343,6 +352,15 @@ internal void Move(Entity entity, Archetype source, Archetype destination, out S
[StructuralChange]
public void Destroy(Entity entity)
{
#if EVENTS
// Raise the OnComponentRemoved event for each component on the entity.
var arch = GetArchetype(entity);
foreach (var compType in arch.Types)
{
OnComponentRemoved(entity, compType);
}
#endif

OnEntityDestroyed(entity);

// Remove from archetype
Expand Down Expand Up @@ -774,6 +792,16 @@ public void Destroy(in QueryDescription queryDescription)
foreach (var index in chunk)
{
var entity = Unsafe.Add(ref entityFirstElement, index);

#if EVENTS
// Raise the OnComponentRemoved event for each component on the entity.
var arch = GetArchetype(entity);
foreach (var compType in arch.Types)
{
OnComponentRemoved(entity, compType);
}
#endif

OnEntityDestroyed(entity);

var version = EntityInfo.GetVersion(entity.Id);
Expand Down
Loading