get rid of component stores on DrawLayerManager
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-20 00:58:33 -07:00
parent 7f89e9b4a0
commit 63e9ca63c0
8 changed files with 36 additions and 70 deletions

View File

@ -14,14 +14,6 @@ namespace Encompass
ComponentBitSet = new ComponentBitSet(typeToIndex); ComponentBitSet = new ComponentBitSet(typeToIndex);
} }
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
{
foreach (var entry in _stores)
{
yield return (entry.Key, entry.Value);
}
}
public virtual void RegisterComponentType<TComponent>() where TComponent : struct, IComponent public virtual void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{ {
if (!_stores.ContainsKey(typeof(TComponent))) if (!_stores.ContainsKey(typeof(TComponent)))
@ -102,17 +94,6 @@ namespace Encompass
return Lookup<TComponent>().Count > 0; return Lookup<TComponent>().Count > 0;
} }
public IEnumerable<(int, Type, IComponent)> AllInterfaceTyped()
{
foreach (var store in _stores.Values)
{
foreach (var thing in store.AllInterfaceTyped())
{
yield return thing;
}
}
}
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct, IComponent public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct, IComponent
{ {
return Lookup<TComponent>().All(); return Lookup<TComponent>().All();

View File

@ -7,7 +7,6 @@ namespace Encompass
internal abstract class TypedComponentStore internal abstract class TypedComponentStore
{ {
public abstract int Count { get; } public abstract int Count { get; }
public abstract IEnumerable<(int, Type, IComponent)> AllInterfaceTyped();
public abstract bool Has(int entity); public abstract bool Has(int entity);
public abstract bool Remove(int entity, int priority); public abstract bool Remove(int entity, int priority);
public abstract void ForceRemove(int entity); public abstract void ForceRemove(int entity);
@ -107,13 +106,5 @@ namespace Encompass
yield return (_components[kvp.Value], kvp.Key); yield return (_components[kvp.Value], kvp.Key);
} }
} }
public override IEnumerable<(int, Type, IComponent)> AllInterfaceTyped()
{
foreach (var kvp in _indices)
{
yield return (kvp.Key, typeof(TComponent), (IComponent)_components[kvp.Value]);
}
}
} }
} }

View File

@ -47,9 +47,9 @@ namespace Encompass
_upToDateComponentStore.SwapWith(componentStore); _upToDateComponentStore.SwapWith(componentStore);
} }
internal void RegisterDrawableComponent<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : struct, IComponent
{ {
_drawLayerManager.RegisterComponentWithLayer(entityID, component, layer); _drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer);
} }
internal void WriteComponents() internal void WriteComponents()

View File

