using System.Collections.Generic; using System.Linq; namespace Encompass { internal class ComponentManager { private readonly DrawLayerManager drawLayerManager; private readonly ComponentUpdateManager componentUpdateManager; private readonly ComponentStore componentStore = new ComponentStore(); private readonly HashSet entitiesMarkedForRemoval = new HashSet(); public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager) { this.drawLayerManager = drawLayerManager; this.componentUpdateManager = componentUpdateManager; } internal void SetComponentStore(ComponentStore componentStore) { this.componentStore.SwapWith(componentStore); } internal void RegisterDrawableComponent(Entity entity, TComponent component, int layer) where TComponent : struct, IComponent { drawLayerManager.RegisterComponentWithLayer(entity, component, layer); } internal void AddComponent(Entity entity, TComponent component) where TComponent : struct, IComponent { componentStore.Set(entity, component); } internal void WriteComponents() { componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore); } internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity() where TComponent : struct, IComponent { return componentStore.All().Select(pair => (pair.Item2, pair.Item1)); } internal IEnumerable GetComponentsByType() where TComponent : struct, IComponent { return componentStore.All().Select(pair => pair.Item2); } internal TComponent GetComponentByEntityAndType(Entity entity) where TComponent : struct, IComponent { return componentStore.Get(entity); } internal bool EntityHasComponentOfType(Entity entity) where TComponent : struct, IComponent { return componentStore.Has(entity); } internal bool ComponentOfTypeExists() where TComponent : struct, IComponent { return componentStore.Any(); } internal void MarkAllComponentsOnEntityForRemoval(Entity entity) { entitiesMarkedForRemoval.Add(entity); } internal void RemoveMarkedComponents() { foreach (var entity in entitiesMarkedForRemoval) { componentStore.Remove(entity); drawLayerManager.UnRegisterEntityWithLayer(entity); } entitiesMarkedForRemoval.Clear(); } public void Remove(Entity entity) where TComponent : struct, IComponent { componentUpdateManager.Remove(entity); drawLayerManager.UnRegisterComponentWithLayer(entity); } } }