Skip to content

Commit

Permalink
Add ObserverBuilder.ObserverFlags()
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Nov 15, 2024
1 parent eafb152 commit d1c3377
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/Flecs.NET.Tests/Cpp/ObserverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,105 @@ private void YieldExisting2Terms()
Assert.Equal(6, count);
}

[Fact]
private void YieldExistingOnCreateFlag()
{
using World world = World.Create();

Entity e1 = world.Entity().Add<Tag0>();
Entity e2 = world.Entity().Add<Tag0>();
Entity e3 = world.Entity().Add<Tag0>().Add<Tag1>();

int count = 0;

Observer o = world.Observer()
.With<Tag0>()
.Event(Ecs.OnAdd)
.Event(Ecs.OnRemove)
.ObserverFlags(EcsObserverYieldOnCreate)
.Each((Entity e) =>
{
if (e == e1)
count++;
if (e == e2)
count += 2;
if (e == e3)
count += 3;
});

Assert.Equal(6, count);

o.Destruct();

Assert.Equal(6, count);
}

[Fact]
private void YieldExistingOnDeleteFlag()
{
using World world = World.Create();

Entity e1 = world.Entity().Add<Tag0>();
Entity e2 = world.Entity().Add<Tag0>();
Entity e3 = world.Entity().Add<Tag0>().Add<Tag1>();

int count = 0;

Observer o = world.Observer()
.With<Tag0>()
.Event(Ecs.OnAdd)
.Event(Ecs.OnRemove)
.ObserverFlags(EcsObserverYieldOnDelete)
.Each((Entity e) =>
{
if (e == e1)
count++;
if (e == e2)
count += 2;
if (e == e3)
count += 3;
});

Assert.Equal(0, count);

o.Destruct();

Assert.Equal(6, count);
}

[Fact]
private void YieldExistingOnCreateDeleteFlag()
{
using World world = World.Create();

Entity e1 = world.Entity().Add<Tag0>();
Entity e2 = world.Entity().Add<Tag0>();
Entity e3 = world.Entity().Add<Tag0>().Add<Tag1>();

int count = 0;

Observer o = world.Observer()
.With<Tag0>()
.Event(Ecs.OnAdd)
.Event(Ecs.OnRemove)
.ObserverFlags(EcsObserverYieldOnCreate | EcsObserverYieldOnDelete)
.Each((Entity e) =>
{
if (e == e1)
count++;
if (e == e2)
count += 2;
if (e == e3)
count += 3;
});

Assert.Equal(6, count);

o.Destruct();

Assert.Equal(12, count);
}

[Fact]
private void DefaultCtor()
{
Expand Down
11 changes: 11 additions & 0 deletions src/Flecs.NET/Core/ObserverBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ public ref ObserverBuilder YieldExisting(bool value = true)
return ref this;
}

/// <summary>
/// Set observer flags.
/// </summary>
/// <param name="flags">The flags value.</param>
/// <returns></returns>
public ref ObserverBuilder ObserverFlags(uint flags)
{
Desc.flags_ |= flags;
return ref this;
}

/// <summary>
/// Set observer context.
/// </summary>
Expand Down

0 comments on commit d1c3377

Please sign in to comment.