fix cast bug in Entity.GetComponents

pull/5/head
Evan Hemsley 2019-06-20 12:38:19 -07:00
parent 74ec2f6320
commit 987b30ceae
2 changed files with 10 additions and 5 deletions

View File

@ -107,9 +107,9 @@ namespace Encompass
internal IEnumerable<KeyValuePair<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal IEnumerable<KeyValuePair<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
var entity_components = GetComponentsByEntity(entityID).Select((kv) => new KeyValuePair<Guid, TComponent>(kv.Key, (TComponent)kv.Value)); var entityComponentsByType = GetComponentsByEntity(entityID).Where((pair) => componentIDToType[pair.Key] == typeof(TComponent)).Select((pair) => new KeyValuePair<Guid, TComponent>(pair.Key, (TComponent)pair.Value));
var active_components_by_type = GetActiveComponentsByType<TComponent>(); var activeComponentsByType = GetActiveComponentsByType<TComponent>();
return entity_components.Intersect(active_components_by_type); return activeComponentsByType.Intersect(entityComponentsByType);
} }
internal IEnumerable<KeyValuePair<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type) internal IEnumerable<KeyValuePair<Guid, IComponent>> GetComponentsByEntityAndType(Guid entityID, Type type)

View File

@ -1,7 +1,9 @@
using System; using System;
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions;
using Encompass; using Encompass;
using System.Collections.Generic;
namespace Tests namespace Tests
{ {
@ -93,11 +95,13 @@ namespace Tests
} }
static bool calledOnDraw = false; static bool calledOnDraw = false;
static IEnumerable<KeyValuePair<Guid, TestDrawComponent>> resultComponents;
[Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))] [Renders(typeof(TestDrawComponent), typeof(AComponent), typeof(CComponent))]
class CalledRenderer : EntityRenderer class CalledRenderer : EntityRenderer
{ {
public override void Render(Entity entity) public override void Render(Entity entity)
{ {
resultComponents = entity.GetComponents<TestDrawComponent>();
calledOnDraw = true; calledOnDraw = true;
} }
} }
@ -110,12 +114,12 @@ namespace Tests
AComponent aComponent; AComponent aComponent;
CComponent cComponent; CComponent cComponent;
TestDrawComponent testDrawComponent = default(TestDrawComponent); TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
entity.AddComponent(aComponent); entity.AddComponent(aComponent);
entity.AddComponent(cComponent); entity.AddComponent(cComponent);
entity.AddDrawComponent(testDrawComponent, 2); var testDrawComponentID = entity.AddDrawComponent(testDrawComponent, 2);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -124,6 +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));
} }
} }
} }