write priority system and require read declaration for entity lookups by component

pull/5/head
Evan Hemsley 2019-08-21 15:13:48 -07:00
parent 9813c260ae
commit 971a663cf9
10 changed files with 149 additions and 183 deletions

View File

@ -19,11 +19,10 @@ namespace Encompass
private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly List<(Entity, Type, Guid, IComponent)> componentWriteData = new List<(Entity, Type, Guid, IComponent)>(); private readonly Dictionary<(Entity, Type), (Guid, IComponent)> componentWriteData = new Dictionary<(Entity, Type), (Guid, IComponent)>();
private Dictionary<(Entity, Type), int> componentWritePriorities = new Dictionary<(Entity, Type), int>(); private readonly Dictionary<(Entity, Type), int> componentWritePriorities = new Dictionary<(Entity, Type), int>();
private readonly HashSet<Guid> componentIDsMarkedForWrite = new HashSet<Guid>(); private readonly HashSet<Guid> componentIDsMarkedForWrite = new HashSet<Guid>();
private readonly HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>(); private readonly HashSet<Guid> componentsMarkedForRemoval = new HashSet<Guid>();
private readonly Dictionary<Guid, IComponent> pendingUpdates = new Dictionary<Guid, IComponent>();
public ComponentManager(DrawLayerManager drawLayerManager) public ComponentManager(DrawLayerManager drawLayerManager)
{ {
@ -53,19 +52,19 @@ namespace Encompass
id = NextID(); id = NextID();
} }
if (componentWritePriorities.ContainsKey((entity, typeof(TComponent)))) if (componentWriteData.ContainsKey((entity, typeof(TComponent))))
{ {
var currentPriority = componentWritePriorities[(entity, typeof(TComponent))]; var currentPriority = componentWritePriorities[(entity, typeof(TComponent))];
if (priority < currentPriority) if (priority < currentPriority)
{ {
componentWriteData.Add((entity, typeof(TComponent), id, component)); componentWriteData[(entity, typeof(TComponent))] = (id, component);
componentWritePriorities[(entity, typeof(TComponent))] = priority; componentWritePriorities[(entity, typeof(TComponent))] = priority;
componentIDsMarkedForWrite.Add(id); componentIDsMarkedForWrite.Add(id);
} }
} }
else else
{ {
componentWriteData.Add((entity, typeof(TComponent), id, component)); componentWriteData.Add((entity, typeof(TComponent)), (id, component));
componentWritePriorities[(entity, typeof(TComponent))] = priority; componentWritePriorities[(entity, typeof(TComponent))] = priority;
componentIDsMarkedForWrite.Add(id); componentIDsMarkedForWrite.Add(id);
} }
@ -81,10 +80,6 @@ namespace Encompass
} }
internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component) internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{
if (!entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { return; }
if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{ {
IDToComponent[componentID] = component; IDToComponent[componentID] = component;
componentIDToEntityID[componentID] = entity.ID; componentIDToEntityID[componentID] = entity.ID;
@ -97,18 +92,30 @@ namespace Encompass
typeToComponentIDs[type].Add(componentID); typeToComponentIDs[type].Add(componentID);
entityIDToComponentIDs[entity.ID].Add(componentID); entityIDToComponentIDs[entity.ID].Add(componentID);
} }
else
internal void UpdateComponent(Guid componentID, IComponent component)
{ {
throw new MultipleComponentOfSameTypeException("Entity {0} cannot have multiple components of type {1}", entity.ID, type.Name); IDToComponent[componentID] = component;
}
} }
internal void WriteComponents() internal void WriteComponents()
{ {
foreach (var (entity, type, componentID, component) in componentWriteData) foreach (var keyValuePair in componentWriteData)
{
var (entity, type) = keyValuePair.Key;
var (componentID, component) = keyValuePair.Value;
if (!entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; }
if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{
UpdateComponent(componentID, component);
}
else
{ {
AddComponent(entity, type, componentID, component); AddComponent(entity, type, componentID, component);
} }
}
componentWriteData.Clear(); componentWriteData.Clear();
componentIDsMarkedForWrite.Clear(); componentIDsMarkedForWrite.Clear();
@ -173,26 +180,6 @@ namespace Encompass
return componentIDToEntityID[componentID]; return componentIDToEntityID[componentID];
} }
internal void AddUpdateComponentOperation<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
{
if (pendingUpdates.ContainsKey(componentID))
{
throw new RepeatUpdateComponentException("Component {0} with ID {1} was updated multiple times this frame", typeof(TComponent).Name, componentID);
}
pendingUpdates.Add(componentID, newComponentValue);
}
internal void PerformComponentUpdates()
{
foreach (var idPair in pendingUpdates)
{
IDToComponent[idPair.Key] = idPair.Value;
}
pendingUpdates.Clear();
}
internal void MarkAllComponentsOnEntityForRemoval(Guid entityID) internal void MarkAllComponentsOnEntityForRemoval(Guid entityID)
{ {
foreach (var componentID in GetComponentIDsByEntityID(entityID)) foreach (var componentID in GetComponentIDsByEntityID(entityID))

View File

@ -94,18 +94,34 @@ namespace Encompass
return entityManager.GetEntity(entityID); return entityManager.GetEntity(entityID);
} }
protected Guid GetEntityIDByComponentID(Guid componentID) protected Guid GetEntityIDByComponentID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{ {
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
if (!pendingRead && !existingRead)
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
return componentMessageManager.GetEntityIDByComponentID(componentID); return componentMessageManager.GetEntityIDByComponentID(componentID);
} }
protected Entity GetEntityByComponentID(Guid componentID) protected Entity GetEntityByComponentID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{ {
return GetEntity(GetEntityIDByComponentID(componentID)); return GetEntity(GetEntityIDByComponentID<TComponent>(componentID));
} }
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{ {
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
if (!pendingRead && !existingRead)
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
if (componentMessageManager.GetComponentTypeByID(componentID) != typeof(TComponent)) if (componentMessageManager.GetComponentTypeByID(componentID) != typeof(TComponent))
{ {
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentMessageManager.GetComponentTypeByID(componentID).Name); throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentMessageManager.GetComponentTypeByID(componentID).Name);
@ -248,11 +264,6 @@ namespace Encompass
} }
} }
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
{
componentManager.AddUpdateComponentOperation(componentID, newComponent);
}
protected Guid SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent protected Guid SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{ {
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0; var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0;
@ -274,17 +285,12 @@ namespace Encompass
SendPendingComponentMessage(newComponentMessage); SendPendingComponentMessage(newComponentMessage);
} }
ComponentWriteMessage<TComponent> componentUpdateMessage;
componentUpdateMessage.componentID = componentID;
componentUpdateMessage.component = component;
SendMessage(componentUpdateMessage);
return componentID; return componentID;
} }
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
{ {
return SetComponent(GetEntityByComponentID(componentID), component); return SetComponent(GetEntityByComponentID<TComponent>(componentID), component);
} }
protected Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent protected Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
@ -308,11 +314,6 @@ namespace Encompass
SendPendingComponentMessage(newComponentMessage); SendPendingComponentMessage(newComponentMessage);
} }
ComponentWriteMessage<TComponent> componentUpdateMessage;
componentUpdateMessage.componentID = componentID;
componentUpdateMessage.component = component;
SendMessage(componentUpdateMessage);
return componentID; return componentID;
} }

