diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 027830e..ba5861c 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -8,9 +8,8 @@ namespace Encompass { public abstract class Engine { - internal readonly List mutateComponentTypes = new List(); - internal readonly List emitMessageTypes = new List(); - internal readonly List readMessageTypes = new List(); + internal readonly List writeTypes = new List(); + internal readonly List readTypes = new List(); private EntityManager entityManager; private ComponentManager componentManager; @@ -18,22 +17,16 @@ namespace Encompass protected Engine() { - var mutatesAttribute = GetType().GetCustomAttribute(false); - if (mutatesAttribute != null) - { - mutateComponentTypes = mutatesAttribute.mutateComponentTypes; - } - - var emitsAttribute = GetType().GetCustomAttribute(false); + var emitsAttribute = GetType().GetCustomAttribute(false); if (emitsAttribute != null) { - emitMessageTypes = emitsAttribute.emitMessageTypes; + writeTypes = emitsAttribute.writeTypes; } var readsAttribute = GetType().GetCustomAttribute(false); if (readsAttribute != null) { - readMessageTypes = readsAttribute.readMessageTypes; + readTypes = readsAttribute.readTypes; } } @@ -81,6 +74,7 @@ namespace Encompass protected TComponent GetComponentByID(Guid componentID) where TComponent : struct, IComponent { + if (componentManager.GetComponentTypeByID(componentID) == typeof(TComponent)) { return (TComponent)componentManager.GetComponentByID(componentID); @@ -103,13 +97,13 @@ namespace Encompass internal void UpdateComponentInWorld(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent { - if (mutateComponentTypes.Contains(typeof(TComponent))) + if (writeTypes.Contains(typeof(TComponent))) { componentManager.UpdateComponent(componentID, newComponent); } else { - throw new IllegalComponentMutationException("Engine {0} tried to mutate undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name); + throw new IllegalComponentMutationException("Engine {0} tried to write undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name); } } @@ -120,7 +114,7 @@ namespace Encompass protected void EmitMessage(TMessage message) where TMessage : struct, IMessage { - if (emitMessageTypes.Contains(typeof(TMessage))) + if (writeTypes.Contains(typeof(TMessage))) { messageManager.AddMessage(message); } @@ -132,7 +126,7 @@ namespace Encompass protected IEnumerable ReadMessages() where TMessage : struct, IMessage { - if (readMessageTypes.Contains(typeof(TMessage))) + if (readTypes.Contains(typeof(TMessage))) { return messageManager.GetMessagesByType(); } @@ -144,7 +138,7 @@ namespace Encompass protected bool Some() where TMessage : struct, IMessage { - if (readMessageTypes.Contains(typeof(TMessage))) + if (readTypes.Contains(typeof(TMessage))) { return messageManager.GetMessagesByType().Any(); } diff --git a/encompass-cs/Engines/Detector.cs b/encompass-cs/Engines/Detector.cs deleted file mode 100644 index 4ead6ac..0000000 --- a/encompass-cs/Engines/Detector.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Reflection; -using System.Collections.Generic; -using Encompass.Exceptions; - -namespace Encompass.Engines -{ - public abstract class Detector : Engine, IEntityTracker - { - private readonly List componentTypes = new List(); - private readonly EntityTracker entityTracker = new EntityTracker(); - - public IEnumerable ComponentTypes { get { return componentTypes; } } - - protected Detector() : base() - { - var detectsAttribute = GetType().GetCustomAttribute(false); - if (detectsAttribute != null) - { - componentTypes = detectsAttribute.componentTypes; - } - else - { - throw new DetectorWithoutComponentTypesException("Detector {0} does not have any component types declared. Use the Detects attribute to declare component types", GetType().Name); - } - } - - public override void Update(double dt) - { - foreach (var id in entityTracker.TrackedEntityIDs) - { - Detect(GetEntity(id), dt); - } - } - - public abstract void Detect(Entity entity, double dt); - - public bool CheckAndTrackEntity(Guid entityID) - { - var entity = GetEntity(entityID); - var shouldTrack = CheckEntity(entity); - if (shouldTrack) { entityTracker.TrackEntity(entityID); } - return shouldTrack; - } - - public bool CheckAndUntrackEntity(Guid entityID) - { - var entity = GetEntity(entityID); - var shouldUntrack = !CheckEntity(entity); - if (shouldUntrack) { entityTracker.UntrackEntity(entityID); } - return shouldUntrack; - } - - private bool CheckEntity(Entity entity) - { - return EntityChecker.CheckEntity(entity, componentTypes); - } - } -} diff --git a/encompass-cs/Engines/Spawner.cs b/encompass-cs/Engines/Spawner.cs index c91e4bf..b4c1901 100644 --- a/encompass-cs/Engines/Spawner.cs +++ b/encompass-cs/Engines/Spawner.cs @@ -9,10 +9,10 @@ namespace Encompass.Engines var readsAttribute = GetType().GetCustomAttribute(false); if (readsAttribute != null) { - readsAttribute.readMessageTypes.Add(typeof(TMessage)); + readsAttribute.readTypes.Add(typeof(TMessage)); } - readMessageTypes.Add(typeof(TMessage)); + readTypes.Add(typeof(TMessage)); } public override void Update(double dt) diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 849dfa4..6c4edf1 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -55,7 +55,7 @@ namespace Encompass entityManager.RegisterEntityTracker(engine as IEntityTracker); } - foreach (var emitMessageType in engine.emitMessageTypes) + foreach (var emitMessageType in engine.writeTypes) { if (!messageTypeToEmitters.ContainsKey(emitMessageType)) { @@ -73,7 +73,7 @@ namespace Encompass } } - foreach (var readMessageType in engine.readMessageTypes) + foreach (var readMessageType in engine.readTypes) { if (!messageTypeToReaders.ContainsKey(readMessageType)) { @@ -140,37 +140,40 @@ namespace Encompass foreach (var engine in engines) { - var mutateAttribute = engine.GetType().GetCustomAttribute(false); - if (mutateAttribute != null) + var writeAttribute = engine.GetType().GetCustomAttribute(false); + if (writeAttribute != null) { - foreach (var mutateComponentType in engine.GetType().GetCustomAttribute(false).mutateComponentTypes) + foreach (var writeType in writeAttribute.writeTypes) { - if (mutatedComponentTypes.Contains(mutateComponentType)) + if (writeType.GetInterfaces().Contains(typeof(IComponent))) // if our write type is a component { - duplicateMutations.Add(mutateComponentType); - } - else - { - mutatedComponentTypes.Add(mutateComponentType); - } + if (mutatedComponentTypes.Contains(writeType)) + { + duplicateMutations.Add(writeType); + } + else + { + mutatedComponentTypes.Add(writeType); + } - if (!componentToEngines.ContainsKey(mutateComponentType)) - { - componentToEngines[mutateComponentType] = new List(); - } + if (!componentToEngines.ContainsKey(writeType)) + { + componentToEngines[writeType] = new List(); + } - componentToEngines[mutateComponentType].Add(engine); + componentToEngines[writeType].Add(engine); + } } } } if (duplicateMutations.Count > 0) { - var errorString = "Multiple Engines mutate the same Component: "; + var errorString = "Multiple Engines write the same Component: "; foreach (var componentType in duplicateMutations) { errorString += "\n" + - componentType.Name + " mutated by: " + + componentType.Name + " written by: " + string.Join(", ", componentToEngines[componentType].Select((engine) => engine.GetType().Name)); } diff --git a/encompass-cs/attributes/Emits.cs b/encompass-cs/attributes/Emits.cs index ff6082f..75ab92d 100644 --- a/encompass-cs/attributes/Emits.cs +++ b/encompass-cs/attributes/Emits.cs @@ -4,13 +4,13 @@ using System.Collections.Generic; namespace Encompass { [AttributeUsage(AttributeTargets.Class)] - public class Emits : Attribute + public class Writes : Attribute { - public readonly List emitMessageTypes; + public readonly List writeTypes; - public Emits(params Type[] emitMessageTypes) + public Writes(params Type[] writeTypes) { - this.emitMessageTypes = new List(emitMessageTypes); + this.writeTypes = new List(writeTypes); } } } diff --git a/encompass-cs/attributes/Mutates.cs b/encompass-cs/attributes/Mutates.cs deleted file mode 100644 index ed147b6..0000000 --- a/encompass-cs/attributes/Mutates.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Encompass -{ - [System.AttributeUsage(System.AttributeTargets.Class)] - public class Mutates : System.Attribute - { - public readonly List mutateComponentTypes; - - public Mutates(params Type[] mutateComponentTypes) - { - this.mutateComponentTypes = new List(mutateComponentTypes); - } - } -} diff --git a/encompass-cs/attributes/Reads.cs b/encompass-cs/attributes/Reads.cs index d738d20..0ca3ee6 100644 --- a/encompass-cs/attributes/Reads.cs +++ b/encompass-cs/attributes/Reads.cs @@ -6,11 +6,11 @@ namespace Encompass [System.AttributeUsage(System.AttributeTargets.Class)] public class Reads : System.Attribute { - public readonly List readMessageTypes; + public readonly List readTypes; public Reads(params Type[] readMessageTypes) { - this.readMessageTypes = new List(readMessageTypes); + this.readTypes = new List(readMessageTypes); } } } diff --git a/test/DetectorTest.cs b/test/DetectorTest.cs deleted file mode 100644 index 174ef2f..0000000 --- a/test/DetectorTest.cs +++ /dev/null @@ -1,123 +0,0 @@ -using NUnit.Framework; -using FluentAssertions; - -using Encompass; -using Encompass.Engines; -using Encompass.Exceptions; -using System; -using System.Collections.Generic; - -namespace Tests -{ - class DetectorTest - { - class NoComponentTypesDetector : Detector - { - public override void Detect(Entity entity, double dt) { } - } - - [Test] - public void DetectorWithNoComponentTypes() - { - var worldBuilder = new WorldBuilder(); - - Action addEngine = () => worldBuilder.AddEngine(new NoComponentTypesDetector()); - addEngine.Should().Throw(); - } - - struct AComponent : IComponent { } - struct BComponent : IComponent { } - struct CComponent : IComponent { } - - static List trackedEntities = new List(); - - [Detects(typeof(AComponent), typeof(BComponent))] - class TestDetector : Detector - { - public override void Detect(Entity entity, double dt) - { - trackedEntities.Add(entity); - } - } - - [Test] - public void CheckAndTrackEntities() - { - var worldBuilder = new WorldBuilder(); - var detector = worldBuilder.AddEngine(new TestDetector()); - - var entityToTrack = worldBuilder.CreateEntity(); - entityToTrack.AddComponent(new AComponent()); - entityToTrack.AddComponent(new BComponent()); - - var entityNotToTrack = worldBuilder.CreateEntity(); - entityNotToTrack.AddComponent(new AComponent()); - entityNotToTrack.AddComponent(new CComponent()); - - var entityWithDeactivatedComponents = worldBuilder.CreateEntity(); - var aComponent = entityWithDeactivatedComponents.AddComponent(new AComponent()); - entityWithDeactivatedComponents.AddComponent(new BComponent()); - entityWithDeactivatedComponents.DeactivateComponent(aComponent); - - var entityWithOneDeactivatedComponent = worldBuilder.CreateEntity(); - var inactiveComponent = entityWithOneDeactivatedComponent.AddComponent(new AComponent()); - entityWithOneDeactivatedComponent.AddComponent(new AComponent()); - entityWithOneDeactivatedComponent.AddComponent(new BComponent()); - entityWithOneDeactivatedComponent.DeactivateComponent(inactiveComponent); - - var world = worldBuilder.Build(); - world.Update(0.01); - - trackedEntities.Should().Contain(entityToTrack); - trackedEntities.Should().NotContain(entityNotToTrack); - trackedEntities.Should().NotContain(entityWithDeactivatedComponents); - trackedEntities.Should().Contain(entityWithOneDeactivatedComponent); - } - - [Test] - public void EntityUntrackedWhenComponentRemoved() - { - var worldBuilder = new WorldBuilder(); - worldBuilder.AddEngine(new TestDetector()); - - var entityToUntrack = worldBuilder.CreateEntity(); - entityToUntrack.AddComponent(new AComponent()); - var bComponent = entityToUntrack.AddComponent(new BComponent()); - - var world = worldBuilder.Build(); - - // have to update twice because we are updating from outside the world - entityToUntrack.RemoveComponent(bComponent); - world.Update(0.01); - - trackedEntities.Clear(); - world.Update(0.01); - - trackedEntities.Should().NotContain(entityToUntrack); - } - - [Test] - public void DetectCalledPerTrackedEntityOnWorldUpdat() - { - var worldBuilder = new WorldBuilder(); - worldBuilder.AddEngine(new TestDetector()); - - var entityOne = worldBuilder.CreateEntity(); - entityOne.AddComponent(new AComponent()); - entityOne.AddComponent(new BComponent()); - - var entityTwo = worldBuilder.CreateEntity(); - entityTwo.AddComponent(new AComponent()); - entityTwo.AddComponent(new BComponent()); - - trackedEntities.Clear(); - - var world = worldBuilder.Build(); - - world.Update(0.01); - - trackedEntities.Should().Contain(entityOne); - trackedEntities.Should().Contain(entityTwo); - } - } -} diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 4c844d3..f47781d 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -110,7 +110,7 @@ namespace Tests Assert.Throws(() => world.Update(0.01f)); } - [Mutates(typeof(MockComponent))] + [Writes(typeof(MockComponent))] public class UpdateComponentTestEngine : Engine { public override void Update(double dt) @@ -178,7 +178,7 @@ namespace Tests var world = worldBuilder.Build(); var ex = Assert.Throws(() => world.Update(0.01f)); - Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to mutate undeclared Component MockComponent")); + Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to write undeclared Component MockComponent")); } struct MockMessage : IMessage @@ -186,7 +186,7 @@ namespace Tests public string myString; } - [Emits(typeof(MockMessage))] + [Writes(typeof(MockMessage))] public class MessageEmitEngine : Engine { public override void Update(double dt) @@ -269,7 +269,7 @@ namespace Tests static bool someTest; - [Emits(typeof(MockMessage))] + [Writes(typeof(MockMessage))] class EmitMockMessageEngine : Engine { public override void Update(double dt) diff --git a/test/SpawnerTest.cs b/test/SpawnerTest.cs index ed924d2..446fe4e 100644 --- a/test/SpawnerTest.cs +++ b/test/SpawnerTest.cs @@ -14,7 +14,7 @@ namespace Tests static Entity resultEntity; - [Emits(typeof(SpawnMessageA))] + [Writes(typeof(SpawnMessageA))] class MessageEmitter : Engine { public override void Update(double dt) diff --git a/test/WorldBuilderTest.cs b/test/WorldBuilderTest.cs index 59efb82..3267281 100644 --- a/test/WorldBuilderTest.cs +++ b/test/WorldBuilderTest.cs @@ -14,7 +14,7 @@ namespace Tests struct BMessage : IMessage { } [Reads(typeof(AMessage))] - [Emits(typeof(BMessage))] + [Writes(typeof(BMessage))] class AEngine : Engine { public override void Update(double dt) @@ -25,7 +25,7 @@ namespace Tests } [Reads(typeof(BMessage))] - [Emits(typeof(AMessage))] + [Writes(typeof(AMessage))] class BEngine : Engine { public override void Update(double dt) @@ -54,7 +54,7 @@ namespace Tests struct DMessage : IMessage { } [Reads(typeof(AMessage))] - [Emits(typeof(BMessage))] + [Writes(typeof(BMessage))] class AEngine : Engine { public override void Update(double dt) @@ -65,7 +65,7 @@ namespace Tests } [Reads(typeof(BMessage))] - [Emits(typeof(CMessage))] + [Writes(typeof(CMessage))] class BEngine : Engine { public override void Update(double dt) @@ -76,7 +76,7 @@ namespace Tests } [Reads(typeof(CMessage))] - [Emits(typeof(DMessage))] + [Writes(typeof(DMessage))] class CEngine : Engine { public override void Update(double dt) @@ -87,7 +87,7 @@ namespace Tests } [Reads(typeof(DMessage))] - [Emits(typeof(AMessage))] + [Writes(typeof(AMessage))] class DEngine : Engine { public override void Update(double dt) @@ -114,13 +114,13 @@ namespace Tests { struct AComponent : IComponent { } - [Mutates(typeof(AComponent))] + [Writes(typeof(AComponent))] class AEngine : Engine { public override void Update(double dt) { } } - [Mutates(typeof(AComponent))] + [Writes(typeof(AComponent))] class BEngine : Engine { public override void Update(double dt) { } @@ -149,8 +149,7 @@ namespace Tests struct CMessage : IMessage { } struct DMessage : IMessage { } - [Mutates(typeof(AComponent))] - [Emits(typeof(AMessage))] + [Writes(typeof(AComponent), typeof(AMessage))] class AEngine : Engine { public override void Update(double dt) @@ -159,8 +158,7 @@ namespace Tests } } - [Mutates(typeof(BComponent))] - [Emits(typeof(BMessage))] + [Writes(typeof(BComponent), typeof(BMessage))] class BEngine : Engine { public override void Update(double dt) @@ -170,7 +168,7 @@ namespace Tests } [Reads(typeof(AMessage), typeof(BMessage))] - [Emits(typeof(DMessage))] + [Writes(typeof(DMessage))] class CEngine : Engine { public override void Update(double dt)