rework delta store to not use inheritance
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-21 20:29:15 -07:00
parent 812fe10ca1
commit 4178e8f02c
3 changed files with 48 additions and 26 deletions

View File

@ -3,39 +3,43 @@ using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
internal class ComponentDeltaStore : ComponentStore internal class ComponentDeltaStore
{ {
private readonly ComponentStore _store;
private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>(); private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>();
private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>(); private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>();
public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } } public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } }
public ComponentDeltaStore(Dictionary<Type, int> typeToIndex) : base(typeToIndex) public ComponentDeltaStore(Dictionary<Type, int> typeToIndex)
{ {
_store = new ComponentStore(typeToIndex);
} }
public override void RegisterComponentType<TComponent>() public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
base.RegisterComponentType<TComponent>(); _store.RegisterComponentType<TComponent>();
if (!_replayers.ContainsKey(typeof(TComponent))) if (!_replayers.ContainsKey(typeof(TComponent)))
{ {
_replayers.Add(typeof(TComponent), new Replayer<TComponent>(this)); _replayers.Add(typeof(TComponent), new Replayer<TComponent>(this));
} }
} }
public override void Set<TComponent>(int entityID, TComponent component) public void Set<TComponent>(int entityID, TComponent component) where TComponent : struct
{ {
base.Set(entityID, component); _store.Set(entityID, component);
RegisterComponentType<TComponent>();
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
_currentReplayers.Add(replayer); _currentReplayers.Add(replayer);
replayer.UnMarkRemoval(entityID); replayer.UnMarkRemoval(entityID);
} }
public override bool Set<TComponent>(int entityID, TComponent component, int priority) public bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
var result = base.Set(entityID, component, priority); var result = _store.Set(entityID, component, priority);
if (result) if (result)
{ {
RegisterComponentType<TComponent>();
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
_currentReplayers.Add(replayer); _currentReplayers.Add(replayer);
replayer.UnMarkRemoval(entityID); replayer.UnMarkRemoval(entityID);
@ -43,9 +47,9 @@ namespace Encompass
return result; return result;
} }
public override bool Remove<TComponent>(int entityID, int priority) public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
var result = base.Remove<TComponent>(entityID, priority); var result = _store.Remove<TComponent>(entityID, priority);
if (result) if (result)
{ {
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
@ -55,18 +59,23 @@ namespace Encompass
return result; return result;
} }
public override void Remove(int entityID) public void Remove(int entityID)
{ {
base.Remove(entityID); _store.Remove(entityID);
foreach (var replayer in CurrentReplayers) foreach (var replayer in CurrentReplayers)
{ {
replayer.MarkRemoval(entityID); replayer.MarkRemoval(entityID);
} }
} }
public override void ClearAll() public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct
{ {
base.ClearAll(); return _store.All<TComponent>();
}
public void ClearAll()
{
_store.ClearAll();
foreach (var replayer in _currentReplayers) foreach (var replayer in _currentReplayers)
{ {
replayer.Clear(); replayer.Clear();

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
internal class ComponentStore internal sealed class ComponentStore
{ {
private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512); private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512);
public ComponentBitSet ComponentBitSet { get; private set; } public ComponentBitSet ComponentBitSet { get; private set; }
@ -16,7 +16,7 @@ namespace Encompass
ComponentBitSet = new ComponentBitSet(typeToIndex); ComponentBitSet = new ComponentBitSet(typeToIndex);
} }
public virtual void RegisterComponentType<TComponent>() where TComponent : struct public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
if (!_stores.ContainsKey(typeof(TComponent))) if (!_stores.ContainsKey(typeof(TComponent)))
{ {
@ -53,13 +53,13 @@ namespace Encompass
return ref Lookup<TComponent>().Get(entityID); return ref Lookup<TComponent>().Get(entityID);
} }
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : struct public void Set<TComponent>(int entityID, TComponent component) where TComponent : struct
{ {
Lookup<TComponent>().Set(entityID, component); Lookup<TComponent>().Set(entityID, component);
ComponentBitSet.Set<TComponent>(entityID); ComponentBitSet.Set<TComponent>(entityID);
} }
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct public bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Set(entityID, component, priority)) if (Lookup<TComponent>().Set(entityID, component, priority))
{ {
@ -69,7 +69,7 @@ namespace Encompass
return false; return false;
} }
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : struct public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Remove(entityID, priority)) if (Lookup<TComponent>().Remove(entityID, priority))
{ {
@ -85,7 +85,7 @@ namespace Encompass
ComponentBitSet.RemoveComponent<TComponent>(entityID); ComponentBitSet.RemoveComponent<TComponent>(entityID);
} }
public virtual void Remove(int entityID) public void Remove(int entityID)
{ {
foreach (var entry in _stores.Values) foreach (var entry in _stores.Values)
{ {
@ -109,7 +109,7 @@ namespace Encompass
Lookup<TComponent>().Clear(); Lookup<TComponent>().Clear();
} }
public virtual void ClearAllPriorities() public void ClearAllPriorities()
{ {
foreach (var store in _stores.Values) foreach (var store in _stores.Values)
{ {
@ -117,7 +117,7 @@ namespace Encompass
} }
} }
public virtual void ClearAll() public void ClearAll()
{ {
ComponentBitSet.Clear(); ComponentBitSet.Clear();
foreach (var store in _stores.Values) foreach (var store in _stores.Values)

View File

@ -60,13 +60,26 @@ namespace Encompass
throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer); throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer);
} }
if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) { UnRegisterComponentWithLayer<TComponent>(entityID); } if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{
if (_typeToEntityToLayer[typeof(TComponent)][entityID] != layer)
{
UnRegisterComponentWithLayer<TComponent>(entityID);
var set = _layerIndexToTypeToID[layer][typeof(TComponent)]; var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Add(entityID); set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer); _typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
} }
}
else
{
var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
}
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct
{ {