diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index 60396ea..ffb4eda 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace Encompass { @@ -69,11 +68,6 @@ namespace Encompass return Lookup().Count > 0; } - // public IEnumerable All() where TComponent : struct, IComponent - // { - // return Lookup().All(); - // } - public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped() { foreach (var store in Stores.Values) diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index bcd07be..c0d53e3 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -6,15 +6,15 @@ namespace Encompass internal class ComponentManager { private readonly DrawLayerManager drawLayerManager; - private readonly ComponentMessageManager componentMessageManager; + private readonly ComponentUpdateManager componentUpdateManager; private readonly ComponentStore componentStore = new ComponentStore(); private readonly HashSet entitiesMarkedForRemoval = new HashSet(); - public ComponentManager(DrawLayerManager drawLayerManager, ComponentMessageManager componentMessageManager) + public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager) { this.drawLayerManager = drawLayerManager; - this.componentMessageManager = componentMessageManager; + this.componentUpdateManager = componentUpdateManager; } internal void SetComponentStore(ComponentStore componentStore) @@ -29,12 +29,12 @@ namespace Encompass internal void AddComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { - componentStore.Set(entity, component); + componentStore.Set(entity, component); } internal void WriteComponents() { - componentStore.SwapWith(componentMessageManager.UpToDateComponentStore); + componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore); } internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity() where TComponent : struct, IComponent @@ -80,8 +80,7 @@ namespace Encompass public void Remove(Entity entity) where TComponent : struct, IComponent { - //componentStore.Remove(entity); - componentMessageManager.Remove(entity); + componentUpdateManager.Remove(entity); drawLayerManager.UnRegisterComponentWithLayer(entity); } } diff --git a/encompass-cs/ComponentMessageManager.cs b/encompass-cs/ComponentUpdateManager.cs similarity index 86% rename from encompass-cs/ComponentMessageManager.cs rename to encompass-cs/ComponentUpdateManager.cs index ab93fec..3f27029 100644 --- a/encompass-cs/ComponentMessageManager.cs +++ b/encompass-cs/ComponentUpdateManager.cs @@ -1,25 +1,21 @@ using System; using System.Collections.Generic; using System.Linq; -using Encompass.Exceptions; namespace Encompass { - class ComponentMessageManager + internal class ComponentUpdateManager { - private readonly ComponentStore componentStore = new ComponentStore(); - + private readonly ComponentStore existingAndPendingComponentStore = new ComponentStore(); private readonly ComponentStore existingComponentStore = new ComponentStore(); private readonly ComponentStore pendingComponentStore = new ComponentStore(); - private ComponentStore upToDateComponentStore = new ComponentStore(); - private readonly Dictionary> typeToEntityToPendingComponentPriority = new Dictionary>(128); - public ComponentStore UpToDateComponentStore { get => upToDateComponentStore; } + public ComponentStore UpToDateComponentStore { get; private set; } = new ComponentStore(); - internal void ClearMessages() + internal void Clear() { - componentStore.ClearAll(); + existingAndPendingComponentStore.ClearAll(); existingComponentStore.ClearAll(); pendingComponentStore.ClearAll(); UpToDateComponentStore.ClearAll(); @@ -32,7 +28,7 @@ namespace Encompass internal void SetStartingComponentStore(ComponentStore componentStore) { - upToDateComponentStore = componentStore; + UpToDateComponentStore = componentStore; } internal void AddExistingComponent(Entity entity, TComponent component) where TComponent : struct, IComponent @@ -52,20 +48,20 @@ namespace Encompass private void RegisterExistingOrPendingComponentMessage(Entity entity, TComponent component) where TComponent : struct, IComponent { - componentStore.Set(entity, component); - upToDateComponentStore.Set(entity, component); + existingAndPendingComponentStore.Set(entity, component); + UpToDateComponentStore.Set(entity, component); } public void UpdateComponent(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent { - upToDateComponentStore.Set(entity, component, priority); + UpToDateComponentStore.Set(entity, component, priority); } // general component reads by type internal IEnumerable<(Entity, TComponent)> ReadExistingAndPendingComponentsByType() where TComponent : struct, IComponent { - return componentStore.All(); + return existingAndPendingComponentStore.All(); } internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType() where TComponent : struct, IComponent @@ -102,7 +98,7 @@ namespace Encompass internal bool SomeExistingOrPendingComponent() where TComponent : struct, IComponent { - return componentStore.Any(); + return existingAndPendingComponentStore.Any(); } internal bool SomeExistingComponent() where TComponent : struct, IComponent @@ -131,12 +127,12 @@ namespace Encompass internal bool HasExistingOrPendingComponent(Entity entity) where TComponent : struct, IComponent { - return componentStore.Has(entity); + return existingAndPendingComponentStore.Has(entity); } internal bool HasExistingOrPendingComponent(Entity entity, Type type) { - return componentStore.Has(type, entity); + return existingAndPendingComponentStore.Has(type, entity); } internal bool HasExistingComponent(Entity entity) where TComponent : struct, IComponent @@ -161,7 +157,7 @@ namespace Encompass internal void Remove(Entity entity) where TComponent : struct, IComponent { - upToDateComponentStore.Remove(entity); + UpToDateComponentStore.Remove(entity); } } } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index ea22c3f..268be45 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -28,14 +28,11 @@ namespace Encompass /// internal bool usesTimeDilation = true; public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; } - /// - /// Used when activating time dilation. Lower priority overrides higher priority. - /// private EntityManager entityManager; private MessageManager messageManager; private ComponentManager componentManager; - private ComponentMessageManager componentMessageManager; + private ComponentUpdateManager componentUpdateManager; private TimeManager timeManager; protected Engine() @@ -81,9 +78,9 @@ namespace Encompass public override bool Equals(object obj) { - if (obj is Engine) + if (obj is Engine engine) { - return this.Equals((Engine)obj); + return Equals(engine); } return false; @@ -114,9 +111,9 @@ namespace Encompass this.messageManager = messageManager; } - internal void AssignComponentMessageManager(ComponentMessageManager componentMessageManager) + internal void AssignComponentUpdateManager(ComponentUpdateManager componentUpdateManager) { - this.componentMessageManager = componentMessageManager; + this.componentUpdateManager = componentUpdateManager; } internal void AssignTimeManager(TimeManager timeManager) @@ -186,15 +183,15 @@ namespace Encompass var existingRead = readTypes.Contains(typeof(TComponent)); if (existingRead && pendingRead) { - return componentMessageManager.ReadExistingAndPendingComponentsByType(); + return componentUpdateManager.ReadExistingAndPendingComponentsByType(); } else if (existingRead) { - return componentMessageManager.ReadExistingComponentsByType(); + return componentUpdateManager.ReadExistingComponentsByType(); } else if (pendingRead) { - return componentMessageManager.ReadPendingComponentsByType(); + return componentUpdateManager.ReadPendingComponentsByType(); } else { @@ -229,15 +226,15 @@ namespace Encompass var existingRead = readTypes.Contains(typeof(TComponent)); if (existingRead && pendingRead) { - return componentMessageManager.ReadFirstExistingOrPendingComponentByType(); + return componentUpdateManager.ReadFirstExistingOrPendingComponentByType(); } else if (existingRead) { - return componentMessageManager.ReadFirstExistingComponentByType(); + return componentUpdateManager.ReadFirstExistingComponentByType(); } else if (pendingRead) { - return componentMessageManager.ReadFirstPendingComponentByType(); + return componentUpdateManager.ReadFirstPendingComponentByType(); } else { @@ -271,15 +268,15 @@ namespace Encompass var existingRead = readTypes.Contains(typeof(TComponent)); if (existingRead && pendingRead) { - return componentMessageManager.SomeExistingOrPendingComponent(); + return componentUpdateManager.SomeExistingOrPendingComponent(); } else if (existingRead) { - return componentMessageManager.SomeExistingComponent(); + return componentUpdateManager.SomeExistingComponent(); } else if (pendingRead) { - return componentMessageManager.SomePendingComponent(); + return componentUpdateManager.SomePendingComponent(); } else { @@ -293,13 +290,13 @@ namespace Encompass var existingRead = readTypes.Contains(typeof(TComponent)); if (existingRead && pendingRead) { - if (componentMessageManager.HasPendingComponent(entity)) + if (componentUpdateManager.HasPendingComponent(entity)) { - return componentMessageManager.ReadPendingComponentByEntityAndType(entity); + return componentUpdateManager.ReadPendingComponentByEntityAndType(entity); } - else if (componentMessageManager.HasExistingComponent(entity)) + else if (componentUpdateManager.HasExistingComponent(entity)) { - return componentMessageManager.ReadExistingComponentByEntityAndType(entity); + return componentUpdateManager.ReadExistingComponentByEntityAndType(entity); } else { @@ -308,11 +305,11 @@ namespace Encompass } else if (existingRead) { - return componentMessageManager.ReadExistingComponentByEntityAndType(entity); + return componentUpdateManager.ReadExistingComponentByEntityAndType(entity); } else if (pendingRead) { - return componentMessageManager.ReadPendingComponentByEntityAndType(entity); + return componentUpdateManager.ReadPendingComponentByEntityAndType(entity); } else { @@ -347,15 +344,15 @@ namespace Encompass if (pendingRead && existingRead) { - return componentMessageManager.HasExistingOrPendingComponent(entity); + return componentUpdateManager.HasExistingOrPendingComponent(entity); } else if (existingRead) { - return componentMessageManager.HasExistingComponent(entity); + return componentUpdateManager.HasExistingComponent(entity); } else if (pendingRead) { - return componentMessageManager.HasPendingComponent(entity); + return componentUpdateManager.HasPendingComponent(entity); } else { @@ -376,15 +373,15 @@ namespace Encompass if (pendingRead && existingRead) { - return componentMessageManager.HasExistingOrPendingComponent(entity, type); + return componentUpdateManager.HasExistingOrPendingComponent(entity, type); } else if (existingRead) { - return componentMessageManager.HasExistingComponent(entity, type); + return componentUpdateManager.HasExistingComponent(entity, type); } else if (pendingRead) { - return componentMessageManager.HasPendingComponent(entity, type); + return componentUpdateManager.HasPendingComponent(entity, type); } else { @@ -413,7 +410,7 @@ namespace Encompass } else { - componentMessageManager.UpdateComponent(entity, component, priority); + componentUpdateManager.UpdateComponent(entity, component, priority); } if (component is IDrawableComponent drawableComponent) @@ -471,12 +468,12 @@ namespace Encompass internal void AddExistingComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { - componentMessageManager.AddExistingComponent(entity, component); + componentUpdateManager.AddExistingComponent(entity, component); } internal void AddPendingComponent(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent { - componentMessageManager.AddPendingComponent(entity, component, priority); + componentUpdateManager.AddPendingComponent(entity, component, priority); } /// diff --git a/encompass-cs/Entity.cs b/encompass-cs/Entity.cs index fa584e3..a08954c 100644 --- a/encompass-cs/Entity.cs +++ b/encompass-cs/Entity.cs @@ -12,14 +12,14 @@ namespace Encompass internal Entity(Guid id) { - this.ID = id; + ID = id; } public override bool Equals(object obj) { - if (obj is Entity) + if (obj is Entity entity) { - return this.Equals((Entity)obj); + return Equals(entity); } return false; diff --git a/encompass-cs/RenderManager.cs b/encompass-cs/RenderManager.cs index bcf07eb..cea10c0 100644 --- a/encompass-cs/RenderManager.cs +++ b/encompass-cs/RenderManager.cs @@ -5,21 +5,13 @@ namespace Encompass { internal class RenderManager { - private readonly ComponentManager componentManager; private readonly DrawLayerManager drawLayerManager; - private readonly EntityManager entityManager; private readonly Dictionary> drawComponentTypeToOrderedRenderer = new Dictionary>(256); - public RenderManager( - ComponentManager componentManager, - DrawLayerManager drawLayerManager, - EntityManager entityManager - ) + public RenderManager(DrawLayerManager drawLayerManager) { - this.componentManager = componentManager; this.drawLayerManager = drawLayerManager; - this.entityManager = entityManager; } public void RegisterOrderedRenderer(Action renderAction) where TComponent : struct, IComponent diff --git a/encompass-cs/TimeDilationData.cs b/encompass-cs/TimeDilationData.cs index 4939f43..b344589 100644 --- a/encompass-cs/TimeDilationData.cs +++ b/encompass-cs/TimeDilationData.cs @@ -14,23 +14,21 @@ namespace Encompass { get { - double calculatedFactor = 1; - if (elapsedTime < easeInTime) { - calculatedFactor = easeInFunction(elapsedTime, 1, factor - 1, easeInTime); + return easeInFunction(elapsedTime, 1, factor - 1, easeInTime); } else if (elapsedTime < easeInTime + activeTime) { - calculatedFactor = factor; + return factor; } else if (elapsedTime < easeInTime + activeTime + easeOutTime) { var elapsedOutTime = elapsedTime - easeInTime - activeTime; - calculatedFactor = easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime); + return easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime); } - return calculatedFactor; + return 1; } } } diff --git a/encompass-cs/TimeManager.cs b/encompass-cs/TimeManager.cs index 79cfe8f..d29ac10 100644 --- a/encompass-cs/TimeManager.cs +++ b/encompass-cs/TimeManager.cs @@ -9,7 +9,7 @@ namespace Encompass private double Linear(double t, double b, double c, double d) { - return c * t / d + b; + return (c * t / d) + b; } public double TimeDilationFactor diff --git a/encompass-cs/World.cs b/encompass-cs/World.cs index 783e251..1154ca3 100644 --- a/encompass-cs/World.cs +++ b/encompass-cs/World.cs @@ -11,7 +11,7 @@ namespace Encompass private readonly EntityManager entityManager; private readonly ComponentManager componentManager; private readonly MessageManager messageManager; - private readonly ComponentMessageManager componentMessageManager; + private readonly ComponentUpdateManager componentUpdateManager; private readonly TimeManager timeManager; private readonly RenderManager renderManager; @@ -20,7 +20,7 @@ namespace Encompass EntityManager entityManager, ComponentManager componentManager, MessageManager messageManager, - ComponentMessageManager componentMessageManager, + ComponentUpdateManager componentUpdateManager, TimeManager timeManager, RenderManager renderManager ) @@ -29,7 +29,7 @@ namespace Encompass this.entityManager = entityManager; this.componentManager = componentManager; this.messageManager = messageManager; - this.componentMessageManager = componentMessageManager; + this.componentUpdateManager = componentUpdateManager; this.timeManager = timeManager; this.renderManager = renderManager; } @@ -61,7 +61,7 @@ namespace Encompass componentManager.WriteComponents(); componentManager.RemoveMarkedComponents(); - componentMessageManager.ClearMessages(); + componentUpdateManager.Clear(); } /// diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index dda5ee2..d8e678e 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -21,12 +21,12 @@ namespace Encompass private readonly List engines = new List(); private readonly DirectedGraph engineGraph = GraphBuilder.DirectedGraph(); private readonly ComponentStore startingComponentStoreForComponentManager = new ComponentStore(); - private readonly ComponentStore startingComponentStoreForComponentMessageManager = new ComponentStore(); + private readonly ComponentStore startingComponentStoreForComponentUpdateManager = new ComponentStore(); private readonly ComponentManager componentManager; private readonly EntityManager entityManager; private readonly MessageManager messageManager; - private readonly ComponentMessageManager componentMessageManager; + private readonly ComponentUpdateManager componentUpdateManager; private readonly TimeManager timeManager; private readonly DrawLayerManager drawLayerManager; private readonly RenderManager renderManager; @@ -41,11 +41,11 @@ namespace Encompass { drawLayerManager = new DrawLayerManager(); timeManager = new TimeManager(); - componentMessageManager = new ComponentMessageManager(); - componentManager = new ComponentManager(drawLayerManager, componentMessageManager); + componentUpdateManager = new ComponentUpdateManager(); + componentManager = new ComponentManager(drawLayerManager, componentUpdateManager); messageManager = new MessageManager(timeManager); entityManager = new EntityManager(componentManager); - renderManager = new RenderManager(componentManager, drawLayerManager, entityManager); + renderManager = new RenderManager(drawLayerManager); } /// @@ -78,7 +78,9 @@ namespace Encompass public void SetComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { startingComponentStoreForComponentManager.Set(entity, component); - startingComponentStoreForComponentMessageManager.Set(entity, component); + startingComponentStoreForComponentUpdateManager.Set(entity, component); + + RegisterComponent(typeof(TComponent)); if (component is IDrawableComponent drawableComponent) { @@ -101,7 +103,7 @@ namespace Encompass engine.AssignEntityManager(entityManager); engine.AssignComponentManager(componentManager); engine.AssignMessageManager(messageManager); - engine.AssignComponentMessageManager(componentMessageManager); + engine.AssignComponentUpdateManager(componentUpdateManager); engine.AssignTimeManager(timeManager); engines.Add(engine); @@ -130,11 +132,11 @@ namespace Encompass senders.Add(engine); } - foreach (var readTypes in engine.readTypes) + foreach (var componentType in engine.readTypes.Union(engine.writeTypes)) { - if (!registeredComponentTypes.Contains(readTypes)) + if (!registeredComponentTypes.Contains(componentType)) { - RegisterComponent(readTypes); + RegisterComponent(componentType); } } @@ -335,13 +337,13 @@ namespace Encompass entityManager, componentManager, messageManager, - componentMessageManager, + componentUpdateManager, timeManager, renderManager ); + componentUpdateManager.SetStartingComponentStore(startingComponentStoreForComponentUpdateManager); componentManager.SetComponentStore(startingComponentStoreForComponentManager); - componentMessageManager.SetStartingComponentStore(startingComponentStoreForComponentMessageManager); return world; } diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index ef3e9f8..febbd0b 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -1,12 +1,7 @@ -using System.ComponentModel; using NUnit.Framework; using FluentAssertions; using Encompass; -using System.Collections.Generic; -using System; -using System.Linq; -using Encompass.Exceptions; namespace Tests {