encompass-cs/encompass-cs/ComponentManager.cs

106 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
namespace Encompass
{
internal class ComponentManager
{
private readonly DrawLayerManager drawLayerManager;
private readonly ComponentUpdateManager componentUpdateManager;
private readonly ComponentStore componentStore = new ComponentStore();
private readonly HashSet<Entity> entitiesMarkedForRemoval = new HashSet<Entity>();
public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager)
{
this.drawLayerManager = drawLayerManager;
this.componentUpdateManager = componentUpdateManager;
}
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
componentStore.RegisterComponentType<TComponent>();
}
public void FinishRegistering()
{
componentStore.FinishRegistering();
}
internal void SetComponentStore(ComponentStore componentStore)
{
this.componentStore.SwapWith(componentStore);
}
internal void RegisterDrawableComponent<TComponent>(Entity entity, TComponent component, int layer) where TComponent : struct, IComponent
{
drawLayerManager.RegisterComponentWithLayer(entity, component, layer);
}
internal void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
componentStore.Set(entity, component);
}
internal void WriteComponents()
{
componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore);
}
internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
return componentStore.All<TComponent>();
}
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
{
foreach (var pair in componentStore.All<TComponent>())
{
yield return pair.Item1;
}
}
internal TComponent GetComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentStore.Get<TComponent>(entity);
}
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentStore.Has<TComponent>(entity);
}
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
{
return componentStore.Any<TComponent>();
}
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<TComponent>(Entity entity) where TComponent : struct, IComponent
{
componentUpdateManager.Remove<TComponent>(entity);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity);
}
public IEnumerable<Entity> EntitiesWithComponents(IEnumerable<Type> types)
{
return componentStore.EntitiesWithComponents(types);
}
}
}