diff --git a/encompass-cs/Attributes/Reads.cs b/encompass-cs/Attributes/Reads.cs index 7c75a7a..a8892dc 100644 --- a/encompass-cs/Attributes/Reads.cs +++ b/encompass-cs/Attributes/Reads.cs @@ -22,7 +22,6 @@ namespace Encompass } this.readTypes.Add(typeof(ComponentMessage<>).MakeGenericType(readType)); - this.readTypes.Add(typeof(PendingComponentMessage<>).MakeGenericType(readType)); } } } diff --git a/encompass-cs/Attributes/ReadsPending.cs b/encompass-cs/Attributes/ReadsPending.cs new file mode 100644 index 0000000..f1798f8 --- /dev/null +++ b/encompass-cs/Attributes/ReadsPending.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Encompass.Exceptions; + +namespace Encompass +{ + [AttributeUsage(AttributeTargets.Class)] + public class ReadsPending : Attribute + { + public readonly HashSet readPendingTypes = new HashSet(); + + public ReadsPending(params Type[] readPendingTypes) + { + foreach (var readPendingType in readPendingTypes) + { + var isComponent = readPendingType.GetInterfaces().Contains(typeof(IComponent)); + + if (!isComponent) + { + throw new IllegalReadTypeException("{0} must be a Component", readPendingType.Name); + } + + this.readPendingTypes.Add(typeof(PendingComponentMessage<>).MakeGenericType(readPendingType)); + } + } + } +} \ No newline at end of file diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 4386fe6..869b7cc 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -46,6 +46,12 @@ namespace Encompass { receiveTypes.UnionWith(readsAttribute.readTypes); } + + var readsPendingAttribute = GetType().GetCustomAttribute(false); + if (readsPendingAttribute != null) + { + receiveTypes.UnionWith(readsPendingAttribute.readPendingTypes); + } } internal void AssignEntityManager(EntityManager entityManager) @@ -181,6 +187,14 @@ namespace Encompass { 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); @@ -217,11 +231,6 @@ namespace Encompass return ExistingComponentsOnEntity(entity); } - protected ValueTuple GetPendingComponent(Entity entity) where TComponent : struct, IComponent - { - return PendingComponentsOnEntity(entity).First(); - } - protected ValueTuple GetComponent(Entity entity) where TComponent : struct, IComponent { return GetComponents(entity).First(); @@ -232,11 +241,6 @@ namespace Encompass return GetComponents(entity).Any(); } - internal void ActivateComponentInWorld(Guid componentID) - { - componentManager.Activate(componentID); - } - internal void UpdateComponentInWorld(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent { componentManager.AddUpdateComponentOperation(componentID, newComponent); diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 5e49e4e..4c69056 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -123,7 +123,7 @@ namespace Tests } } - [Reads(typeof(MockComponent))] + [ReadsPending(typeof(MockComponent))] class HasMockComponentEngine : Engine { private Entity entity; @@ -481,7 +481,7 @@ namespace Tests } [Receives(typeof(CheckHasMockComponentMessage))] - [Reads(typeof(MockComponent))] + [ReadsPending(typeof(MockComponent))] class CheckHasPendingMockComponentEngine : Engine { public override void Update(double dt)