111 lines
3.9 KiB
C#
111 lines
3.9 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;
|
|
private readonly HashSet<int> entitiesMarkedForRemoval = new HashSet<int>();
|
|
|
|
internal ComponentBitSet ComponentBitSet { get { return componentStore.ComponentBitSet; } }
|
|
|
|
public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager, Dictionary<Type, int> typeToIndex)
|
|
{
|
|
this.drawLayerManager = drawLayerManager;
|
|
this.componentUpdateManager = componentUpdateManager;
|
|
componentStore = new ComponentStore(typeToIndex);
|
|
}
|
|
|
|
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
componentStore.RegisterComponentType<TComponent>();
|
|
}
|
|
|
|
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.ID, component, layer);
|
|
}
|
|
|
|
internal void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
componentStore.Set(entity.ID, component);
|
|
}
|
|
|
|
internal void WriteComponents()
|
|
{
|
|
componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore);
|
|
}
|
|
|
|
internal IEnumerable<(TComponent, int)> 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.ID);
|
|
}
|
|
|
|
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return componentStore.Has<TComponent>(entity.ID);
|
|
}
|
|
|
|
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentStore.Any<TComponent>();
|
|
}
|
|
|
|
internal void MarkAllComponentsOnEntityForRemoval(Entity entity)
|
|
{
|
|
entitiesMarkedForRemoval.Add(entity.ID);
|
|
}
|
|
|
|
internal void RemoveMarkedComponents()
|
|
{
|
|
foreach (var entityID in entitiesMarkedForRemoval)
|
|
{
|
|
componentStore.Remove(entityID);
|
|
drawLayerManager.UnRegisterEntityWithLayer(entityID);
|
|
}
|
|
|
|
entitiesMarkedForRemoval.Clear();
|
|
}
|
|
|
|
public bool RemoveImmediate<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
|
|
{
|
|
if (componentUpdateManager.RemoveImmediate<TComponent>(entity, priority))
|
|
{
|
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
|
|
{
|
|
if (componentUpdateManager.Remove<TComponent>(entity, priority))
|
|
{
|
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
|
|
}
|
|
}
|
|
}
|
|
}
|