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

134 lines
4.1 KiB
C#
Raw Normal View History

2019-12-23 00:21:07 +00:00
using Encompass.Collections;
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; }
2019-12-22 09:15:58 +00:00
public ComponentStore(Dictionary<Type, int> typeToIndex)
{
ComponentBitSet = new ComponentBitSet(typeToIndex);
2019-12-22 09:15:58 +00:00
}
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);
}
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 BitSet512 EntityBitArray(Entity entity)
{
return ComponentBitSet.EntityBitArray(entity);
}
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);
}
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
}
}
}