From 7d070d68c9e9bb535a9c4407f1f2d9ce73d15ac9 Mon Sep 17 00:00:00 2001 From: BeanCheeseBurrito Date: Fri, 15 Nov 2024 01:05:46 -0800 Subject: [PATCH] Add ObserverBuilder.ObserverFlags() --- src/Flecs.NET.Tests/Cpp/ObserverTests.cs | 99 ++++++++++++++++++++++++ src/Flecs.NET/Core/ObserverBuilder.cs | 11 +++ 2 files changed, 110 insertions(+) diff --git a/src/Flecs.NET.Tests/Cpp/ObserverTests.cs b/src/Flecs.NET.Tests/Cpp/ObserverTests.cs index 75d78d2e..a115a988 100644 --- a/src/Flecs.NET.Tests/Cpp/ObserverTests.cs +++ b/src/Flecs.NET.Tests/Cpp/ObserverTests.cs @@ -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(); + Entity e2 = world.Entity().Add(); + Entity e3 = world.Entity().Add().Add(); + + int count = 0; + + Observer o = world.Observer() + .With() + .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(); + Entity e2 = world.Entity().Add(); + Entity e3 = world.Entity().Add().Add(); + + int count = 0; + + Observer o = world.Observer() + .With() + .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(); + Entity e2 = world.Entity().Add(); + Entity e3 = world.Entity().Add().Add(); + + int count = 0; + + Observer o = world.Observer() + .With() + .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() { diff --git a/src/Flecs.NET/Core/ObserverBuilder.cs b/src/Flecs.NET/Core/ObserverBuilder.cs index b30ad94a..fa993795 100644 --- a/src/Flecs.NET/Core/ObserverBuilder.cs +++ b/src/Flecs.NET/Core/ObserverBuilder.cs @@ -105,6 +105,17 @@ public ref ObserverBuilder YieldExisting(bool value = true) return ref this; } + /// + /// Set observer flags. + /// + /// The flags value. + /// + public ref ObserverBuilder ObserverFlags(uint flags) + { + Desc.flags_ |= flags; + return ref this; + } + /// /// Set observer context. ///