update component uses a callback now

pull/5/head
Evan Hemsley 2019-06-15 11:40:42 -07:00
parent cb872b7c42
commit d9756e0c3d
2 changed files with 37 additions and 23 deletions

View File

@ -38,11 +38,22 @@ namespace Encompass {
return this.componentManager.GetActiveComponentByType<TComponent>();
}
protected void UpdateComponent<TComponent>(TComponent originalComponent, TComponent newComponent) where TComponent : struct, IComponent {
internal void UpdateComponentInWorld<TComponent>(TComponent originalComponent, TComponent newComponent) where TComponent : struct, IComponent {
if (mutateComponentTypes.Contains(typeof(TComponent))) {
this.componentManager.UpdateComponent(originalComponent, newComponent);
} else {
throw new IllegalComponentMutationException("Engine {0} tried to mutate undeclared Component {1}", this.GetType().ToString(), typeof(TComponent).ToString());
throw new IllegalComponentMutationException("Engine {0} tried to mutate undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
}
protected void UpdateComponent<TComponent>(TComponent component, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent {
var updatedComponent = updateFunction(component);
this.UpdateComponentInWorld(component, updatedComponent);
}
protected void UpdateComponents<TComponent>(IEnumerable<TComponent> components, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent {
foreach (var component in components) {
this.UpdateComponent(component, updateFunction);
}
}
}

View File

@ -8,14 +8,14 @@ using System;
namespace Tests {
public class EngineTest
{
static IEnumerable<MockComponent> components;
static MockComponent component;
static IEnumerable<MockComponent> resultComponents;
static MockComponent resultComponent;
public class ReadComponentsTestEngine : Engine
{
public override void Update(float dt)
{
components = this.ReadComponents<MockComponent>();
resultComponents = this.ReadComponents<MockComponent>();
}
}
@ -23,7 +23,7 @@ namespace Tests {
{
public override void Update(float dt)
{
component = this.ReadComponent<MockComponent>();
resultComponent = this.ReadComponent<MockComponent>();
}
}
@ -50,8 +50,8 @@ namespace Tests {
world.Update(0.01f);
Assert.Contains(mockComponent, components.ToList());
Assert.Contains(mockComponentB, components.ToList());
Assert.Contains(mockComponent, resultComponents.ToList());
Assert.Contains(mockComponentB, resultComponents.ToList());
}
[Test]
@ -72,7 +72,7 @@ namespace Tests {
world.Update(0.01f);
Assert.AreEqual(mockComponent, component);
Assert.AreEqual(mockComponent, resultComponent);
}
[Test]
@ -104,12 +104,13 @@ namespace Tests {
{
public override void Update(float dt)
{
var originalComponent = this.ReadComponent<MockComponent>();
var newComponent = this.ReadComponent<MockComponent>();
newComponent.myInt = 420;
newComponent.myString = "blaze it";
this.UpdateComponent(originalComponent, newComponent);
component = this.ReadComponent<MockComponent>();
var component = this.ReadComponent<MockComponent>();
this.UpdateComponent(component, (MockComponent comp) => {
comp.myInt = 420;
comp.myString = "blaze it";
return comp;
});
resultComponent = this.ReadComponent<MockComponent>();
}
}
@ -131,19 +132,20 @@ namespace Tests {
world.Update(0.01f);
Assert.AreEqual(420, component.myInt);
Assert.AreEqual("blaze it", component.myString);
Assert.AreEqual(420, resultComponent.myInt);
Assert.AreEqual("blaze it", resultComponent.myString);
}
public class UndeclaredUpdateComponentTestEngine : Engine
{
public override void Update(float dt)
{
var originalComponent = this.ReadComponent<MockComponent>();
var newComponent = this.ReadComponent<MockComponent>();
newComponent.myInt = 420;
newComponent.myString = "blaze it";
this.UpdateComponent(originalComponent, newComponent);
var component = this.ReadComponent<MockComponent>();
this.UpdateComponent(component, (MockComponent comp) => {
comp.myInt = 420;
comp.myString = "blaze it";
return comp;
});
component = this.ReadComponent<MockComponent>();
}
}
@ -164,7 +166,8 @@ namespace Tests {
var world = worldBuilder.Build();
Assert.Throws<IllegalComponentMutationException>(() => world.Update(0.01f));
var ex =Assert.Throws<IllegalComponentMutationException>(() => world.Update(0.01f));
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to mutate undeclared Component MockComponent"));
}
}
}