diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 6ffb8a6..5db7d12 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -85,6 +85,7 @@ namespace Encompass { //componentStore.Remove(entity); componentMessageManager.Remove(entity); + drawLayerManager.UnRegisterComponentWithLayer(entity); } private void Remove(Entity entity) diff --git a/encompass-cs/ComponentMessageManager.cs b/encompass-cs/ComponentMessageManager.cs index 06137b9..9c37d0b 100644 --- a/encompass-cs/ComponentMessageManager.cs +++ b/encompass-cs/ComponentMessageManager.cs @@ -11,7 +11,7 @@ namespace Encompass private readonly ComponentStore existingComponentStore = new ComponentStore(); private readonly ComponentStore pendingComponentStore = new ComponentStore(); - private readonly ComponentStore upToDateComponentStore = new ComponentStore(); + private ComponentStore upToDateComponentStore = new ComponentStore(); private readonly Dictionary> typeToEntityToPendingComponentPriority = new Dictionary>(); @@ -30,6 +30,11 @@ namespace Encompass } } + internal void SetStartingComponentStore(ComponentStore componentStore) + { + upToDateComponentStore = componentStore; + } + internal void AddExistingComponentMessage(ComponentMessage componentMessage) where TComponent : struct, IComponent { RegisterExistingOrPendingComponentMessage(componentMessage.entity, componentMessage.component); diff --git a/encompass-cs/DrawLayerManager.cs b/encompass-cs/DrawLayerManager.cs index aab17b2..c116bb7 100644 --- a/encompass-cs/DrawLayerManager.cs +++ b/encompass-cs/DrawLayerManager.cs @@ -7,7 +7,6 @@ namespace Encompass internal class DrawLayerManager { private readonly SortedList layerOrder = new SortedList(); - private readonly ComponentStore componentStore = new ComponentStore(); private readonly Dictionary layerIndexToComponentStore = new Dictionary(); private readonly Dictionary> layerIndexToGeneralRenderers = new Dictionary>(); @@ -52,7 +51,12 @@ namespace Encompass public void RegisterComponentWithLayer(Entity entity, TComponent component, int layer) where TComponent : struct, IComponent { - if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entity)) { UnRegisterComponentWithLayer(entity, component); } + if (!typeToEntityToLayer.ContainsKey(typeof(TComponent))) + { + typeToEntityToLayer.Add(typeof(TComponent), new Dictionary()); + } + + if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entity)) { UnRegisterComponentWithLayer(entity); } if (layerIndexToComponentStore.ContainsKey(layer)) { @@ -74,8 +78,10 @@ namespace Encompass } } - public void UnRegisterComponentWithLayer(Entity entity, TComponent component) where TComponent : struct, IComponent + public void UnRegisterComponentWithLayer(Entity entity) where TComponent : struct, IComponent { + if (!typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; } + if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entity)) { var layer = typeToEntityToLayer[typeof(TComponent)][entity]; @@ -86,7 +92,10 @@ namespace Encompass public void UnRegisterEntityWithLayer(Entity entity) { - componentStore.Remove(entity); + foreach (var store in layerIndexToComponentStore.Values) + { + store.Remove(entity); + } } public IEnumerable GeneralRenderersByLayer(int layer) @@ -98,7 +107,9 @@ namespace Encompass public IEnumerable<(Entity, Type, IComponent)> AllInLayer(int layer) { - return layerIndexToComponentStore[layer].AllInterfaceTyped(); + return layerIndexToComponentStore.ContainsKey(layer) ? + layerIndexToComponentStore[layer].AllInterfaceTyped() : + Enumerable.Empty<(Entity, Type, IComponent)>(); } } } diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index a12d53a..a64e97e 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -20,7 +20,8 @@ namespace Encompass { private readonly List engines = new List(); private readonly DirectedGraph engineGraph = GraphBuilder.DirectedGraph(); - private readonly ComponentStore componentStore = new ComponentStore(); + private readonly ComponentStore startingComponentStoreForComponentManager = new ComponentStore(); + private readonly ComponentStore startingComponentStoreForComponentMessageManager = new ComponentStore(); private readonly ComponentManager componentManager; private readonly EntityManager entityManager; @@ -76,7 +77,8 @@ namespace Encompass /// public void SetComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { - componentStore.Set(entity, component); + startingComponentStoreForComponentManager.Set(entity, component); + startingComponentStoreForComponentMessageManager.Set(entity, component); if (component is IDrawableComponent drawableComponent) { @@ -348,7 +350,8 @@ namespace Encompass renderManager ); - componentManager.SetComponentStore(componentStore); + componentManager.SetComponentStore(startingComponentStoreForComponentManager); + componentMessageManager.SetStartingComponentStore(startingComponentStoreForComponentMessageManager); return world; }