read existing or pending components automagically based on attribute declarations

pull/5/head
Evan Hemsley 2019-07-20 14:10:06 -07:00
parent 911f766cfa
commit fd8a0b1b00
4 changed files with 49 additions and 71 deletions

View File

@ -1,4 +1,4 @@
{ {
"dotnet-test-explorer.testProjectPath": "test", // "dotnet-test-explorer.testProjectPath": "test"
"omnisharp.path": "latest" // "omnisharp.path": "latest"
} }

1
TODO
View File

@ -1 +1,2 @@
- look at test coverage
- docs - docs

View File

@ -171,29 +171,61 @@ namespace Encompass
componentManager.MarkForDeactivation(componentID); componentManager.MarkForDeactivation(componentID);
} }
private IEnumerable<ValueTuple<Guid, TComponent>> ExistingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent private IEnumerable<ValueTuple<Entity, Guid, TComponent>> ExistingComponents<TComponent>() where TComponent : struct, IComponent
{ {
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>))) return ReadMessages<ComponentMessage<TComponent>>().Select((message) => (message.entity, message.componentID, message.component));
}
private IEnumerable<ValueTuple<Entity, Guid, TComponent>> PendingComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadMessages<PendingComponentMessage<TComponent>>().Select((message) => (message.entity, message.componentID, message.component));
}
private IEnumerable<ValueTuple<Entity, Guid, TComponent>> ReadComponentMessages<TComponent>() where TComponent : struct, IComponent
{
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
if (existingRead && pendingRead)
{
return ExistingComponents<TComponent>().Union(PendingComponents<TComponent>());
}
else if (existingRead)
{
return ExistingComponents<TComponent>();
}
else if (pendingRead)
{
return PendingComponents<TComponent>();
}
else
{ {
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
} }
}
return ReadMessages<ComponentMessage<TComponent>>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component)); private IEnumerable<ValueTuple<Guid, TComponent>> ExistingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3));
} }
private IEnumerable<ValueTuple<Guid, TComponent>> PendingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent private IEnumerable<ValueTuple<Guid, TComponent>> PendingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>))) return ReadComponentMessages<TComponent>().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3));
{
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
} }
return ReadMessages<PendingComponentMessage<TComponent>>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component)); protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Select((triple) => (triple.Item2, triple.Item3));
} }
protected IEnumerable<ValueTuple<Guid, TComponent>> GetPendingComponents<TComponent>(Entity entity) where TComponent : struct, IComponent protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
return PendingComponentsOnEntity<TComponent>(entity); return ReadComponents<TComponent>().Single();
}
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Any();
} }
protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) where TComponent : struct, IComponent protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) where TComponent : struct, IComponent
@ -211,11 +243,6 @@ namespace Encompass
return GetComponents<TComponent>(entity).First(); return GetComponents<TComponent>(entity).First();
} }
protected bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return GetPendingComponents<TComponent>(entity).Any();
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
return GetComponents<TComponent>(entity).Any(); return GetComponents<TComponent>(entity).Any();
@ -264,36 +291,6 @@ namespace Encompass
return ReadMessages<TMessage>().Single(); return ReadMessages<TMessage>().Single();
} }
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadPendingComponents<TComponent>() where TComponent : struct, IComponent
{
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
}
return ReadMessages<PendingComponentMessage<TComponent>>().Select((message) => (message.componentID, message.component));
}
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
return ReadMessages<ComponentMessage<TComponent>>().Select((message) => (message.componentID, message.component));
}
protected ValueTuple<Guid, TComponent> ReadPendingComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadPendingComponents<TComponent>().Single();
}
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponents<TComponent>().Single();
}
protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage
{ {
if (!receiveTypes.Contains(typeof(TMessage))) if (!receiveTypes.Contains(typeof(TMessage)))
@ -304,26 +301,6 @@ namespace Encompass
return ReadMessages<TMessage>().Any(); return ReadMessages<TMessage>().Any();
} }
protected bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent
{
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
}
return ReadMessages<PendingComponentMessage<TComponent>>().Any();
}
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
return ReadMessages<ComponentMessage<TComponent>>().Any();
}
protected void Destroy(Guid entityID) protected void Destroy(Guid entityID)
{ {
entityManager.MarkForDestroy(entityID); entityManager.MarkForDestroy(entityID);

View File

@ -136,7 +136,7 @@ namespace Tests
public override void Update(double dt) public override void Update(double dt)
{ {
Assert.IsTrue(HasPendingComponent<MockComponent>(entity)); Assert.IsTrue(HasComponent<MockComponent>(entity));
} }
} }
@ -489,7 +489,7 @@ namespace Tests
{ {
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>()) foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
{ {
Assert.IsTrue(HasPendingComponent<MockComponent>(checkHasMockComponentMessage.entity)); Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
} }
} }
} }