changing KeyValueTuple component returns to ValueTuple

pull/5/head
Evan Hemsley 2019-06-27 16:55:12 -07:00
parent 51c014eb1e
commit 87c5a785c7
8 changed files with 47 additions and 50 deletions

View File

@ -81,38 +81,38 @@ namespace Encompass
Enumerable.Empty<Guid>(); Enumerable.Empty<Guid>();
} }
internal IEnumerable<KeyValuePair<Guid, IComponent>> GetComponentsByEntity(Guid entityID) internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntity(Guid entityID)
{ {
return GetComponentIDsByEntityID(entityID).Intersect(activeComponents).Select((id) => new KeyValuePair<Guid, IComponent>(id, IDToComponent[id])); return GetComponentIDsByEntityID(entityID).Intersect(activeComponents).Select((id) => new ValueTuple<Guid, IComponent>(id, IDToComponent[id]));
} }
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetActiveComponentsByType<TComponent>() where TComponent : struct, IComponent internal IEnumerable<ValueTuple<Guid, TComponent>> GetActiveComponentsByType<TComponent>() where TComponent : struct, IComponent
{ {
return typeToComponentIDs.ContainsKey(typeof(TComponent)) ? return typeToComponentIDs.ContainsKey(typeof(TComponent)) ?
typeToComponentIDs[typeof(TComponent)].Select((id) => new KeyValuePair<Guid, TComponent>(id, (TComponent)IDToComponent[id])) : typeToComponentIDs[typeof(TComponent)].Select((id) => new ValueTuple<Guid, TComponent>(id, (TComponent)IDToComponent[id])) :
Enumerable.Empty<KeyValuePair<Guid, TComponent>>(); Enumerable.Empty<ValueTuple<Guid, TComponent>>();
} }
internal IEnumerable<KeyValuePair<Guid, IComponent>> GetActiveComponentsByType(Type type) internal IEnumerable<ValueTuple<Guid, IComponent>> GetActiveComponentsByType(Type type)
{ {
return typeToComponentIDs.ContainsKey(type) ? return typeToComponentIDs.ContainsKey(type) ?
typeToComponentIDs[type].Select((id) => new KeyValuePair<Guid, IComponent>(id, IDToComponent[id])) : typeToComponentIDs[type].Select((id) => new ValueTuple<Guid, IComponent>(id, IDToComponent[id])) :
Enumerable.Empty<KeyValuePair<Guid, IComponent>>(); Enumerable.Empty<ValueTuple<Guid, IComponent>>();
} }
internal KeyValuePair<Guid, TComponent> GetActiveComponentByType<TComponent>() where TComponent : struct, IComponent internal ValueTuple<Guid, TComponent> GetActiveComponentByType<TComponent>() where TComponent : struct, IComponent
{ {
return GetActiveComponentsByType<TComponent>().Single(); return GetActiveComponentsByType<TComponent>().Single();
} }
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Key] == typeof(TComponent)).Select((pair) => new KeyValuePair<Guid, TComponent>(pair.Key, (TComponent)pair.Value)); var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Item1] == typeof(TComponent)).Select((pair) => new ValueTuple<Guid, TComponent>(pair.Item1, (TComponent)pair.Item2));
var activeComponentsByType = GetActiveComponentsByType<TComponent>(); var activeComponentsByType = GetActiveComponentsByType<TComponent>();
return activeComponentsByType.Intersect(entityComponentsByType); return activeComponentsByType.Intersect(entityComponentsByType);
} }
internal IEnumerable<KeyValuePair<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type) internal IEnumerable<ValueTuple<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type)
{ {
var entityComponents = GetComponentsByEntity(entityID); var entityComponents = GetComponentsByEntity(entityID);
var activeComponentsByType = GetActiveComponentsByType(type); var activeComponentsByType = GetActiveComponentsByType(type);
@ -151,7 +151,6 @@ namespace Encompass
internal void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent internal void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
{ {
var entityID = GetEntityIDByComponentID(componentID);
IDToComponent[componentID] = newComponentValue; IDToComponent[componentID] = newComponentValue;
} }

View File

@ -86,12 +86,12 @@ namespace Encompass
} }
} }
protected IEnumerable<KeyValuePair<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetActiveComponentsByType<TComponent>(); return componentManager.GetActiveComponentsByType<TComponent>();
} }
protected KeyValuePair<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetActiveComponentByType<TComponent>(); return componentManager.GetActiveComponentByType<TComponent>();
} }

View File

