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

129 lines
4.0 KiB
C#
Raw Normal View History

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();
2019-12-05 20:10:33 +00:00
2019-12-05 22:59:55 +00:00
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
2019-12-05 20:10:33 +00:00
{
2019-12-05 22:59:55 +00:00
foreach (var entry in Stores)
2019-12-05 20:10:33 +00:00
{
2019-12-05 22:59:55 +00:00
yield return (entry.Key, entry.Value);
2019-12-05 20:10:33 +00:00
}
}
2019-12-16 07:28:02 +00:00
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
if (!Stores.ContainsKey(typeof(TComponent)))
{
2019-12-05 20:10:33 +00:00
var store = new TypedComponentStore<TComponent>();
Stores.Add(typeof(TComponent), store);
componentBitSet.RegisterType<TComponent>();
}
2019-12-16 07:28:02 +00:00
}
public void FinishRegistering()
{
componentBitSet.FinishRegistering();
}
2019-12-16 07:28:02 +00:00
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
{
//RegisterComponentType<TComponent>();
2019-12-05 20:10:33 +00:00
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);
2019-12-05 20:10:33 +00:00
}
public IEnumerable<Entity> EntitiesWithComponents(IEnumerable<Type> types)
{
return componentBitSet.EntitiesWithComponents(types);
}
2019-12-05 20:10:33 +00:00
public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return Lookup<TComponent>().Get(entity);
}
2019-12-05 20:10:33 +00:00
public void Set<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
Lookup<TComponent>().Set(entity, component);
componentBitSet.Set<TComponent>(entity);
}
2019-12-05 22:59:55 +00:00
public bool Set<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
{
componentBitSet.Set<TComponent>(entity);
2019-12-05 22:59:55 +00:00
return Lookup<TComponent>().Set(entity, component, priority);
}
2019-12-05 20:10:33 +00:00
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
{
componentBitSet.RemoveComponent<TComponent>(entity);
2019-12-05 20:10:33 +00:00
Lookup<TComponent>().Remove(entity);
}
2019-12-05 20:10:33 +00:00
public void Remove(Entity entity)
{
2019-12-05 20:10:33 +00:00
foreach (var entry in Stores.Values)
{
entry.Remove(entity);
}
2019-12-20 19:28:56 +00:00
componentBitSet.RemoveEntity(entity);
}
2019-12-05 20:10:33 +00:00
public bool Any<TComponent>() where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return Lookup<TComponent>().Count > 0;
}
2019-12-05 22:59:55 +00:00
public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped()
{
foreach (var store in Stores.Values)
{
foreach (var thing in store.AllInterfaceTyped())
{
yield return thing;
}
}
}
2019-12-17 04:40:15 +00:00
public IEnumerable<(TComponent, Entity)> All<TComponent>() where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
return Lookup<TComponent>().All();
}
public void Clear<TComponent>() where TComponent : struct, IComponent
{
Lookup<TComponent>().Clear();
}
public void ClearAll()
{
componentBitSet.Clear();
2019-12-05 20:10:33 +00:00
foreach (var store in Stores.Values)
{
2019-12-05 20:10:33 +00:00
store.Clear();
}
}
2019-12-05 22:59:55 +00:00
public void SwapWith(ComponentStore other)
{
(Stores, other.Stores) = (other.Stores, Stores);
(componentBitSet, other.componentBitSet) = (other.componentBitSet, componentBitSet);
2019-12-05 22:59:55 +00:00
}
}
}