better error message when component is not found on entity

pull/5/head
Evan Hemsley 2019-12-28 15:08:07 -08:00
parent 9b0bf34c73
commit 936b97c4ec
5 changed files with 83 additions and 37 deletions

View File

@ -22,6 +22,7 @@ namespace Encompass
public TComponent Get(int entityID) public TComponent Get(int entityID)
{ {
if (!store.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
return store[entityID]; return store[entityID];
} }

View File

@ -1,12 +0,0 @@
using System;
namespace Encompass.Exceptions
{
public class ComponentNotFoundException : Exception
{
public ComponentNotFoundException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -1,12 +0,0 @@
using System;
namespace Encompass.Exceptions
{
public class ComponentTypeMismatchException : Exception
{
public ComponentTypeMismatchException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -1,12 +0,0 @@
using System;
namespace Encompass.Exceptions
{
public class RepeatUpdateComponentException : Exception
{
public RepeatUpdateComponentException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -833,7 +833,7 @@ namespace Tests
worldBuilder.AddEngine(new ReadEntityByComponentTypeEngine()); worldBuilder.AddEngine(new ReadEntityByComponentTypeEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { }); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01); world.Update(0.01);
@ -841,7 +841,69 @@ namespace Tests
entity.Should().BeEquivalentTo(readEntity); entity.Should().BeEquivalentTo(readEntity);
} }
struct MockComponentB : IComponent
{
public MockComponentB(int value)
{
this.value = value;
}
int value;
}
static MockComponentB getComponentResult;
[Reads(typeof(MockComponent), typeof(MockComponentB))]
class GetComponentEngine : Engine
{
public override void Update(double dt)
{
getComponentResult = GetComponent<MockComponentB>(ReadEntity<MockComponent>());
}
}
[Test]
public void GetComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
worldBuilder.SetComponent(entity, new MockComponentB(3));
worldBuilder.AddEngine(new GetComponentEngine());
var world = worldBuilder.Build();
world.Update(0.01);
getComponentResult.Should().BeEquivalentTo(new MockComponentB(3));
}
[Reads(typeof(MockComponent), typeof(MockComponentB))]
class GetComponentExceptionEngine : Engine
{
public override void Update(double dt)
{
foreach (var entity in ReadEntities<MockComponent>())
{
GetComponent<MockComponentB>(entity);
}
}
}
[Test]
public void GetComponentWhenComponentIsNotOnEntity()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new GetComponentExceptionEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build();
Assert.Throws<Encompass.Exceptions.NoComponentOfTypeOnEntityException>(() => world.Update(0.01));
}
static Entity[] readEntities; static Entity[] readEntities;
@ -1606,6 +1668,25 @@ namespace Tests
Assert.DoesNotThrow(() => world.Update(0.2)); Assert.DoesNotThrow(() => world.Update(0.2));
Assert.DoesNotThrow(() => world.Update(0.25)); Assert.DoesNotThrow(() => world.Update(0.25));
} }
[Test]
public void DestroyedEntitiesAreRemovedFromTracking()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
worldBuilder.AddEngine(new DestroyWithEngine());
worldBuilder.AddEngine(new ReadEntitiesWithComponentTypeEngine());
var world = worldBuilder.Build();
world.Update(0.01);
world.Update(0.01);
readEntities.Should().BeEmpty();
}
} }
} }
} }