View File

@ -1,18 +0,0 @@
namespace Encompass.Engines
{
internal class ComponentUpdater<TComponent> : Engine where TComponent : struct, IComponent
{
public ComponentUpdater() : base()
{
receiveTypes.Add(typeof(ComponentWriteMessage<TComponent>));
}
public override void Update(double dt)
{
foreach (var componentUpdateMessage in ReadMessages<ComponentWriteMessage<TComponent>>())
{
UpdateComponentInWorld(componentUpdateMessage.componentID, componentUpdateMessage.component);
}
}
}
}

View File

@ -41,7 +41,6 @@ namespace Encompass
componentMessageManager.ClearMessages(); componentMessageManager.ClearMessages();
entityManager.DestroyMarkedEntities(); entityManager.DestroyMarkedEntities();
componentManager.PerformComponentUpdates();
componentManager.RemoveMarkedComponents(); componentManager.RemoveMarkedComponents();
componentManager.WriteComponents(); componentManager.WriteComponents();
} }

View File

@ -50,14 +50,14 @@ namespace Encompass
messageManager.AddMessageDelayed(message, time); messageManager.AddMessageDelayed(message, time);
} }
public Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent public Guid SetComponent<TComponent>(Entity entity, TComponent component, int priority = 0) where TComponent : struct, IComponent
{ {
return componentManager.MarkComponentForWrite(entity, component, 0); return componentManager.MarkComponentForWrite(entity, component, priority);
} }
public Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent public Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int priority = 0, int layer = 0) where TComponent : struct, IComponent
{ {
return componentManager.MarkDrawComponentForWrite(entity, component, layer); return componentManager.MarkDrawComponentForWrite(entity, component, priority, layer);
} }
internal void RegisterComponent(Type componentType) internal void RegisterComponent(Type componentType)
@ -66,11 +66,6 @@ namespace Encompass
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentMessageEmitter<>).MakeGenericType(componentType))); AddEngine((Engine)Activator.CreateInstance(typeof(ComponentMessageEmitter<>).MakeGenericType(componentType)));
} }
internal void RegisterNewComponentUpdater(Type componentType)
{
AddEngine((Engine)Activator.CreateInstance(typeof(ComponentUpdater<>).MakeGenericType(componentType)));
}
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{ {
engine.AssignEntityManager(entityManager); engine.AssignEntityManager(entityManager);
@ -128,19 +123,6 @@ namespace Encompass
typeToReaders[receiveType].Add(engine); typeToReaders[receiveType].Add(engine);
} }
foreach (var sendType in engine.sendTypes)
{
if (sendType.IsGenericType)
{
var genericTypeDefinition = sendType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(ComponentWriteMessage<>))
{
var componentType = sendType.GetGenericArguments().Single();
RegisterNewComponentUpdater(componentType);
}
}
}
return engine; return engine;
} }
@ -268,7 +250,6 @@ namespace Encompass
renderManager renderManager
); );
componentManager.PerformComponentUpdates();
componentManager.RemoveMarkedComponents(); componentManager.RemoveMarkedComponents();
componentManager.WriteComponents(); componentManager.WriteComponents();

