diff --git a/encompass-cs/Attributes/Reads.cs b/encompass-cs/Attributes/Reads.cs index a8892dc..7c75a7a 100644 --- a/encompass-cs/Attributes/Reads.cs +++ b/encompass-cs/Attributes/Reads.cs @@ -22,6 +22,7 @@ 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 deleted file mode 100644 index e0a774d..0000000 --- a/encompass-cs/Attributes/ReadsPending.cs +++ /dev/null @@ -1,28 +0,0 @@ -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)); - } - } - } -} diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index b18107a..e137355 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -45,10 +45,13 @@ namespace Encompass entityIDToComponentIDs.Add(entityID, new HashSet()); } - internal Guid AddComponent(Guid entityID, TComponent component) where TComponent : struct, IComponent + internal Guid NextID() { - var componentID = Guid.NewGuid(); + return Guid.NewGuid(); + } + internal Guid AddComponent(Entity entity, Guid componentID, TComponent component) where TComponent : struct, IComponent + { IDToComponent[componentID] = component; componentIDToType[componentID] = typeof(TComponent); @@ -59,19 +62,19 @@ namespace Encompass typeToComponentIDs[typeof(TComponent)].Add(componentID); - entityIDToComponentIDs[entityID].Add(componentID); - componentIDToEntityID[componentID] = entityID; + entityIDToComponentIDs[entity.ID].Add(componentID); + componentIDToEntityID[componentID] = entity.ID; activeComponents.Add(componentID); - entitiesWithAddedComponents.Add(entityID); + entitiesWithAddedComponents.Add(entity.ID); return componentID; } - internal Guid AddDrawComponent(Guid entityID, TComponent component, int layer = 0) where TComponent : struct, IComponent + internal Guid AddDrawComponent(Entity entity, Guid componentID, TComponent component, int layer = 0) where TComponent : struct, IComponent { - var componentID = AddComponent(entityID, component); + AddComponent(entity, componentID, component); drawLayerManager.RegisterComponentWithLayer(componentID, layer); return componentID; } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index f94f519..4386fe6 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -46,12 +46,6 @@ namespace Encompass { receiveTypes.UnionWith(readsAttribute.readTypes); } - - var readsPendingAttribute = GetType().GetCustomAttribute(false); - if (readsPendingAttribute != null) - { - receiveTypes.UnionWith(readsPendingAttribute.readPendingTypes); - } } internal void AssignEntityManager(EntityManager entityManager) @@ -113,57 +107,55 @@ namespace Encompass protected Guid AddComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { - if (!sendTypes.Contains(typeof(PendingComponentMessage))) + var componentID = componentManager.NextID(); + + componentManager.AddComponent(entity, componentID, component); + + if (sendTypes.Contains(typeof(PendingComponentMessage))) { - throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name); + PendingComponentMessage componentMessage; + componentMessage.entity = entity; + componentMessage.componentID = componentID; + componentMessage.component = component; + SendMessage(componentMessage); } - var componentID = componentManager.AddComponent(entity.ID, component); - - PendingComponentMessage componentMessage; - componentMessage.entity = entity; - componentMessage.componentID = componentID; - componentMessage.component = component; - SendMessage(componentMessage); - return componentID; } protected Guid AddDrawComponent(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent { - if (!sendTypes.Contains(typeof(PendingComponentMessage))) + var componentID = componentManager.NextID(); + + componentManager.AddDrawComponent(entity, componentID, component); + + if (sendTypes.Contains(typeof(PendingComponentMessage))) { - throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name); + PendingComponentMessage newComponentMessage; + newComponentMessage.entity = entity; + newComponentMessage.componentID = componentID; + newComponentMessage.component = component; + SendMessage(newComponentMessage); } - var componentID = componentManager.AddDrawComponent(entity.ID, component, layer); - - PendingComponentMessage newComponentMessage; - newComponentMessage.entity = entity; - newComponentMessage.componentID = componentID; - newComponentMessage.component = component; - SendMessage(newComponentMessage); - return componentID; } protected void ActivateComponent(Guid componentID) where TComponent : struct, IComponent { - if (!sendTypes.Contains(typeof(PendingComponentMessage))) - { - throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name); - } - var entity = GetEntity(componentManager.GetEntityIDByComponentID(componentID)); var component = GetComponentByID(componentID); - PendingComponentMessage newComponentMessage; - newComponentMessage.entity = entity; - newComponentMessage.componentID = componentID; - newComponentMessage.component = component; - SendMessage(newComponentMessage); + if (sendTypes.Contains(typeof(PendingComponentMessage))) + { + PendingComponentMessage newComponentMessage; + newComponentMessage.entity = entity; + newComponentMessage.componentID = componentID; + newComponentMessage.component = component; + SendMessage(newComponentMessage); - componentManager.Activate(componentID); // TODO: actually delay this to end of frame, make sure to update after activate + componentManager.Activate(componentID); + } } protected void DeactivateComponent(Guid componentID) @@ -189,14 +181,6 @@ 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); @@ -248,6 +232,11 @@ 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/encompass-cs/Messages/ComponentMessage.cs b/encompass-cs/Messages/ComponentMessage.cs index 1805a67..c1b74ee 100644 --- a/encompass-cs/Messages/ComponentMessage.cs +++ b/encompass-cs/Messages/ComponentMessage.cs @@ -2,7 +2,7 @@ using System; namespace Encompass { - public struct ComponentMessage : IMessage where TComponent : struct, IComponent + internal struct ComponentMessage : IMessage where TComponent : struct, IComponent { public Entity entity; public Guid componentID; diff --git a/encompass-cs/Messages/ComponentUpdateMessage.cs b/encompass-cs/Messages/ComponentUpdateMessage.cs index b63b894..0221bbb 100644 --- a/encompass-cs/Messages/ComponentUpdateMessage.cs +++ b/encompass-cs/Messages/ComponentUpdateMessage.cs @@ -2,7 +2,7 @@ using System; namespace Encompass { - public struct ComponentUpdateMessage : IMessage where TComponent : struct, IComponent + internal struct ComponentUpdateMessage : IMessage where TComponent : struct, IComponent { public Guid componentID; public TComponent component; diff --git a/encompass-cs/Messages/PendingComponentMessage.cs b/encompass-cs/Messages/PendingComponentMessage.cs index 8cc765a..5fd0166 100644 --- a/encompass-cs/Messages/PendingComponentMessage.cs +++ b/encompass-cs/Messages/PendingComponentMessage.cs @@ -2,7 +2,7 @@ using System; namespace Encompass { - public struct PendingComponentMessage : IMessage where TComponent : struct, IComponent + internal struct PendingComponentMessage : IMessage where TComponent : struct, IComponent { public Entity entity; public Guid componentID; diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 091ce53..47060f3 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -48,12 +48,14 @@ namespace Encompass public Guid AddComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { - return componentManager.AddComponent(entity.ID, component); + var componentID = componentManager.NextID(); + return componentManager.AddComponent(entity, componentID, component); } public Guid AddDrawComponent(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent { - return componentManager.AddDrawComponent(entity.ID, component, layer); + var componentID = componentManager.NextID(); + return componentManager.AddDrawComponent(entity, componentID, component, layer); } public void DeactivateComponent(Guid componentID) diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 58cfaaf..5e49e4e 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -124,7 +124,6 @@ namespace Tests } [Reads(typeof(MockComponent))] - [ReadsPending(typeof(MockComponent))] class HasMockComponentEngine : Engine { private Entity entity; @@ -482,7 +481,7 @@ namespace Tests } [Receives(typeof(CheckHasMockComponentMessage))] - [ReadsPending(typeof(MockComponent))] + [Reads(typeof(MockComponent))] class CheckHasPendingMockComponentEngine : Engine { public override void Update(double dt)