144 lines
4.5 KiB
C#
144 lines
4.5 KiB
C#
using MoonTools.FastCollections;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class ComponentStore
|
|
{
|
|
private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512);
|
|
public ComponentBitSet ComponentBitSet { get; private set; }
|
|
private readonly Dictionary<Type, int> _typeToIndex;
|
|
|
|
public ComponentStore(Dictionary<Type, int> typeToIndex)
|
|
{
|
|
_typeToIndex = typeToIndex;
|
|
ComponentBitSet = new ComponentBitSet(typeToIndex);
|
|
}
|
|
|
|
public virtual void RegisterComponentType<TComponent>() where TComponent : unmanaged
|
|
{
|
|
if (!_stores.ContainsKey(typeof(TComponent)))
|
|
{
|
|
var store = new TypedComponentStore<TComponent>();
|
|
_stores.Add(typeof(TComponent), store);
|
|
}
|
|
|
|
if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); }
|
|
}
|
|
|
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : unmanaged
|
|
{
|
|
RegisterComponentType<TComponent>();
|
|
return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
|
|
}
|
|
|
|
public bool Has<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return Lookup<TComponent>().Has(entityID);
|
|
}
|
|
|
|
public bool Has(Type type, int entityID)
|
|
{
|
|
return _stores.ContainsKey(type) && _stores[type].Has(entityID);
|
|
}
|
|
|
|
public BitSet512 EntityBitArray(int entityID)
|
|
{
|
|
return ComponentBitSet.EntityBitArray(entityID);
|
|
}
|
|
|
|
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
return ref Lookup<TComponent>().Get(entityID);
|
|
}
|
|
|
|
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : unmanaged
|
|
{
|
|
Lookup<TComponent>().Set(entityID, component);
|
|
ComponentBitSet.Set<TComponent>(entityID);
|
|
}
|
|
|
|
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : unmanaged
|
|
{
|
|
if (Lookup<TComponent>().Set(entityID, component, priority))
|
|
{
|
|
ComponentBitSet.Set<TComponent>(entityID);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : unmanaged
|
|
{
|
|
if (Lookup<TComponent>().Remove(entityID, priority))
|
|
{
|
|
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void ForceRemove<TComponent>(int entityID) where TComponent : unmanaged
|
|
{
|
|
Lookup<TComponent>().ForceRemove(entityID);
|
|
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
|
}
|
|
|
|
public virtual void Remove(int entityID)
|
|
{
|
|
foreach (var entry in _stores.Values)
|
|
{
|
|
entry.ForceRemove(entityID);
|
|
}
|
|
ComponentBitSet.RemoveEntity(entityID);
|
|
}
|
|
|
|
public bool Any<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return Lookup<TComponent>().Count > 0;
|
|
}
|
|
|
|
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : unmanaged
|
|
{
|
|
return Lookup<TComponent>().All();
|
|
}
|
|
|
|
public void Clear<TComponent>() where TComponent : unmanaged
|
|
{
|
|
Lookup<TComponent>().Clear();
|
|
}
|
|
|
|
public virtual void ClearAllPriorities()
|
|
{
|
|
foreach (var store in _stores.Values)
|
|
{
|
|
store.ClearPriorities();
|
|
}
|
|
}
|
|
|
|
public virtual void ClearAll()
|
|
{
|
|
ComponentBitSet.Clear();
|
|
foreach (var store in _stores.Values)
|
|
{
|
|
store.Clear();
|
|
}
|
|
}
|
|
|
|
public void SwapWith(ComponentStore other)
|
|
{
|
|
(_stores, other._stores) = (other._stores, _stores);
|
|
(ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet);
|
|
}
|
|
|
|
public void UpdateUsing(ComponentDeltaStore delta)
|
|
{
|
|
foreach (var replayer in delta.CurrentReplayers)
|
|
{
|
|
replayer.Replay(this);
|
|
}
|
|
}
|
|
}
|
|
}
|