oecs-sharp/OECS/ComponentStore.cs

113 lines
3.2 KiB
C#
Raw Normal View History

using System.Runtime.CompilerServices;
namespace OECS;
/// <summary>
/// Registry of all component sparse sets, keyed by component type.
/// Provides typed generic accessors that delegate to the underlying
/// <see cref="SparseSet{T}"/> instances.
/// </summary>
internal class ComponentStore
{
private readonly Dictionary<Type, ISparseSet> _sets = new();
/// <summary>
/// Gets or creates the sparse set for component type <typeparamref name="T"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private SparseSet<T> GetSet<T>() where T : struct
{
var type = typeof(T);
if (!_sets.TryGetValue(type, out var set))
{
var newSet = new SparseSet<T>();
_sets[type] = newSet;
return newSet;
}
return (SparseSet<T>)set;
}
/// <summary>
/// Gets the sparse set for component type <typeparamref name="T"/> if it exists.
/// </summary>
private SparseSet<T>? GetSetIfExists<T>() where T : struct
{
if (_sets.TryGetValue(typeof(T), out var set))
return (SparseSet<T>)set;
return null;
}
/// <summary>
/// Returns all registered component types.
/// </summary>
public IEnumerable<Type> ComponentTypes => _sets.Keys;
public void Add<T>(Entity entity, T component) where T : struct
{
GetSet<T>().Add(entity, component);
}
public void Remove<T>(Entity entity) where T : struct
{
GetSetIfExists<T>()?.Remove(entity);
}
/// <summary>
/// Removes the component of the given type from the entity.
/// No-op if the entity does not have the component or the type is not registered.
/// Used for cascading relationship removal during entity destruction.
/// </summary>
public void Remove(Entity entity, Type componentType)
{
GetSet(componentType)?.Remove(entity);
}
public ref T Get<T>(Entity entity) where T : struct
{
return ref GetSet<T>().Get(entity);
}
public bool Has<T>(Entity entity) where T : struct
{
return GetSetIfExists<T>()?.Contains(entity) ?? false;
}
/// <summary>
/// Tries to get the component value for the given entity.
/// Returns true if the component exists, with the value copied to <paramref name="value"/>.
/// </summary>
public bool TryGet<T>(Entity entity, out T value) where T : struct
{
var set = GetSetIfExists<T>();
if (set != null && set.Contains(entity))
{
value = set.Get(entity);
return true;
}
value = default;
return false;
}
/// <summary>
/// Removes all components for the given entity across all component types.
/// </summary>
public void RemoveAll(Entity entity)
{
foreach (var set in _sets.Values)
{
set.Remove(entity);
}
}
/// <summary>
/// Returns the sparse set for a given type, or null if not registered.
/// Used by query execution to probe sets by Type.
/// </summary>
public ISparseSet? GetSet(Type componentType)
{
_sets.TryGetValue(componentType, out var set);
return set;
}
}