View File

@ -1,3 +1,4 @@
using System.ComponentModel;
using NUnit.Framework; using NUnit.Framework;
using FluentAssertions; using FluentAssertions;
@ -11,7 +12,7 @@ namespace Tests
{ {
public class ComponentTests public class ComponentTests
{ {
struct MockComponent : IComponent struct MockComponent : Encompass.IComponent
{ {
public string myString; public string myString;
public int myInt; public int myInt;
@ -22,7 +23,6 @@ namespace Tests
public Entity entity; public Entity entity;
} }
static IEnumerable<(Guid, MockComponent)> gottenMockComponentIDPairs = Enumerable.Empty<(Guid, MockComponent)>();
static (Guid, MockComponent) gottenMockComponentIDPair; static (Guid, MockComponent) gottenMockComponentIDPair;
[Receives(typeof(EntityMessage))] [Receives(typeof(EntityMessage))]
@ -70,7 +70,7 @@ namespace Tests
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
AddComponentTestMessage addComponentTestMessage; AddComponentTestMessage addComponentTestMessage;
addComponentTestMessage.entity = entity; addComponentTestMessage.entity = entity;
@ -83,48 +83,64 @@ namespace Tests
} }
[Test] [Test]
public void AddMultipleComponentOfSameTypeToEntity() public void SetMultipleComponentOfSameTypeOnEntity()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ReadMockComponentEngine());
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" }, 2);
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" }, 0);
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" }, 1);
Assert.Throws<MultipleComponentOfSameTypeException>(() => worldBuilder.Build()); var world = worldBuilder.Build();
world.Update(0.01);
Assert.That(gottenMockComponentIDPair.Item2.myInt, Is.EqualTo(50));
Assert.That(gottenMockComponentIDPair.Item2.myString, Is.EqualTo("hi"));
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
[WritesPending(typeof(MockComponent))]
[Writes(typeof(MockComponent))] [Writes(typeof(MockComponent))]
class MultipleAddEngine : Engine class OverwriteEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>()) foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{ {
var entity = GetEntityByComponentID(mockComponentID); var entity = GetEntityByComponentID<MockComponent>(mockComponentID);
SetComponent(entity, new MockComponent()); SetComponent(entity, new MockComponent { myInt = 420 });
} }
} }
} }
[ReadsPending(typeof(MockComponent))]
[Reads(typeof(MockComponent))]
class ReadMockComponentEngine : Engine
{
public override void Update(double dt)
{
gottenMockComponentIDPair = ReadComponent<MockComponent>();
}
}
[Test] [Test]
public void EngineAddMultipleComponentOfSameTypeToEntity() public void EngineOverwriteComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new MultipleAddEngine()); worldBuilder.AddEngine(new OverwriteEngine());
worldBuilder.AddEngine(new ReadMockComponentEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent {});
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01);
Assert.Throws<MultipleComponentOfSameTypeException>(() => world.Update(0.01)); Assert.That(gottenMockComponentIDPair.Item2.myInt, Is.EqualTo(420));
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
@ -135,7 +151,7 @@ namespace Tests
{ {
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>()) foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{ {
var entity = GetEntityByComponentID(mockComponentID); var entity = GetEntityByComponentID<MockComponent>(mockComponentID);
SetComponent(entity, mockComponent); SetComponent(entity, mockComponent);
RemoveComponent(mockComponentID); RemoveComponent(mockComponentID);
} }
@ -149,7 +165,7 @@ namespace Tests
worldBuilder.AddEngine(new AddAndRemoveComponentEngine()); worldBuilder.AddEngine(new AddAndRemoveComponentEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -242,7 +258,7 @@ namespace Tests
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
var componentID = worldBuilder.AddComponent<MockComponent>(entity, mockComponent); var componentID = worldBuilder.SetComponent<MockComponent>(entity, mockComponent);
EntityMessage entityMessage; EntityMessage entityMessage;
entityMessage.entity = entity; entityMessage.entity = entity;
@ -285,7 +301,7 @@ namespace Tests
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
HasComponentTestMessage hasComponentTestMessage; HasComponentTestMessage hasComponentTestMessage;
hasComponentTestMessage.entity = entity; hasComponentTestMessage.entity = entity;
@ -371,7 +387,7 @@ namespace Tests
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
var componentID = worldBuilder.AddComponent(entity, mockComponent); var componentID = worldBuilder.SetComponent(entity, mockComponent);
RemoveComponentTestMessage removeComponentMessage; RemoveComponentTestMessage removeComponentMessage;
removeComponentMessage.entity = entity; removeComponentMessage.entity = entity;

View File

@ -59,8 +59,8 @@ namespace Tests
mockComponentB.myInt = 1; mockComponentB.myInt = 1;
mockComponentB.myString = "howdy"; mockComponentB.myString = "howdy";
var componentAID = worldBuilder.AddComponent(entity, mockComponent); var componentAID = worldBuilder.SetComponent(entity, mockComponent);
var componentBID = worldBuilder.AddComponent(entityB, mockComponentB); var componentBID = worldBuilder.SetComponent(entityB, mockComponentB);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -83,7 +83,7 @@ namespace Tests
mockComponent.myInt = 0; mockComponent.myInt = 0;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -109,8 +109,8 @@ namespace Tests
mockComponentB.myInt = 1; mockComponentB.myInt = 1;
mockComponentB.myString = "howdy"; mockComponentB.myString = "howdy";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.AddComponent(entityB, mockComponentB); worldBuilder.SetComponent(entityB, mockComponentB);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -129,7 +129,7 @@ namespace Tests
component.myInt = 420; component.myInt = 420;
component.myString = "blaze it"; component.myString = "blaze it";
SetComponent(componentID, component); SetComponent(GetEntityByComponentID<MockComponent>(componentID), component);
} }
} }
@ -149,7 +149,7 @@ namespace Tests
mockComponent.myInt = 0; mockComponent.myInt = 0;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -161,7 +161,6 @@ namespace Tests
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
[Writes(typeof(MockComponent))]
public class UndeclaredUpdateComponentTestEngine : Engine public class UndeclaredUpdateComponentTestEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
@ -188,7 +187,7 @@ namespace Tests
mockComponent.myInt = 0; mockComponent.myInt = 0;
mockComponent.myString = "hello"; mockComponent.myString = "hello";
worldBuilder.AddComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -354,7 +353,7 @@ namespace Tests
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -392,10 +391,10 @@ namespace Tests
componentB.myString = "hello"; componentB.myString = "hello";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, componentA); worldBuilder.SetComponent(entity, componentA);
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityB, componentB); worldBuilder.SetComponent(entityB, componentB);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01f);
@ -437,7 +436,7 @@ namespace Tests
foreach (var componentPair in ReadComponents<DestroyerComponent>()) foreach (var componentPair in ReadComponents<DestroyerComponent>())
{ {
var componentID = componentPair.Item1; var componentID = componentPair.Item1;
var entityID = GetEntityIDByComponentID(componentID); var entityID = GetEntityIDByComponentID<DestroyerComponent>(componentID);
Destroy(entityID); Destroy(entityID);
} }
} }
@ -470,13 +469,13 @@ namespace Tests
mockComponent.myInt = 2; mockComponent.myInt = 2;
mockComponent.myString = "blah"; mockComponent.myString = "blah";
worldBuilder.AddComponent(entity, destroyerComponent); worldBuilder.SetComponent(entity, destroyerComponent);
var componentID = worldBuilder.AddComponent(entity, mockComponent); var componentID = worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.AddComponent(entityB, destroyerComponent); worldBuilder.SetComponent(entityB, destroyerComponent);
var componentBID = worldBuilder.AddComponent(entityB, mockComponent); var componentBID = worldBuilder.SetComponent(entityB, mockComponent);
var componentCID = worldBuilder.AddComponent(entityC, mockComponent); var componentCID = worldBuilder.SetComponent(entityC, mockComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -496,7 +495,7 @@ namespace Tests
foreach (var componentPair in ReadComponents<DestroyerComponent>()) foreach (var componentPair in ReadComponents<DestroyerComponent>())
{ {
var componentID = componentPair.Item1; var componentID = componentPair.Item1;
var entity = GetEntityByComponentID(componentID); var entity = GetEntityByComponentID<MockComponent>(componentID);
var (id, _) = GetComponent<MockComponent>(entity); var (id, _) = GetComponent<MockComponent>(entity);
RemoveComponent(id); RemoveComponent(id);
Destroy(entity.ID); Destroy(entity.ID);
@ -513,8 +512,8 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new DestroyerComponent()); worldBuilder.SetComponent(entity, new DestroyerComponent());
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -529,7 +528,7 @@ namespace Tests
public override void Update(double dt) public override void Update(double dt)
{ {
var componentID = ReadComponent<MockComponent>().Item1; var componentID = ReadComponent<MockComponent>().Item1;
entityFromComponentIDResult = GetEntityByComponentID(componentID); entityFromComponentIDResult = GetEntityByComponentID<MockComponent>(componentID);
} }
} }
@ -544,7 +543,7 @@ namespace Tests
component.myString = "howdy"; component.myString = "howdy";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, component); worldBuilder.SetComponent(entity, component);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01); world.Update(0.01);
@ -561,7 +560,7 @@ namespace Tests
{ {
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>()) foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{ {
var entity = GetEntityByComponentID(mockComponentID); var entity = GetEntityByComponentID<MockComponent>(mockComponentID);
RemoveComponent(mockComponentID); RemoveComponent(mockComponentID);
SetComponent(entity, new MockComponent()); SetComponent(entity, new MockComponent());
} }
@ -575,7 +574,7 @@ namespace Tests
{ {
foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>()) foreach (var (mockComponentID, mockComponent) in ReadComponents<MockComponent>())
{ {
entityFromComponentIDResult = GetEntityByComponentID(mockComponentID); entityFromComponentIDResult = GetEntityByComponentID<MockComponent>(mockComponentID);
} }
} }
} }
@ -588,7 +587,7 @@ namespace Tests
worldBuilder.AddEngine(new GetEntityFromPendingComponentIDEngine()); worldBuilder.AddEngine(new GetEntityFromPendingComponentIDEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -615,7 +614,7 @@ namespace Tests
worldBuilder.AddEngine(new GetPendingComponentFromIDEngine()); worldBuilder.AddEngine(new GetPendingComponentFromIDEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent()); worldBuilder.SetComponent(entity, new MockComponent());
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -644,7 +643,7 @@ namespace Tests
component.myString = "howdy"; component.myString = "howdy";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, component); worldBuilder.SetComponent(entity, component);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01f);
@ -654,7 +653,7 @@ namespace Tests
struct OtherComponent : IComponent { } struct OtherComponent : IComponent { }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent), typeof(OtherComponent))]
class GetComponentByIDWithTypeMismatchEngine : Engine class GetComponentByIDWithTypeMismatchEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
@ -675,7 +674,7 @@ namespace Tests
component.myString = "howdy"; component.myString = "howdy";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, component); worldBuilder.SetComponent(entity, component);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -710,7 +709,7 @@ namespace Tests
EntityIDComponent entityIDComponent; EntityIDComponent entityIDComponent;
entityIDComponent.entityID = entityTwo.ID; entityIDComponent.entityID = entityTwo.ID;
worldBuilder.AddComponent(entity, entityIDComponent); worldBuilder.SetComponent(entity, entityIDComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -729,9 +728,10 @@ namespace Tests
public MockComponent mockComponent; public MockComponent mockComponent;
} }
[Reads(typeof(MockComponent))]
[Receives(typeof(MockComponentUpdateMessage))] [Receives(typeof(MockComponentUpdateMessage))]
[Writes(typeof(MockComponent))] [Writes(typeof(MockComponent))]
class RepeatUpdateEngine : Engine class UpdateByComponentIDEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
@ -744,10 +744,10 @@ namespace Tests
} }
[Test] [Test]
public void EngineUpdatesComponentMultipleTimes() public void EngineUpdateByComponentID()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new RepeatUpdateEngine()); worldBuilder.AddEngine(new UpdateByComponentIDEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
@ -755,7 +755,7 @@ namespace Tests
mockComponent.myInt = 1; mockComponent.myInt = 1;
mockComponent.myString = "5"; mockComponent.myString = "5";
var mockComponentID = worldBuilder.AddComponent(entity, mockComponent); var mockComponentID = worldBuilder.SetComponent(entity, mockComponent);
MockComponentUpdateMessage mockComponentUpdateMessage; MockComponentUpdateMessage mockComponentUpdateMessage;
mockComponentUpdateMessage.componentID = mockComponentID; mockComponentUpdateMessage.componentID = mockComponentID;
@ -763,7 +763,7 @@ namespace Tests
worldBuilder.SendMessage(mockComponentUpdateMessage); worldBuilder.SendMessage(mockComponentUpdateMessage);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
Assert.Throws<RepeatUpdateComponentException>(() => world.Update(0.01)); Assert.DoesNotThrow(() => world.Update(0.01));
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
@ -789,7 +789,7 @@ namespace Tests
worldBuilder.AddEngine(new MessageReadEngine()); worldBuilder.AddEngine(new MessageReadEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, new MockComponent {}); worldBuilder.SetComponent(entity, new MockComponent {});
var world = worldBuilder.Build(); var world = worldBuilder.Build();

View File

@ -32,7 +32,7 @@ namespace Tests
AComponent aComponent; AComponent aComponent;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
var componentID = worldBuilder.AddComponent(entity, aComponent); var componentID = worldBuilder.SetComponent(entity, aComponent);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -52,10 +52,10 @@ namespace Tests
AComponent aComponentTwo; AComponent aComponentTwo;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
var componentID = worldBuilder.AddComponent(entity, aComponent); var componentID = worldBuilder.SetComponent(entity, aComponent);
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();
var componentTwoID = worldBuilder.AddComponent(entityB, aComponentTwo); var componentTwoID = worldBuilder.SetComponent(entityB, aComponentTwo);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01f);

