replace entity storage with int

pull/5/head
Evan Hemsley 2019-12-29 13:54:08 -08:00
parent f7095ffdfb
commit 1eb72874c1
8 changed files with 125 additions and 116 deletions

View File

@ -39,9 +39,9 @@ namespace Encompass
existingComponentStore.SwapWith(componentStore);
}
internal void RegisterDrawableComponent<TComponent>(Entity entity, TComponent component, int layer) where TComponent : struct, IComponent
internal void RegisterDrawableComponent<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent
{
drawLayerManager.RegisterComponentWithLayer(entity.ID, component, layer);
drawLayerManager.RegisterComponentWithLayer(entityID, component, layer);
}
internal void WriteComponents()
@ -51,25 +51,25 @@ namespace Encompass
immediateComponentStore.ClearAll();
}
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
internal void AddExistingComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent
{
upToDateComponentStore.Set(entity.ID, component);
upToDateComponentStore.Set(entityID, component);
}
internal bool AddImmediateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent
{
if (immediateComponentStore.Set(entity.ID, component, priority))
if (immediateComponentStore.Set(entityID, component, priority))
{
upToDateComponentStore.Set(entity.ID, component);
upToDateComponentStore.Set(entityID, component);
return true;
}
return false;
}
public bool UpdateComponent<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
public bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent
{
return upToDateComponentStore.Set(entity.ID, component, priority);
return upToDateComponentStore.Set(entityID, component, priority);
}
// existing or immediate reads
@ -129,51 +129,51 @@ namespace Encompass
// component getters
internal TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
internal TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return upToDateComponentStore.Get<TComponent>(entity.ID);
return upToDateComponentStore.Get<TComponent>(entityID);
}
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return existingComponentStore.Get<TComponent>(entity.ID);
return existingComponentStore.Get<TComponent>(entityID);
}
internal TComponent ReadImmediateComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
internal TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return immediateComponentStore.Get<TComponent>(entity.ID);
return immediateComponentStore.Get<TComponent>(entityID);
}
// has checkers
internal bool HasExistingOrImmediateComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent
{
return upToDateComponentStore.Has<TComponent>(entity.ID);
return upToDateComponentStore.Has<TComponent>(entityID);
}
internal bool HasExistingOrImmediateComponent(Entity entity, Type type)
internal bool HasExistingOrImmediateComponent(int entityID, Type type)
{
return upToDateComponentStore.Has(type, entity.ID);
return upToDateComponentStore.Has(type, entityID);
}
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct, IComponent
{
return existingComponentStore.Has<TComponent>(entity.ID);
return existingComponentStore.Has<TComponent>(entityID);
}
internal bool HasExistingComponent(Entity entity, Type type)
internal bool HasExistingComponent(int entityID, Type type)
{
return existingComponentStore.Has(type, entity.ID);
return existingComponentStore.Has(type, entityID);
}
internal bool HasImmediateComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent
{
return immediateComponentStore.Has<TComponent>(entity.ID);
return immediateComponentStore.Has<TComponent>(entityID);
}
internal bool HasImmediateComponent(Entity entity, Type type)
internal bool HasImmediateComponent(int entityID, Type type)
{
return immediateComponentStore.Has(type, entity.ID);
return immediateComponentStore.Has(type, entityID);
}
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
@ -189,19 +189,19 @@ namespace Encompass
}
}
internal TComponent GetComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
internal TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return existingComponentStore.Get<TComponent>(entity.ID);
return existingComponentStore.Get<TComponent>(entityID);
}
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return existingComponentStore.Has<TComponent>(entity.ID);
return existingComponentStore.Has<TComponent>(entityID);
}
internal void MarkAllComponentsOnEntityForRemoval(Entity entity)
internal void MarkAllComponentsOnEntityForRemoval(int entityID)
{
entitiesMarkedForRemoval.Add(entity.ID);
entitiesMarkedForRemoval.Add(entityID);
}
internal void RemoveMarkedComponents()
@ -216,22 +216,22 @@ namespace Encompass
entitiesMarkedForRemoval.Clear();
}
public bool RemoveImmediate<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct, IComponent
{
if (immediateComponentStore.Remove<TComponent>(entity.ID, priority))
if (immediateComponentStore.Remove<TComponent>(entityID, priority))
{
upToDateComponentStore.Remove<TComponent>(entity.ID, priority);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
upToDateComponentStore.Remove<TComponent>(entityID, priority);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
return true;
}
return false;
}
public void Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
public void Remove<TComponent>(int entityID, int priority) where TComponent : struct, IComponent
{
if (upToDateComponentStore.Remove<TComponent>(entity.ID, priority))
if (upToDateComponentStore.Remove<TComponent>(entityID, priority))
{
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
}
}
}