@ -9,16 +9,14 @@ namespace Encompass
{ {
private readonly SortedList<int, int> _layerOrder = new SortedList<int, int>(); private readonly SortedList<int, int> _layerOrder = new SortedList<int, int>();
private readonly Dictionary<int, ComponentStore> _layerIndexToComponentStore = new Dictionary<int, ComponentStore>(512); private readonly Dictionary<int, Dictionary<Type, HashSet<int>>> _layerIndexToComponentStore = new Dictionary<int, Dictionary<Type, HashSet<int>>>();
private readonly Dictionary<int, HashSet<GeneralRenderer>> _layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512); private readonly Dictionary<int, HashSet<GeneralRenderer>> _layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512);
private readonly Dictionary<Type, Dictionary<int, int>> _typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512); private readonly Dictionary<Type, Dictionary<int, int>> _typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512);
private readonly Dictionary<Type, int> _typeToIndex;
public IEnumerable<int> LayerOrder { get { return _layerOrder.Values; } } public IEnumerable<int> LayerOrder { get { return _layerOrder.Values; } }
public DrawLayerManager(Dictionary<Type, int> typeToIndex) public DrawLayerManager()
{ {
_typeToIndex = typeToIndex;
RegisterDrawLayer(0); RegisterDrawLayer(0);
} }
@ -28,7 +26,7 @@ namespace Encompass
{ {
_layerOrder.Add(layer, layer); _layerOrder.Add(layer, layer);
_layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>()); _layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>());
_layerIndexToComponentStore.Add(layer, new ComponentStore(_typeToIndex)); _layerIndexToComponentStore.Add(layer, new Dictionary<Type, HashSet<int>>());
} }
} }
@ -41,7 +39,10 @@ namespace Encompass
foreach (var pair in _layerIndexToComponentStore) foreach (var pair in _layerIndexToComponentStore)
{ {
pair.Value.RegisterComponentType<TComponent>(); if (!pair.Value.ContainsKey(typeof(TComponent)))
{
pair.Value.Add(typeof(TComponent), new HashSet<int>());
}
} }
} }
@ -52,21 +53,7 @@ namespace Encompass
set.Add(renderer); set.Add(renderer);
} }
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterComponentWithLayer<TComponent>(int entityID, int layer) where TComponent : struct, IComponent
{
if (_layerIndexToGeneralRenderers.ContainsKey(layer))
{
_layerIndexToGeneralRenderers[layer].Remove(renderer);
}
}
public void AdjustRendererLayer(GeneralRenderer renderer, int oldLayer, int newLayer)
{
UnregisterGeneralRendererWithLayer(renderer, oldLayer);
RegisterGeneralRendererWithLayer(renderer, newLayer);
}
public void RegisterComponentWithLayer<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent
{ {
if (!_layerIndexToComponentStore.ContainsKey(layer)) if (!_layerIndexToComponentStore.ContainsKey(layer))
{ {
@ -75,8 +62,8 @@ namespace Encompass
if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) { UnRegisterComponentWithLayer<TComponent>(entityID); } if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) { UnRegisterComponentWithLayer<TComponent>(entityID); }
var set = _layerIndexToComponentStore[layer]; var set = _layerIndexToComponentStore[layer][typeof(TComponent)];
set.Set<TComponent>(entityID, component); set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer); _typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
} }
@ -88,7 +75,7 @@ namespace Encompass
if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{ {
var layer = _typeToEntityToLayer[typeof(TComponent)][entityID]; var layer = _typeToEntityToLayer[typeof(TComponent)][entityID];
_layerIndexToComponentStore[layer].ForceRemove<TComponent>(entityID); _layerIndexToComponentStore[layer][typeof(TComponent)].Remove(entityID);
} }
_typeToEntityToLayer[typeof(TComponent)].Remove(entityID); _typeToEntityToLayer[typeof(TComponent)].Remove(entityID);
} }
@ -97,7 +84,10 @@ namespace Encompass
{ {
foreach (var store in _layerIndexToComponentStore.Values) foreach (var store in _layerIndexToComponentStore.Values)
{ {
store.Remove(entityID); foreach (var set in store.Values)
{
set.Remove(entityID);
}
} }
} }
@ -108,11 +98,15 @@ namespace Encompass
Enumerable.Empty<GeneralRenderer>(); Enumerable.Empty<GeneralRenderer>();
} }
public IEnumerable<(int, Type, IComponent)> AllInLayer(int layer) public IEnumerable<(int, Type)> AllInLayer(int layer)
{ {
return _layerIndexToComponentStore.ContainsKey(layer) ? foreach (var kvp in _layerIndexToComponentStore[layer])
_layerIndexToComponentStore[layer].AllInterfaceTyped() : {
Enumerable.Empty<(int, Type, IComponent)>(); foreach (var id in kvp.Value)
{
yield return (id, kvp.Key);
}
}
} }
} }
} }

View File

@ -474,7 +474,7 @@ namespace Encompass
if (written && component is IDrawableComponent drawableComponent) if (written && component is IDrawableComponent drawableComponent)
{ {
_componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
} }
} }
@ -505,7 +505,7 @@ namespace Encompass
if (component is IDrawableComponent drawableComponent) if (component is IDrawableComponent drawableComponent)
{ {
_componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
} }
} }

