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