View File

@ -41,8 +41,17 @@ namespace Encompass
private EntitySetQuery entityQuery;
private HashSet<Entity> _trackedEntities = new HashSet<Entity>();
protected IEnumerable<Entity> TrackedEntities { get { return _trackedEntities; } }
private HashSet<int> _trackedEntities = new HashSet<int>();
protected IEnumerable<Entity> TrackedEntities
{
get
{
foreach (var entityID in _trackedEntities)
{
yield return entityManager.GetEntity(entityID);
}
}
}
protected Engine()
{
@ -313,21 +322,21 @@ namespace Encompass
}
}
private TComponent GetComponentHelper<TComponent>(Entity entity) where TComponent : struct, IComponent
private TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entity);
return componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID);
}
else if (existingRead)
{
return componentManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
return componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID);
}
else if (immediateRead)
{
return componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entity);
return componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID);
}
else
{
@ -346,7 +355,7 @@ namespace Encompass
/// </exception>
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return GetComponentHelper<TComponent>(entity);
return GetComponentHelper<TComponent>(entity.ID);
}
/// <summary>
@ -362,15 +371,15 @@ namespace Encompass
if (immediateRead && existingRead)
{
return componentManager.HasExistingOrImmediateComponent<TComponent>(entity);
return componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID);
}
else if (existingRead)
{
return componentManager.HasExistingComponent<TComponent>(entity);
return componentManager.HasExistingComponent<TComponent>(entity.ID);
}
else if (immediateRead)
{
return componentManager.HasImmediateComponent<TComponent>(entity);
return componentManager.HasImmediateComponent<TComponent>(entity.ID);
}
else
{
@ -391,15 +400,15 @@ namespace Encompass
if (immediateRead && existingRead)
{
return componentManager.HasExistingOrImmediateComponent(entity, type);
return componentManager.HasExistingOrImmediateComponent(entity.ID, type);
}
else if (existingRead)
{
return componentManager.HasExistingComponent(entity, type);
return componentManager.HasExistingComponent(entity.ID, type);
}
else if (immediateRead)
{
return componentManager.HasImmediateComponent(entity, type);
return componentManager.HasImmediateComponent(entity.ID, type);
}
else
{
@ -425,25 +434,25 @@ namespace Encompass
bool written;
if (writeImmediateTypes.Contains(typeof(TComponent)))
{
written = componentManager.AddImmediateComponent(entity, component, priority);
written = componentManager.AddImmediateComponent(entity.ID, component, priority);
if (written)
{
trackingManager.ImmediateUpdateTracking(entity, typeof(TComponent));
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
}
}
else
{
written = componentManager.UpdateComponent(entity, component, priority);
written = componentManager.UpdateComponent(entity.ID, component, priority);
}
if (!componentManager.HasExistingComponent<TComponent>(entity))
if (!componentManager.HasExistingComponent<TComponent>(entity.ID))
{
trackingManager.RegisterAddition(entity, typeof(TComponent));
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
}
if (written && component is IDrawableComponent drawableComponent)
{
componentManager.RegisterDrawableComponent(entity, component, drawableComponent.Layer);
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
}
}
@ -481,9 +490,9 @@ namespace Encompass
messageManager.AddMessageIgnoringTimeDilation(message, time);
}
internal void AddExistingComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
internal void AddExistingComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent
{
componentManager.AddExistingComponent(entity, component);
componentManager.AddExistingComponent(entityID, component);
}
/// <summary>
@ -535,7 +544,7 @@ namespace Encompass
/// </summary>
protected void Destroy(Entity entity)
{
entityManager.MarkForDestroy(entity);
entityManager.MarkForDestroy(entity.ID);
}
/// <summary>
@ -575,19 +584,19 @@ namespace Encompass
if (writeImmediateTypes.Contains(typeof(TComponent)))
{
if (componentManager.RemoveImmediate<TComponent>(entity, priority))
if (componentManager.RemoveImmediate<TComponent>(entity.ID, priority))
{
trackingManager.ImmediateUpdateTracking(entity, typeof(TComponent));
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
}
}
else
{
componentManager.Remove<TComponent>(entity, priority);
componentManager.Remove<TComponent>(entity.ID, priority);
}
if (componentManager.HasExistingComponent<TComponent>(entity))
if (componentManager.HasExistingComponent<TComponent>(entity.ID))
{
trackingManager.RegisterRemoval(entity, typeof(TComponent));
trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
}
}
@ -651,27 +660,27 @@ namespace Encompass
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
}
internal void CheckAndUpdateTracking(Entity entity)
internal void CheckAndUpdateTracking(int entityID)
{
if (_trackedEntities.Contains(entity) && !entityQuery.CheckEntity(entity, componentManager.ExistingBits))
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
{
_trackedEntities.Remove(entity);
_trackedEntities.Remove(entityID);
}
else if (!_trackedEntities.Contains(entity) && entityQuery.CheckEntity(entity, componentManager.ExistingBits))
else if (!_trackedEntities.Contains(entityID) && entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
{
_trackedEntities.Add(entity);
_trackedEntities.Add(entityID);
}
}
internal void ImmediateCheckAndUpdateTracking(Entity entity)
internal void ImmediateCheckAndUpdateTracking(int entityID)
{
if (_trackedEntities.Contains(entity) && !entityQuery.ImmediateCheckEntity(entity, componentManager.ImmediateBits, componentManager.ExistingBits))
if (_trackedEntities.Contains(entityID) && !entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
{
_trackedEntities.Remove(entity);
_trackedEntities.Remove(entityID);
}
else if (!_trackedEntities.Contains(entity) && entityQuery.ImmediateCheckEntity(entity, componentManager.ImmediateBits, componentManager.ExistingBits))
else if (!_trackedEntities.Contains(entityID) && entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
{
_trackedEntities.Add(entity);
_trackedEntities.Add(entityID);
}
}
@ -713,9 +722,9 @@ namespace Encompass
);
}
internal void RegisterDestroyedEntity(Entity entity)
internal void RegisterDestroyedEntity(int entityID)
{
_trackedEntities.Remove(entity);
_trackedEntities.Remove(entityID);
}
}
}