@ -26,12 +26,12 @@ namespace Encompass
return componentManager.AddDrawComponent(id, component, layer); return componentManager.AddDrawComponent(id, component, layer);
} }
public IEnumerable<KeyValuePair<Guid, TComponent>> GetComponents<TComponent>() where TComponent : struct, IComponent public IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetComponentsByEntityAndType<TComponent>(id); return componentManager.GetComponentsByEntityAndType<TComponent>(id);
} }
public KeyValuePair<Guid, TComponent> GetComponent<TComponent>() where TComponent : struct, IComponent public ValueTuple<Guid, TComponent> GetComponent<TComponent>() where TComponent : struct, IComponent
{ {
return GetComponents<TComponent>().First(); return GetComponents<TComponent>().First();
} }

View File

@ -24,12 +24,12 @@ namespace Encompass
return entityManager.GetEntity(entityID); return entityManager.GetEntity(entityID);
} }
protected IEnumerable<KeyValuePair<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetActiveComponentsByType<TComponent>(); return componentManager.GetActiveComponentsByType<TComponent>();
} }
protected KeyValuePair<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetActiveComponentByType<TComponent>(); return componentManager.GetActiveComponentByType<TComponent>();
} }

View File

@ -12,7 +12,7 @@ namespace Tests
{ {
public class EngineTest public class EngineTest
{ {
static List<KeyValuePair<Guid, MockComponent>> resultComponents; static List<ValueTuple<Guid, MockComponent>> resultComponents;
static MockComponent resultComponent; static MockComponent resultComponent;
static List<MockMessage> resultMessages; static List<MockMessage> resultMessages;
@ -21,7 +21,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultComponents = this.ReadComponents<MockComponent>().ToList(); resultComponents = ReadComponents<MockComponent>().ToList();
} }
} }
@ -29,7 +29,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultComponent = this.ReadComponent<MockComponent>().Value; resultComponent = ReadComponent<MockComponent>().Item2;
} }
} }
@ -56,7 +56,7 @@ namespace Tests
world.Update(0.01f); 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(mockComponent);
resultComponentValues.Should().Contain(mockComponentB); resultComponentValues.Should().Contain(mockComponentB);
} }
@ -111,13 +111,13 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
(var componentID, var component) = this.ReadComponent<MockComponent>(); (var componentID, var component) = ReadComponent<MockComponent>();
component.myInt = 420; component.myInt = 420;
component.myString = "blaze it"; component.myString = "blaze it";
this.UpdateComponent(componentID, component); UpdateComponent(componentID, component);
resultComponent = this.ReadComponent<MockComponent>().Value; resultComponent = ReadComponent<MockComponent>().Item2;
} }
} }
@ -151,9 +151,9 @@ namespace Tests
component.myInt = 420; component.myInt = 420;
component.myString = "blaze it"; component.myString = "blaze it";
this.UpdateComponent(componentID, component); UpdateComponent(componentID, component);
component = this.ReadComponent<MockComponent>().Value; component = ReadComponent<MockComponent>().Item2;
} }
} }
@ -320,8 +320,8 @@ namespace Tests
Assert.Throws<IllegalMessageReadException>(() => world.Update(0.01f)); Assert.Throws<IllegalMessageReadException>(() => world.Update(0.01f));
} }
static KeyValuePair<Guid, MockComponent> pairA; static ValueTuple<Guid, MockComponent> pairA;
static KeyValuePair<Guid, MockComponent> pairB; static ValueTuple<Guid, MockComponent> pairB;
class SameValueComponentReadEngine : Engine class SameValueComponentReadEngine : Engine
{ {
@ -357,10 +357,10 @@ namespace Tests
world.Update(0.01f); world.Update(0.01f);
Assert.That(pairA, Is.Not.EqualTo(pairB)); 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<KeyValuePair<Guid, MockComponent>> emptyComponentReadResult; static IEnumerable<ValueTuple<Guid, MockComponent>> emptyComponentReadResult;
class ReadEmptyMockComponentsEngine : Engine class ReadEmptyMockComponentsEngine : Engine
{ {
@ -388,18 +388,16 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var componentPairs = ReadComponents<DestroyerComponent>(); foreach (var componentPair in ReadComponents<DestroyerComponent>())
foreach (var componentPair in componentPairs)
{ {
var componentID = componentPair.Key; var componentID = componentPair.Item1;
var entityID = GetEntityIDByComponentID(componentID); var entityID = GetEntityIDByComponentID(componentID);
Destroy(entityID); Destroy(entityID);
} }
} }
} }
static IEnumerable<KeyValuePair<Guid, MockComponent>> results; static IEnumerable<ValueTuple<Guid, MockComponent>> results;
class ReaderEngine : Engine class ReaderEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
@ -442,7 +440,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var componentID = ReadComponent<MockComponent>().Key; var componentID = ReadComponent<MockComponent>().Item1;
entityFromComponentIDResult = GetEntityByComponentID(componentID); entityFromComponentIDResult = GetEntityByComponentID(componentID);
} }
} }
@ -471,7 +469,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var componentID = ReadComponent<MockComponent>().Key; var componentID = ReadComponent<MockComponent>().Item1;
mockComponentByIDResult = GetComponentByID<MockComponent>(componentID); mockComponentByIDResult = GetComponentByID<MockComponent>(componentID);
} }
} }
@ -500,7 +498,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var componentID = ReadComponent<MockComponent>().Key; var componentID = ReadComponent<MockComponent>().Item1;
GetComponentByID<OtherComponent>(componentID); GetComponentByID<OtherComponent>(componentID);
} }
} }

