From fb0a03e963ac821753210f6cb12861b9eed5ccbf Mon Sep 17 00:00:00 2001 From: "D. Moonfire" Date: Sat, 14 Jan 2023 17:13:01 -0600 Subject: [PATCH] feat(entity): added `SetAll()` which takes a params list - added unit test to verify - minor code clean up to get the tests running closes #1 --- src/MfGames.Gallium/Entity.cs | 64 +++++++++++++++---- tests/MfGames.Gallium.Tests/EntityTests.cs | 37 ++++++++++- .../EnumerableEntityTests.cs | 2 +- .../MfGames.Gallium.Tests/GalliumTestsBase.cs | 2 +- .../MfGames.Gallium.Tests/ITestComponent3.cs | 2 +- tests/MfGames.Gallium.Tests/TestComponent1.cs | 2 +- tests/MfGames.Gallium.Tests/TestComponent2.cs | 2 +- .../MfGames.Gallium.Tests/TestComponent3a.cs | 2 +- .../MfGames.Gallium.Tests/TestComponent3b.cs | 2 +- 9 files changed, 93 insertions(+), 22 deletions(-) diff --git a/src/MfGames.Gallium/Entity.cs b/src/MfGames.Gallium/Entity.cs index 7f60251..045a964 100644 --- a/src/MfGames.Gallium/Entity.cs +++ b/src/MfGames.Gallium/Entity.cs @@ -10,6 +10,17 @@ namespace MfGames.Gallium; /// public record Entity { + public Entity() + : this(Interlocked.Increment(ref nextId)) + { + } + + private Entity(int id) + { + this.Id = id; + this.Components = ImmutableDictionary.Create(); + } + /// public virtual bool Equals(Entity? other) { @@ -29,10 +40,10 @@ public record Entity /// public override int GetHashCode() { - return this.Id; + return this.Id.GetHashCode(); } - private ImmutableDictionary Components { get; set; } + private ImmutableDictionary Components { get; init; } /// /// The internal ID to ensure the entities are unique. Since we are not @@ -42,17 +53,6 @@ public record Entity /// private static int nextId; - public Entity() - : this(Interlocked.Increment(ref nextId)) - { - } - - private Entity(int id) - { - this.Id = id; - this.Components = ImmutableDictionary.Create(); - } - /// /// Gets a value indicating whether the entity has a specific type of /// component registered. @@ -354,7 +354,43 @@ public record Entity return this with { - Components = this.Components.SetItem(typeof(T1), component) + Components = this.Components.SetItem(typeof(T1), component), + }; + } + + /// + /// Sets zero or more components into an entity in a single call. This does + /// not allow for specifying the data type; each item will be added with + /// the result of `component.GetType()`. + /// + /// + /// The components to add to the entity. Any null objects + /// will be ignored. + /// + /// + /// A new Entity with the modified component collection if there is at + /// least one component to set, otherwise the same entity. + /// + public Entity SetAll(params object?[] components) + { + if (components.Length == 0) + { + return this; + } + + ImmutableDictionary collection = this.Components; + + foreach (object? component in components) + { + if (component != null) + { + collection = collection.SetItem(component.GetType(), component); + } + } + + return this with + { + Components = collection, }; } diff --git a/tests/MfGames.Gallium.Tests/EntityTests.cs b/tests/MfGames.Gallium.Tests/EntityTests.cs index 803f5f2..634e805 100644 --- a/tests/MfGames.Gallium.Tests/EntityTests.cs +++ b/tests/MfGames.Gallium.Tests/EntityTests.cs @@ -5,7 +5,7 @@ using MfGames.Gallium; using Xunit; using Xunit.Abstractions; -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class EntityTests : GalliumTestsBase { @@ -151,6 +151,41 @@ public class EntityTests : GalliumTestsBase Assert.Equal(0, entity2.Count); } + [Fact] + public void SetAllSkipsNull() + { + var component1 = new TestComponent1(); + var component2 = new TestComponent2(); + Entity entity1 = new Entity().SetAll(component1, null); + + Assert.Equal(1, entity1.Count); + Assert.True(entity1.Has()); + Assert.False(entity1.Has()); + } + + [Fact] + public void SetAllWithNothingWorks() + { + Entity entity1 = new Entity().SetAll(); + + Assert.Equal(0, entity1.Count); + Assert.False(entity1.Has()); + Assert.False(entity1.Has()); + } + + [Fact] + public void SetAllWorks() + { + var component1 = new TestComponent1(); + var component2 = new TestComponent2(); + Entity entity1 = new Entity().SetAll(component1, component2); + + Assert.Equal(2, entity1.Count); + Assert.True(entity1.Has()); + Assert.True(entity1.Has()); + Assert.False(entity1.Has()); + } + [Fact] public void SettingComponentsWorks() { diff --git a/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs b/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs index 0ff727b..f1bd8c6 100644 --- a/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs +++ b/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs @@ -4,7 +4,7 @@ using MfGames.Gallium; using Xunit; -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class EnumerableEntityTests { diff --git a/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs b/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs index 89cc6cf..f657d05 100644 --- a/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs +++ b/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs @@ -3,7 +3,7 @@ using Serilog.Core; using Xunit.Abstractions; -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; /// /// Common initialization logic for Gallium-based tests including setting diff --git a/tests/MfGames.Gallium.Tests/ITestComponent3.cs b/tests/MfGames.Gallium.Tests/ITestComponent3.cs index ccc4105..e5e60b6 100644 --- a/tests/MfGames.Gallium.Tests/ITestComponent3.cs +++ b/tests/MfGames.Gallium.Tests/ITestComponent3.cs @@ -1,4 +1,4 @@ -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public interface ITestComponent3 { diff --git a/tests/MfGames.Gallium.Tests/TestComponent1.cs b/tests/MfGames.Gallium.Tests/TestComponent1.cs index 8634d8c..ef8aecc 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent1.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent1.cs @@ -1,4 +1,4 @@ -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class TestComponent1 { diff --git a/tests/MfGames.Gallium.Tests/TestComponent2.cs b/tests/MfGames.Gallium.Tests/TestComponent2.cs index 1775dd2..6fd373f 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent2.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent2.cs @@ -1,4 +1,4 @@ -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class TestComponent2 { diff --git a/tests/MfGames.Gallium.Tests/TestComponent3a.cs b/tests/MfGames.Gallium.Tests/TestComponent3a.cs index 29f9a4a..19f5fd2 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent3a.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent3a.cs @@ -1,4 +1,4 @@ -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class TestComponent3a : ITestComponent3 { diff --git a/tests/MfGames.Gallium.Tests/TestComponent3b.cs b/tests/MfGames.Gallium.Tests/TestComponent3b.cs index 9631a65..c6ee6fd 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent3b.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent3b.cs @@ -1,4 +1,4 @@ -namespace Gallium.Tests; +namespace MfGames.Gallium.Tests; public class TestComponent3b : ITestComponent3 {