View File

@ -6,7 +6,7 @@
{
foreach (var (component, entity) in InternalRead<TComponent>())
{
AddExistingComponent(entity, component);
AddExistingComponent(entity.ID, component);
}
}
}

View File

@ -7,15 +7,15 @@ namespace Encompass
{
private readonly int entityCapacity;
private readonly IDManager idManager = new IDManager();
private readonly Dictionary<int, Entity> IDs = new Dictionary<int, Entity>(32768);
private readonly HashSet<int> IDs = new HashSet<int>();
private readonly HashSet<Entity> entitiesMarkedForDestroy = new HashSet<Entity>();
private readonly HashSet<int> entitiesMarkedForDestroy = new HashSet<int>();
private readonly ComponentManager componentManager;
public IEnumerable<Entity> Entities
public IEnumerable<int> EntityIDs
{
get { return IDs.Values; }
get { return IDs; }
}
public EntityManager(ComponentManager componentManager, int entityCapacity)
@ -35,7 +35,7 @@ namespace Encompass
{
var id = NextID();
var entity = new Entity(id);
IDs.Add(id, entity);
IDs.Add(id);
return entity;
}
else
@ -46,7 +46,7 @@ namespace Encompass
public bool EntityExists(int id)
{
return IDs.ContainsKey(id);
return IDs.Contains(id);
}
public Entity GetEntity(int id)
@ -56,22 +56,22 @@ namespace Encompass
throw new Encompass.Exceptions.EntityNotFoundException("Entity with id {0} does not exist.", id);
}
return IDs[id];
return new Entity(id);
}
public void MarkForDestroy(Entity entity)
public void MarkForDestroy(int entityID)
{
entitiesMarkedForDestroy.Add(entity);
entitiesMarkedForDestroy.Add(entityID);
}
public void DestroyMarkedEntities(IEnumerable<Engine> engines)
{
foreach (var entity in entitiesMarkedForDestroy)
foreach (var entityID in entitiesMarkedForDestroy)
{
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entity); }
componentManager.MarkAllComponentsOnEntityForRemoval(entity);
IDs.Remove(entity.ID);
idManager.Free(entity.ID);
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); }
componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
IDs.Remove(entityID);
idManager.Free(entityID);
}
entitiesMarkedForDestroy.Clear();

