encompass-cs/encompass-cs/Collections/ComponentStore.cs

129 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
namespace Encompass
{
internal class ComponentStore
{
private Dictionary<Type, TypedComponentStore> Stores = new Dictionary<Type, TypedComponentStore>(512);
private ComponentBitSet componentBitSet = new ComponentBitSet();
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
{
foreach (var entry in Stores)
{
yield return (entry.Key, entry.Value);
}
}
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
if (!Stores.ContainsKey(typeof(TComponent)))
{
var store = new TypedComponentStore<TComponent>();
Stores.Add(typeof(TComponent), store);
componentBitSet.RegisterType<TComponent>();
}
}
public void FinishRegistering()
{
componentBitSet.FinishRegistering();
}
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
{
//RegisterComponentType<TComponent>();
return Stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
}
public bool Has<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return Lookup<TComponent>().Has(entity);
}
public bool Has(Type type, Entity entity)
{
return Stores.ContainsKey(type) && Stores[type].Has(entity);
}
public IEnumerable<Entity> EntitiesWithComponents(IEnumerable<Type> types)
{
return componentBitSet.EntitiesWithComponents(types);
}
public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return Lookup<TComponent>().Get(entity);
}
public void Set<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
Lookup<TComponent>().Set(entity, component);
componentBitSet.Set<TComponent>(entity);
}
public bool Set<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{
componentBitSet.Set<TComponent>(entity);
return Lookup<TComponent>().Set(entity, component, priority);
}
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
{
componentBitSet.RemoveComponent<TComponent>(entity);
Lookup<TComponent>().Remove(entity);
}
public void Remove(Entity entity)
{
foreach (var entry in Stores.Values)
{
entry.Remove(entity);
}
componentBitSet.DestroyEntity(entity);
}
public bool Any<TComponent>() where TComponent : struct, IComponent
{
return Lookup<TComponent>().Count > 0;
}
public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped()
{
foreach (var store in Stores.Values)
{
foreach (var thing in store.AllInterfaceTyped())
{
yield return thing;
}
}
}
public IEnumerable<(TComponent, Entity)> All<TComponent>() where TComponent : struct, IComponent
{
return Lookup<TComponent>().All();
}
public void Clear<TComponent>() where TComponent : struct, IComponent
{
Lookup<TComponent>().Clear();
}
public 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);
}
}
}