View File

@ -8,7 +8,7 @@ namespace Encompass
private readonly EntityManager _entityManager; private readonly EntityManager _entityManager;
private readonly DrawLayerManager _drawLayerManager; private readonly DrawLayerManager _drawLayerManager;
private readonly Dictionary<Type, Action<Entity, IComponent>> _drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity, IComponent>>(256); private readonly Dictionary<Type, Action<Entity>> _drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity>>(256);
public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager) public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager)
{ {
@ -16,7 +16,7 @@ namespace Encompass
_drawLayerManager = drawLayerManager; _drawLayerManager = drawLayerManager;
} }
public void RegisterOrderedRenderer<TComponent>(Action<Entity, IComponent> renderAction) where TComponent : struct, IComponent public void RegisterOrderedRenderer<TComponent>(Action<Entity> renderAction) where TComponent : struct, IComponent
{ {
_drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction); _drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
_drawLayerManager.RegisterOrderedDrawable<TComponent>(); _drawLayerManager.RegisterOrderedDrawable<TComponent>();
@ -33,12 +33,12 @@ namespace Encompass
{ {
var generalRendererSet = _drawLayerManager.GeneralRenderersByLayer(layer); var generalRendererSet = _drawLayerManager.GeneralRenderersByLayer(layer);
foreach (var (entityID, componentType, component) in _drawLayerManager.AllInLayer(layer)) foreach (var (entityID, componentType) in _drawLayerManager.AllInLayer(layer))
{ {
if (_drawComponentTypeToOrderedRenderer.ContainsKey(componentType)) if (_drawComponentTypeToOrderedRenderer.ContainsKey(componentType))
{ {
var internalRenderAction = _drawComponentTypeToOrderedRenderer[componentType]; var internalRenderAction = _drawComponentTypeToOrderedRenderer[componentType];
internalRenderAction(_entityManager.GetEntity(entityID), component); internalRenderAction(_entityManager.GetEntity(entityID));
} }
} }

View File

@ -9,9 +9,9 @@ namespace Encompass
{ {
public abstract void Render(Entity entity, TComponent drawComponent); public abstract void Render(Entity entity, TComponent drawComponent);
internal void InternalRender(Entity entity, IComponent component) internal void InternalRender(Entity entity)
{ {
Render(entity, (TComponent)component); Render(entity, GetComponent<TComponent>(entity));
} }
} }
} }

View File

@ -45,7 +45,7 @@ namespace Encompass
public WorldBuilder(int entityCapacity = 32768) public WorldBuilder(int entityCapacity = 32768)
{ {
_entityCapacity = entityCapacity; _entityCapacity = entityCapacity;
_drawLayerManager = new DrawLayerManager(_typeToIndex); _drawLayerManager = new DrawLayerManager();
_timeManager = new TimeManager(); _timeManager = new TimeManager();
_trackingManager = new TrackingManager(); _trackingManager = new TrackingManager();
_componentManager = new ComponentManager(_drawLayerManager, _typeToIndex); _componentManager = new ComponentManager(_drawLayerManager, _typeToIndex);
@ -92,7 +92,7 @@ namespace Encompass
if (component is IDrawableComponent drawableComponent) if (component is IDrawableComponent drawableComponent)
{ {
_componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
_drawLayerManager.RegisterOrderedDrawable<TComponent>(); _drawLayerManager.RegisterOrderedDrawable<TComponent>();
} }
} }
@ -406,7 +406,7 @@ namespace Encompass
{ {
var dummyTimeManager = new TimeManager(); var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager); var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyDrawLayerManager = new DrawLayerManager(_typeToIndex); var dummyDrawLayerManager = new DrawLayerManager();
var dummyTrackingManager = new TrackingManager(); var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, _typeToIndex); var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, _typeToIndex);
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity); var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);