using Encompass.Collections; using System; using System.Collections.Generic; namespace Encompass { internal class ComponentStore { private Dictionary Stores = new Dictionary(512); public ComponentBitSet ComponentBitSet { get; private set; } public ComponentStore(Dictionary typeToIndex) { ComponentBitSet = new ComponentBitSet(typeToIndex); } 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); } } 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 BitSet EntityBitArray(Entity entity) { return ComponentBitSet.EntityBitArray(entity); } 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.RemoveEntity(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); } } }