From 87c5a785c7a5800c017908ab4f0685ea63cb19b8 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Thu, 27 Jun 2019 16:55:12 -0700 Subject: [PATCH] changing KeyValueTuple component returns to ValueTuple --- encompass-cs/ComponentManager.cs | 25 ++++++++++---------- encompass-cs/Engine.cs | 4 ++-- encompass-cs/Entity.cs | 4 ++-- encompass-cs/Renderer.cs | 4 ++-- test/EngineTest.cs | 40 +++++++++++++++----------------- test/EntityRendererTest.cs | 4 ++-- test/EntityTest.cs | 10 ++++---- test/GeneralRendererTest.cs | 6 ++--- 8 files changed, 47 insertions(+), 50 deletions(-) diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 8ba9fa6..2e386ee 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -81,38 +81,38 @@ namespace Encompass Enumerable.Empty(); } - internal IEnumerable> GetComponentsByEntity(Guid entityID) + internal IEnumerable> GetComponentsByEntity(Guid entityID) { - return GetComponentIDsByEntityID(entityID).Intersect(activeComponents).Select((id) => new KeyValuePair(id, IDToComponent[id])); + return GetComponentIDsByEntityID(entityID).Intersect(activeComponents).Select((id) => new ValueTuple(id, IDToComponent[id])); } - internal IEnumerable> GetActiveComponentsByType() where TComponent : struct, IComponent + internal IEnumerable> GetActiveComponentsByType() where TComponent : struct, IComponent { return typeToComponentIDs.ContainsKey(typeof(TComponent)) ? - typeToComponentIDs[typeof(TComponent)].Select((id) => new KeyValuePair(id, (TComponent)IDToComponent[id])) : - Enumerable.Empty>(); + typeToComponentIDs[typeof(TComponent)].Select((id) => new ValueTuple(id, (TComponent)IDToComponent[id])) : + Enumerable.Empty>(); } - internal IEnumerable> GetActiveComponentsByType(Type type) + internal IEnumerable> GetActiveComponentsByType(Type type) { return typeToComponentIDs.ContainsKey(type) ? - typeToComponentIDs[type].Select((id) => new KeyValuePair(id, IDToComponent[id])) : - Enumerable.Empty>(); + typeToComponentIDs[type].Select((id) => new ValueTuple(id, IDToComponent[id])) : + Enumerable.Empty>(); } - internal KeyValuePair GetActiveComponentByType() where TComponent : struct, IComponent + internal ValueTuple GetActiveComponentByType() where TComponent : struct, IComponent { return GetActiveComponentsByType().Single(); } - internal IEnumerable> GetComponentsByEntityAndType(Guid entityID) where TComponent : struct, IComponent + internal IEnumerable> GetComponentsByEntityAndType(Guid entityID) where TComponent : struct, IComponent { - var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Key] == typeof(TComponent)).Select((pair) => new KeyValuePair(pair.Key, (TComponent)pair.Value)); + var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Item1] == typeof(TComponent)).Select((pair) => new ValueTuple(pair.Item1, (TComponent)pair.Item2)); var activeComponentsByType = GetActiveComponentsByType(); return activeComponentsByType.Intersect(entityComponentsByType); } - internal IEnumerable> GetComponentsByEntityAndType(Guid entityID, Type type) + internal IEnumerable> GetComponentsByEntityAndType(Guid entityID, Type type) { var entityComponents = GetComponentsByEntity(entityID); var activeComponentsByType = GetActiveComponentsByType(type); @@ -151,7 +151,6 @@ namespace Encompass internal void UpdateComponent(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent { - var entityID = GetEntityIDByComponentID(componentID); IDToComponent[componentID] = newComponentValue; } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 80c9b30..efd6e0d 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -86,12 +86,12 @@ namespace Encompass } } - protected IEnumerable> ReadComponents() where TComponent : struct, IComponent + protected IEnumerable> ReadComponents() where TComponent : struct, IComponent { return componentManager.GetActiveComponentsByType(); } - protected KeyValuePair ReadComponent() where TComponent : struct, IComponent + protected ValueTuple ReadComponent() where TComponent : struct, IComponent { return componentManager.GetActiveComponentByType(); } diff --git a/encompass-cs/Entity.cs b/encompass-cs/Entity.cs index c39ce78..20b3101 100644 --- a/encompass-cs/Entity.cs +++ b/encompass-cs/Entity.cs @@ -26,12 +26,12 @@ namespace Encompass return componentManager.AddDrawComponent(id, component, layer); } - public IEnumerable> GetComponents() where TComponent : struct, IComponent + public IEnumerable> GetComponents() where TComponent : struct, IComponent { return componentManager.GetComponentsByEntityAndType(id); } - public KeyValuePair GetComponent() where TComponent : struct, IComponent + public ValueTuple GetComponent() where TComponent : struct, IComponent { return GetComponents().First(); } diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index 1cf3351..9f4c860 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -24,12 +24,12 @@ namespace Encompass return entityManager.GetEntity(entityID); } - protected IEnumerable> ReadComponents() where TComponent : struct, IComponent + protected IEnumerable> ReadComponents() where TComponent : struct, IComponent { return componentManager.GetActiveComponentsByType(); } - protected KeyValuePair ReadComponent() where TComponent : struct, IComponent + protected ValueTuple ReadComponent() where TComponent : struct, IComponent { return componentManager.GetActiveComponentByType(); } diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 50f2b22..88b58ec 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -12,7 +12,7 @@ namespace Tests { public class EngineTest { - static List> resultComponents; + static List> resultComponents; static MockComponent resultComponent; static List resultMessages; @@ -21,7 +21,7 @@ namespace Tests { public override void Update(double dt) { - resultComponents = this.ReadComponents().ToList(); + resultComponents = ReadComponents().ToList(); } } @@ -29,7 +29,7 @@ namespace Tests { public override void Update(double dt) { - resultComponent = this.ReadComponent().Value; + resultComponent = ReadComponent().Item2; } } @@ -56,7 +56,7 @@ namespace Tests world.Update(0.01f); - var resultComponentValues = resultComponents.Select((kv) => kv.Value); + var resultComponentValues = resultComponents.Select((kv) => kv.Item2); resultComponentValues.Should().Contain(mockComponent); resultComponentValues.Should().Contain(mockComponentB); } @@ -111,13 +111,13 @@ namespace Tests { public override void Update(double dt) { - (var componentID, var component) = this.ReadComponent(); + (var componentID, var component) = ReadComponent(); component.myInt = 420; component.myString = "blaze it"; - this.UpdateComponent(componentID, component); + UpdateComponent(componentID, component); - resultComponent = this.ReadComponent().Value; + resultComponent = ReadComponent().Item2; } } @@ -151,9 +151,9 @@ namespace Tests component.myInt = 420; component.myString = "blaze it"; - this.UpdateComponent(componentID, component); + UpdateComponent(componentID, component); - component = this.ReadComponent().Value; + component = ReadComponent().Item2; } } @@ -320,8 +320,8 @@ namespace Tests Assert.Throws(() => world.Update(0.01f)); } - static KeyValuePair pairA; - static KeyValuePair pairB; + static ValueTuple pairA; + static ValueTuple pairB; class SameValueComponentReadEngine : Engine { @@ -357,10 +357,10 @@ namespace Tests world.Update(0.01f); Assert.That(pairA, Is.Not.EqualTo(pairB)); - Assert.That(pairA.Value, Is.EqualTo(pairB.Value)); + Assert.That(pairA.Item2, Is.EqualTo(pairB.Item2)); } - static IEnumerable> emptyComponentReadResult; + static IEnumerable> emptyComponentReadResult; class ReadEmptyMockComponentsEngine : Engine { @@ -388,18 +388,16 @@ namespace Tests { public override void Update(double dt) { - var componentPairs = ReadComponents(); - - foreach (var componentPair in componentPairs) + foreach (var componentPair in ReadComponents()) { - var componentID = componentPair.Key; + var componentID = componentPair.Item1; var entityID = GetEntityIDByComponentID(componentID); Destroy(entityID); } } } - static IEnumerable> results; + static IEnumerable> results; class ReaderEngine : Engine { public override void Update(double dt) @@ -442,7 +440,7 @@ namespace Tests { public override void Update(double dt) { - var componentID = ReadComponent().Key; + var componentID = ReadComponent().Item1; entityFromComponentIDResult = GetEntityByComponentID(componentID); } } @@ -471,7 +469,7 @@ namespace Tests { public override void Update(double dt) { - var componentID = ReadComponent().Key; + var componentID = ReadComponent().Item1; mockComponentByIDResult = GetComponentByID(componentID); } } @@ -500,7 +498,7 @@ namespace Tests { public override void Update(double dt) { - var componentID = ReadComponent().Key; + var componentID = ReadComponent().Item1; GetComponentByID(componentID); } } diff --git a/test/EntityRendererTest.cs b/test/EntityRendererTest.cs index 009528f..75367cf 100644 --- a/test/EntityRendererTest.cs +++ b/test/EntityRendererTest.cs @@ -95,7 +95,7 @@ namespace Tests } static bool calledOnDraw = false; - static IEnumerable> resultComponents; + static IEnumerable> resultComponents; [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))] class CalledRenderer : EntityRenderer { @@ -128,7 +128,7 @@ namespace Tests Assert.IsTrue(renderer.IsTracking(entity.id)); Assert.IsTrue(calledOnDraw); - resultComponents.Should().Contain(new KeyValuePair(testDrawComponentID, testDrawComponent)); + resultComponents.Should().Contain(new ValueTuple(testDrawComponentID, testDrawComponent)); } } } diff --git a/test/EntityTest.cs b/test/EntityTest.cs index 445bc51..bbd4d9e 100644 --- a/test/EntityTest.cs +++ b/test/EntityTest.cs @@ -30,7 +30,7 @@ namespace Tests var world = worldBuilder.Build(); Assert.IsTrue(entity.HasComponent()); - Assert.That(entity.GetComponent().Value, Is.EqualTo(mockComponent)); + Assert.That(entity.GetComponent().Item2, Is.EqualTo(mockComponent)); } [Test] @@ -58,9 +58,9 @@ namespace Tests var world = worldBuilder.Build(); var components = entity.GetComponents(); - components.Should().Contain(new KeyValuePair(componentAID, mockComponentA)); - components.Should().Contain(new KeyValuePair(componentBID, mockComponentB)); - components.Should().Contain(new KeyValuePair(componentCID, mockComponentC)); + components.Should().Contain(new ValueTuple(componentAID, mockComponentA)); + components.Should().Contain(new ValueTuple(componentBID, mockComponentB)); + components.Should().Contain(new ValueTuple(componentCID, mockComponentC)); } [Test] @@ -77,7 +77,7 @@ namespace Tests var world = worldBuilder.Build(); - Assert.AreEqual(new KeyValuePair(componentID, mockComponent), entity.GetComponent()); + Assert.AreEqual(new ValueTuple(componentID, mockComponent), entity.GetComponent()); } [Test] diff --git a/test/GeneralRendererTest.cs b/test/GeneralRendererTest.cs index 3319043..fd0e099 100644 --- a/test/GeneralRendererTest.cs +++ b/test/GeneralRendererTest.cs @@ -7,13 +7,13 @@ using Encompass; namespace Tests { - public class GeneralRendererTest + public static class GeneralRendererTest { struct AComponent : IComponent { } public class SingletonRead { - static KeyValuePair result; + static ValueTuple result; class TestRenderer : GeneralRenderer { @@ -39,7 +39,7 @@ namespace Tests world.Update(0.01f); world.Draw(); - Assert.That(result, Is.EqualTo(new KeyValuePair(componentID, aComponent))); + Assert.That(result, Is.EqualTo(new ValueTuple(componentID, aComponent))); } [Test]