View File

@ -19,9 +19,9 @@ namespace Encompass
NotWithMask = notWithMask;
}
public bool CheckEntity(Entity entity, ComponentBitSet componentBitSet)
public bool CheckEntity(int entityID, ComponentBitSet componentBitSet)
{
var existingBits = componentBitSet.EntityBitArray(entity.ID);
var existingBits = componentBitSet.EntityBitArray(entityID);
var existing = (WithExistingMask & existingBits) | NotWithMask;
var existingForbidden = ~(WithoutExistingMask & existingBits);
@ -29,10 +29,10 @@ namespace Encompass
return (existing & existingForbidden).AllTrue();
}
public bool ImmediateCheckEntity(Entity entity, ComponentBitSet immediateBitLookup, ComponentBitSet existingBitLookup)
public bool ImmediateCheckEntity(int entityID, ComponentBitSet immediateBitLookup, ComponentBitSet existingBitLookup)
{
var immediateBits = immediateBitLookup.EntityBitArray(entity.ID);
var existingBits = existingBitLookup.EntityBitArray(entity.ID);
var immediateBits = immediateBitLookup.EntityBitArray(entityID);
var existingBits = existingBitLookup.EntityBitArray(entityID);
var immediate = WithImmediateMask & immediateBits;
var existing = WithExistingMask & existingBits;

View File

@ -59,12 +59,12 @@ namespace Encompass
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentManager.GetComponentByEntityAndType<TComponent>(entity);
return componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentManager.EntityHasComponentOfType<TComponent>(entity);
return componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
}
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent

View File

@ -8,10 +8,10 @@ namespace Encompass
private Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
private Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
private HashSet<(Entity, Type)> _additions = new HashSet<(Entity, Type)>();
private HashSet<(Entity, Type)> _removals = new HashSet<(Entity, Type)>();
private HashSet<(int, Type)> _additions = new HashSet<(int, Type)>();
private HashSet<(int, Type)> _removals = new HashSet<(int, Type)>();
private HashSet<(Entity, Engine)> _pairsToCheck = new HashSet<(Entity, Engine)>();
private HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>();
public void RegisterComponentTypeToEngine(Type type, Engine engine)
{
@ -25,37 +25,37 @@ namespace Encompass
_immediateComponentTypesToEngines[type].Add(engine);
}
public void RegisterAddition(Entity entity, Type type)
public void RegisterAddition(int entityID, Type type)
{
_additions.Add((entity, type));
_additions.Add((entityID, type));
}
public void RegisterRemoval(Entity entity, Type type)
public void RegisterRemoval(int entityID, Type type)
{
_removals.Add((entity, type));
_removals.Add((entityID, type));
}
public void InitializeTracking(IEnumerable<Entity> entities)
public void InitializeTracking(IEnumerable<int> entityIDs)
{
foreach (var entity in entities)
foreach (var entityID in entityIDs)
{
foreach (var engineSet in _componentTypesToEngines.Values)
{
foreach (var engine in engineSet)
{
engine.CheckAndUpdateTracking(entity);
engine.CheckAndUpdateTracking(entityID);
}
}
}
}
public void ImmediateUpdateTracking(Entity entity, Type componentType)
public void ImmediateUpdateTracking(int entityID, Type componentType)
{
if (_immediateComponentTypesToEngines.ContainsKey(componentType))
{
foreach (var engine in _componentTypesToEngines[componentType])
{
engine.ImmediateCheckAndUpdateTracking(entity);
engine.ImmediateCheckAndUpdateTracking(entityID);
}
}
}

View File

@ -94,7 +94,7 @@ namespace Encompass
if (component is IDrawableComponent drawableComponent)
{
componentManager.RegisterDrawableComponent(entity, component, drawableComponent.Layer);
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
drawLayerManager.RegisterOrderedDrawable<TComponent>();
}
}
@ -397,7 +397,7 @@ namespace Encompass
componentManager.SetComponentStore(startingComponentStoreForComponentManager);
trackingManager.InitializeTracking(entityManager.Entities);
trackingManager.InitializeTracking(entityManager.EntityIDs);
return world;
}