272 lines
10 KiB
C#
272 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class ComponentManager
|
|
{
|
|
private readonly DrawLayerManager _drawLayerManager;
|
|
|
|
private readonly ComponentStore _existingComponentStore;
|
|
private readonly ComponentStore _immediateComponentStore;
|
|
private readonly ComponentDeltaStore _replayStore;
|
|
private readonly ComponentStore _upToDateComponentStore;
|
|
|
|
public Dictionary<Type, int> TypeToIndex { get; }
|
|
|
|
private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>();
|
|
|
|
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
|
|
internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
|
|
|
|
public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex)
|
|
{
|
|
this._drawLayerManager = drawLayerManager;
|
|
_existingComponentStore = new ComponentStore(typeToIndex);
|
|
_immediateComponentStore = new ComponentStore(typeToIndex);
|
|
_replayStore = new ComponentDeltaStore(typeToIndex);
|
|
_upToDateComponentStore = new ComponentStore(typeToIndex);
|
|
TypeToIndex = typeToIndex;
|
|
}
|
|
|
|
public void RegisterComponentType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
_existingComponentStore.RegisterComponentType<TComponent>();
|
|
_immediateComponentStore.RegisterComponentType<TComponent>();
|
|
_replayStore.RegisterComponentType<TComponent>();
|
|
_upToDateComponentStore.RegisterComponentType<TComponent>();
|
|
}
|
|
|
|
internal void SetExistingComponentStore(ComponentStore componentStore)
|
|
{
|
|
_existingComponentStore.SwapWith(componentStore);
|
|
}
|
|
|
|
internal void SetUpToDateComponentStore(ComponentStore componentStore)
|
|
{
|
|
_upToDateComponentStore.SwapWith(componentStore);
|
|
}
|
|
|
|
internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : unmanaged
|
|
{
|
|
_drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer);
|
|
}
|
|
|
|
internal void WriteComponents()
|
|
{
|
|
_existingComponentStore.UpdateUsing(_replayStore);
|
|
_existingComponentStore.ClearAllPriorities();
|
|
_upToDateComponentStore.ClearAllPriorities();
|
|
_immediateComponentStore.ClearAll();
|
|
_replayStore.ClearAll();
|
|
}
|
|
|
|
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged
|
|
{
|
|
if (_immediateComponentStore.Set(entityID, component, priority))
|
|
{
|
|
_replayStore.Set(entityID, component);
|
|
_upToDateComponentStore.Set(entityID, component);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : unmanaged
|
|
{
|
|
_immediateComponentStore.Set(entityID, component);
|
|
_replayStore.Set(entityID, component);
|
|
_upToDateComponentStore.Set(entityID, component);
|
|
}
|
|
|
|
internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged
|
|
{
|
|
var result = _upToDateComponentStore.Set(entityID, component, priority);
|
|
if (result)
|
|
{
|
|
_replayStore.Set(entityID, component);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : unmanaged
|
|
{
|
|
_upToDateComponentStore.Set(entityID, component);
|
|
_replayStore.Set(entityID, component);
|
|
}
|
|
|
|
// existing or immediate reads
|
|
|
|
internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _upToDateComponentStore.All<TComponent>();
|
|
}
|
|
|
|
internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator();
|
|
enumerator.MoveNext();
|
|
return enumerator.Current;
|
|
}
|
|
|
|
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _upToDateComponentStore.Any<TComponent>();
|
|
}
|
|
|
|
// existing reads
|
|
|
|
internal (TComponent, int) ReadFirstExistingComponentByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
var enumerator = GetComponentsIncludingEntity<TComponent>().GetEnumerator();
|
|
enumerator.MoveNext();
|
|
return enumerator.Current;
|
|
}
|
|
|
|
internal bool SomeExistingComponent<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _existingComponentStore.Any<TComponent>();
|
|
}
|
|
|
|
// immediate reads
|
|
|
|
internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _immediateComponentStore.All<TComponent>();
|
|
}
|
|
|
|
internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
|
var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator();
|
|
enumerator.MoveNext();
|
|
return enumerator.Current;
|
|
}
|
|
|
|
internal bool SomeImmediateComponent<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _immediateComponentStore.Any<TComponent>();
|
|
}
|
|
|
|
// component getters
|
|
|
|
internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return ref _upToDateComponentStore.Get<TComponent>(entityID);
|
|
}
|
|
|
|
internal ref readonly TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return ref _existingComponentStore.Get<TComponent>(entityID);
|
|
}
|
|
|
|
internal ref readonly TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return ref _immediateComponentStore.Get<TComponent>(entityID);
|
|
}
|
|
|
|
// has checkers
|
|
|
|
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return _upToDateComponentStore.Has<TComponent>(entityID);
|
|
}
|
|
|
|
internal bool HasExistingOrImmediateComponent(int entityID, Type type)
|
|
{
|
|
return _upToDateComponentStore.Has(type, entityID);
|
|
}
|
|
|
|
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return _existingComponentStore.Has<TComponent>(entityID);
|
|
}
|
|
|
|
internal bool HasExistingComponent(int entityID, Type type)
|
|
{
|
|
return _existingComponentStore.Has(type, entityID);
|
|
}
|
|
|
|
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return _immediateComponentStore.Has<TComponent>(entityID);
|
|
}
|
|
|
|
internal bool HasImmediateComponent(int entityID, Type type)
|
|
{
|
|
return _immediateComponentStore.Has(type, entityID);
|
|
}
|
|
|
|
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return _existingComponentStore.All<TComponent>();
|
|
}
|
|
|
|
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
foreach (var pair in _existingComponentStore.All<TComponent>())
|
|
{
|
|
yield return pair.Item1;
|
|
}
|
|
}
|
|
|
|
internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return ref _existingComponentStore.Get<TComponent>(entityID);
|
|
}
|
|
|
|
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return _existingComponentStore.Has<TComponent>(entityID);
|
|
}
|
|
|
|
internal void MarkAllComponentsOnEntityForRemoval(int entityID)
|
|
{
|
|
_entitiesMarkedForRemoval.Add(entityID);
|
|
}
|
|
|
|
internal void RemoveMarkedComponents()
|
|
{
|
|
foreach (var entityID in _entitiesMarkedForRemoval)
|
|
{
|
|
_existingComponentStore.Remove(entityID);
|
|
_immediateComponentStore.Remove(entityID);
|
|
_replayStore.Remove(entityID);
|
|
_upToDateComponentStore.Remove(entityID);
|
|
_drawLayerManager.UnRegisterEntityWithLayer(entityID);
|
|
}
|
|
|
|
_entitiesMarkedForRemoval.Clear();
|
|
}
|
|
|
|
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : unmanaged
|
|
{
|
|
if (_immediateComponentStore.Remove<TComponent>(entityID, priority))
|
|
{
|
|
_replayStore.Remove<TComponent>(entityID, priority);
|
|
_upToDateComponentStore.Remove<TComponent>(entityID, priority);
|
|
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void Remove<TComponent>(int entityID, int priority) where TComponent : unmanaged
|
|
{
|
|
if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
|
|
{
|
|
_replayStore.Remove<TComponent>(entityID, priority);
|
|
_drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
|
|
}
|
|
}
|
|
|
|
public bool UpToDateEntityIsEmpty(int entityID)
|
|
{
|
|
return _upToDateComponentStore.EntityBitArray(entityID).AllFalse();
|
|
}
|
|
}
|
|
}
|