From fd8a0b1b00a9852cdb7708e9bdf1e3a3204d1168 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sat, 20 Jul 2019 14:10:06 -0700 Subject: [PATCH] read existing or pending components automagically based on attribute declarations --- .vscode/settings.json | 4 +- TODO | 3 +- encompass-cs/Engine.cs | 109 ++++++++++++++++------------------------- test/ComponentTest.cs | 4 +- 4 files changed, 49 insertions(+), 71 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 241d503..ef8a10b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - "dotnet-test-explorer.testProjectPath": "test", - "omnisharp.path": "latest" + // "dotnet-test-explorer.testProjectPath": "test" + // "omnisharp.path": "latest" } diff --git a/TODO b/TODO index 07668db..6a8a4e6 100644 --- a/TODO +++ b/TODO @@ -1 +1,2 @@ -- docs +- look at test coverage +- docs \ No newline at end of file diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index d00fc62..f94f519 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -171,29 +171,61 @@ namespace Encompass componentManager.MarkForDeactivation(componentID); } - private IEnumerable> ExistingComponentsOnEntity(Entity entity) where TComponent : struct, IComponent + private IEnumerable> ExistingComponents() where TComponent : struct, IComponent { - if (!receiveTypes.Contains(typeof(ComponentMessage))) + return ReadMessages>().Select((message) => (message.entity, message.componentID, message.component)); + } + + private IEnumerable> PendingComponents() where TComponent : struct, IComponent + { + return ReadMessages>().Select((message) => (message.entity, message.componentID, message.component)); + } + + private IEnumerable> ReadComponentMessages() where TComponent : struct, IComponent + { + var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage)); + var existingRead = receiveTypes.Contains(typeof(ComponentMessage)); + if (existingRead && pendingRead) + { + return ExistingComponents().Union(PendingComponents()); + } + else if (existingRead) + { + return ExistingComponents(); + } + else if (pendingRead) + { + return PendingComponents(); + } + else { throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); } + } - return ReadMessages>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component)); + private IEnumerable> ExistingComponentsOnEntity(Entity entity) where TComponent : struct, IComponent + { + return ReadComponentMessages().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3)); } private IEnumerable> PendingComponentsOnEntity(Entity entity) where TComponent : struct, IComponent { - if (!receiveTypes.Contains(typeof(PendingComponentMessage))) - { - throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name); - } - - return ReadMessages>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component)); + return ReadComponentMessages().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3)); } - protected IEnumerable> GetPendingComponents(Entity entity) where TComponent : struct, IComponent + protected IEnumerable> ReadComponents() where TComponent : struct, IComponent { - return PendingComponentsOnEntity(entity); + return ReadComponentMessages().Select((triple) => (triple.Item2, triple.Item3)); + } + + protected ValueTuple ReadComponent() where TComponent : struct, IComponent + { + return ReadComponents().Single(); + } + + protected bool SomeComponent() where TComponent : struct, IComponent + { + return ReadComponentMessages().Any(); } protected IEnumerable> GetComponents(Entity entity) where TComponent : struct, IComponent @@ -211,11 +243,6 @@ namespace Encompass return GetComponents(entity).First(); } - protected bool HasPendingComponent(Entity entity) where TComponent : struct, IComponent - { - return GetPendingComponents(entity).Any(); - } - protected bool HasComponent(Entity entity) where TComponent : struct, IComponent { return GetComponents(entity).Any(); @@ -264,36 +291,6 @@ namespace Encompass return ReadMessages().Single(); } - protected IEnumerable> ReadPendingComponents() where TComponent : struct, IComponent - { - if (!receiveTypes.Contains(typeof(PendingComponentMessage))) - { - throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name); - } - - return ReadMessages>().Select((message) => (message.componentID, message.component)); - } - - protected IEnumerable> ReadComponents() where TComponent : struct, IComponent - { - if (!receiveTypes.Contains(typeof(ComponentMessage))) - { - throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name); - } - - return ReadMessages>().Select((message) => (message.componentID, message.component)); - } - - protected ValueTuple ReadPendingComponent() where TComponent : struct, IComponent - { - return ReadPendingComponents().Single(); - } - - protected ValueTuple ReadComponent() where TComponent : struct, IComponent - { - return ReadComponents().Single(); - } - protected bool SomeMessage() where TMessage : struct, IMessage { if (!receiveTypes.Contains(typeof(TMessage))) @@ -304,26 +301,6 @@ namespace Encompass return ReadMessages().Any(); } - protected bool SomePendingComponent() where TComponent : struct, IComponent - { - if (!receiveTypes.Contains(typeof(PendingComponentMessage))) - { - throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name); - } - - return ReadMessages>().Any(); - } - - protected bool SomeComponent() where TComponent : struct, IComponent - { - if (!receiveTypes.Contains(typeof(ComponentMessage))) - { - throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name); - } - - return ReadMessages>().Any(); - } - protected void Destroy(Guid entityID) { entityManager.MarkForDestroy(entityID); diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 332231e..58cfaaf 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -136,7 +136,7 @@ namespace Tests public override void Update(double dt) { - Assert.IsTrue(HasPendingComponent(entity)); + Assert.IsTrue(HasComponent(entity)); } } @@ -489,7 +489,7 @@ namespace Tests { foreach (var checkHasMockComponentMessage in ReadMessages()) { - Assert.IsTrue(HasPendingComponent(checkHasMockComponentMessage.entity)); + Assert.IsTrue(HasComponent(checkHasMockComponentMessage.entity)); } } }