View File

@ -95,7 +95,7 @@ namespace Tests
} }
static bool calledOnDraw = false; static bool calledOnDraw = false;
static IEnumerable<KeyValuePair<Guid, TestDrawComponent>> resultComponents; static IEnumerable<ValueTuple<Guid, TestDrawComponent>> resultComponents;
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))] [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))]
class CalledRenderer : EntityRenderer class CalledRenderer : EntityRenderer
{ {
@ -128,7 +128,7 @@ namespace Tests
Assert.IsTrue(renderer.IsTracking(entity.id)); Assert.IsTrue(renderer.IsTracking(entity.id));
Assert.IsTrue(calledOnDraw); Assert.IsTrue(calledOnDraw);
resultComponents.Should().Contain(new KeyValuePair<Guid, TestDrawComponent>(testDrawComponentID, testDrawComponent)); resultComponents.Should().Contain(new ValueTuple<Guid, TestDrawComponent>(testDrawComponentID, testDrawComponent));
} }
} }
} }

View File

@ -30,7 +30,7 @@ namespace Tests
var world = worldBuilder.Build(); var world = worldBuilder.Build();
Assert.IsTrue(entity.HasComponent<MockComponent>()); Assert.IsTrue(entity.HasComponent<MockComponent>());
Assert.That(entity.GetComponent<MockComponent>().Value, Is.EqualTo(mockComponent)); Assert.That(entity.GetComponent<MockComponent>().Item2, Is.EqualTo(mockComponent));
} }
[Test] [Test]
@ -58,9 +58,9 @@ namespace Tests
var world = worldBuilder.Build(); var world = worldBuilder.Build();
var components = entity.GetComponents<MockComponent>(); var components = entity.GetComponents<MockComponent>();
components.Should().Contain(new KeyValuePair<Guid, MockComponent>(componentAID, mockComponentA)); components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentAID, mockComponentA));
components.Should().Contain(new KeyValuePair<Guid, MockComponent>(componentBID, mockComponentB)); components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentBID, mockComponentB));
components.Should().Contain(new KeyValuePair<Guid, MockComponent>(componentCID, mockComponentC)); components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentCID, mockComponentC));
} }
[Test] [Test]
@ -77,7 +77,7 @@ namespace Tests
var world = worldBuilder.Build(); var world = worldBuilder.Build();
Assert.AreEqual(new KeyValuePair<Guid, MockComponent>(componentID, mockComponent), entity.GetComponent<MockComponent>()); Assert.AreEqual(new ValueTuple<Guid, MockComponent>(componentID, mockComponent), entity.GetComponent<MockComponent>());
} }
[Test] [Test]

View File

@ -7,13 +7,13 @@ using Encompass;
namespace Tests namespace Tests
{ {
public class GeneralRendererTest public static class GeneralRendererTest
{ {
struct AComponent : IComponent { } struct AComponent : IComponent { }
public class SingletonRead public class SingletonRead
{ {
static KeyValuePair<Guid, AComponent> result; static ValueTuple<Guid, AComponent> result;
class TestRenderer : GeneralRenderer class TestRenderer : GeneralRenderer
{ {
@ -39,7 +39,7 @@ namespace Tests
world.Update(0.01f); world.Update(0.01f);
world.Draw(); world.Draw();
Assert.That(result, Is.EqualTo(new KeyValuePair<Guid, AComponent>(componentID, aComponent))); Assert.That(result, Is.EqualTo(new ValueTuple<Guid, AComponent>(componentID, aComponent)));
} }
[Test] [Test]