changes to component update handling

pull/5/head
Evan Hemsley 2019-12-26 20:18:46 -08:00
parent b644f4fb4b
commit 1a17f9a8e1
3 changed files with 87 additions and 21 deletions

View File

@ -45,16 +45,17 @@ namespace Encompass
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{ {
RegisterExistingOrImmediateComponentMessage(entity, component); existingAndImmediateComponentStore.Set(entity.ID, component);
existingComponentStore.Set(entity.ID, component); existingComponentStore.Set(entity.ID, component);
UpToDateComponentStore.Set(entity.ID, component);
} }
internal bool AddImmediateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent internal bool AddImmediateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{ {
if (immediateComponentStore.Set(entity.ID, component, priority)) if (immediateComponentStore.Set(entity.ID, component, priority))
{ {
RegisterExistingOrImmediateComponentMessage(entity, component); existingAndImmediateComponentStore.Set(entity.ID, component);
UpToDateComponentStore.Set(entity.ID, component);
return true; return true;
} }
@ -84,12 +85,6 @@ namespace Encompass
return false; return false;
} }
private void RegisterExistingOrImmediateComponentMessage<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
existingAndImmediateComponentStore.Set(entity.ID, component);
UpToDateComponentStore.Set(entity.ID, component);
}
public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{ {
return UpToDateComponentStore.Set<TComponent>(entity.ID, component, priority); return UpToDateComponentStore.Set<TComponent>(entity.ID, component, priority);
@ -157,6 +152,11 @@ namespace Encompass
// read components by entity and type // read components by entity and type
internal TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return existingAndImmediateComponentStore.Get<TComponent>(entity.ID);
}
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent internal TComponent ReadExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
return existingComponentStore.Get<TComponent>(entity.ID); return existingComponentStore.Get<TComponent>(entity.ID);

View File

@ -316,18 +316,7 @@ namespace Encompass
var existingRead = readTypes.Contains(typeof(TComponent)); var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead) if (existingRead && immediateRead)
{ {
if (componentUpdateManager.HasImmediateComponent<TComponent>(entity)) return componentUpdateManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entity);
{
return componentUpdateManager.ReadImmediateComponentByEntityAndType<TComponent>(entity);
}
else if (componentUpdateManager.HasExistingComponent<TComponent>(entity))
{
return componentUpdateManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
}
else
{
throw new NoComponentOfTypeOnEntityException("No Component of type {0} exists on Entity {1}", typeof(TComponent).Name, entity.ID);
}
} }
else if (existingRead) else if (existingRead)
{ {

View File

@ -1462,6 +1462,83 @@ namespace Tests
queriedEntities.ToArray().Should().BeEquivalentTo(new Entity[] { entityB }); queriedEntities.ToArray().Should().BeEquivalentTo(new Entity[] { entityB });
} }
[ReadsImmediate(typeof(MockComponentB))]
class ReadImmediateComponentsEngine : Engine
{
private List<MockComponentB> _components;
public ReadImmediateComponentsEngine(List<MockComponentB> components)
{
_components = components;
}
public override void Update(double dt)
{
_components.AddRange(ReadComponents<MockComponentB>());
}
}
[Test]
public void ReadImmediateComponents()
{
var worldBuilder = new WorldBuilder();
var _components = new List<MockComponentB>();
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
worldBuilder.AddEngine(new AddImmediateComponentEngine());
worldBuilder.AddEngine(new ReadImmediateComponentsEngine(_components));
var world = worldBuilder.Build();
world.Update(0.01);
_components.Should().NotBeEmpty();
}
[ReadsImmediate(typeof(MockComponentB))]
[Reads(typeof(MockComponent))]
class HasAndGetImmediateComponentEngine : Engine
{
private List<MockComponentB> _components;
public HasAndGetImmediateComponentEngine(List<MockComponentB> components)
{
_components = components;
}
public override void Update(double dt)
{
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>())
{
if (HasComponent<MockComponentB>(entity))
{
_components.Add(GetComponent<MockComponentB>(entity));
}
}
}
}
[Test]
public void HasAndGetImmediateComponent()
{
var worldBuilder = new WorldBuilder();
var _components = new List<MockComponentB>();
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent());
worldBuilder.AddEngine(new AddImmediateComponentEngine());
worldBuilder.AddEngine(new HasAndGetImmediateComponentEngine(_components));
var world = worldBuilder.Build();
world.Update(0.01);
_components.Should().NotBeEmpty();
}
} }
} }
} }