View File

@ -52,9 +52,9 @@ namespace Tests
TestDrawComponent testDrawComponent; TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, aComponent); worldBuilder.SetComponent(entity, aComponent);
worldBuilder.AddComponent(entity, cComponent); worldBuilder.SetComponent(entity, cComponent);
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 2); var testDrawComponentID = worldBuilder.SetDrawComponent(entity, testDrawComponent, 2);
var world = worldBuilder.Build(); var world = worldBuilder.Build();
@ -72,7 +72,7 @@ namespace Tests
{ {
foreach (var (componentID, component) in ReadComponents<TestDrawComponent>()) foreach (var (componentID, component) in ReadComponents<TestDrawComponent>())
{ {
Destroy(GetEntityIDByComponentID(componentID)); Destroy(GetEntityIDByComponentID<TestDrawComponent>(componentID));
} }
} }
} }
@ -88,7 +88,7 @@ namespace Tests
TestDrawComponent testDrawComponent; TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 1); var testDrawComponentID = worldBuilder.SetDrawComponent(entity, testDrawComponent, 1);
var world = worldBuilder.Build(); var world = worldBuilder.Build();

View File

@ -43,26 +43,26 @@ namespace Tests
TestDrawComponent testDrawComponent = default(TestDrawComponent); TestDrawComponent testDrawComponent = default(TestDrawComponent);
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entity, testComponent); worldBuilder.SetComponent(entity, testComponent);
var testDrawComponentOneID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 3); var testDrawComponentOneID = worldBuilder.SetDrawComponent(entity, testDrawComponent, 3);
TestDrawComponent testDrawComponentTwo = default(TestDrawComponent); TestDrawComponent testDrawComponentTwo = default(TestDrawComponent);
var entityTwo = worldBuilder.CreateEntity(); var entityTwo = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityTwo, testComponent); worldBuilder.SetComponent(entityTwo, testComponent);
var testDrawComponentTwoID = worldBuilder.AddDrawComponent(entityTwo, testDrawComponentTwo, 1); var testDrawComponentTwoID = worldBuilder.SetDrawComponent(entityTwo, testDrawComponentTwo, 1);
TestDrawComponent testDrawComponentThree = default(TestDrawComponent); TestDrawComponent testDrawComponentThree = default(TestDrawComponent);
var entityThree = worldBuilder.CreateEntity(); var entityThree = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityThree, testComponent); worldBuilder.SetComponent(entityThree, testComponent);
var testDrawComponentThreeID = worldBuilder.AddDrawComponent(entityThree, testDrawComponentThree, 5); var testDrawComponentThreeID = worldBuilder.SetDrawComponent(entityThree, testDrawComponentThree, 5);
TestDrawComponent testDrawComponentFour = default(TestDrawComponent); TestDrawComponent testDrawComponentFour = default(TestDrawComponent);
var entityFour = worldBuilder.CreateEntity(); var entityFour = worldBuilder.CreateEntity();
worldBuilder.AddComponent(entityFour, testComponent); worldBuilder.SetComponent(entityFour, testComponent);
var testDrawComponentFourID = worldBuilder.AddDrawComponent(entityFour, testDrawComponentFour, -5); var testDrawComponentFourID = worldBuilder.SetDrawComponent(entityFour, testDrawComponentFour, -5);
var world = worldBuilder.Build(); var world = worldBuilder.Build();