encompass-cs/encompass-cs/ComponentManager.cs

115 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2019-07-18 01:53:31 +00:00
using Encompass.Exceptions;
using Collections.Pooled;
2019-06-16 01:05:56 +00:00
namespace Encompass
{
internal class ComponentManager
{
2019-06-20 17:46:15 +00:00
private readonly DrawLayerManager drawLayerManager;
2019-12-05 20:10:33 +00:00
private readonly ComponentStore componentStore = new ComponentStore();
2019-12-05 20:10:33 +00:00
private readonly Dictionary<(Entity, Type), IComponent> componentWriteData = new Dictionary<(Entity, Type), IComponent>();
private readonly Dictionary<(Entity, Type), int> componentWritePriorities = new Dictionary<(Entity, Type), int>();
2019-12-05 20:10:33 +00:00
private readonly HashSet<Entity> entitiesMarkedForRemoval = new HashSet<Entity>();
2019-07-18 01:53:31 +00:00
public ComponentManager(DrawLayerManager drawLayerManager)
2019-06-19 23:13:02 +00:00
{
this.drawLayerManager = drawLayerManager;
2019-06-19 23:13:02 +00:00
}
2019-12-05 20:10:33 +00:00
internal void MarkComponentForWrite<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-12-05 20:10:33 +00:00
componentStore.RegisterComponentType<TComponent>();
if (componentWriteData.ContainsKey((entity, typeof(TComponent))))
{
var currentPriority = componentWritePriorities[(entity, typeof(TComponent))];
if (priority < currentPriority)
{
2019-12-05 20:10:33 +00:00
componentWriteData[(entity, typeof(TComponent))] = component;
componentWritePriorities[(entity, typeof(TComponent))] = priority;
}
}
else
{
2019-12-05 20:10:33 +00:00
componentWriteData.Add((entity, typeof(TComponent)), component);
componentWritePriorities[(entity, typeof(TComponent))] = priority;
}
}
internal void RegisterDrawableComponent<TComponent>(Guid componentID, TComponent component) where TComponent : IDrawableComponent
{
drawLayerManager.RegisterComponentWithLayer(componentID, component.Layer);
}
2019-12-05 20:10:33 +00:00
internal void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
2019-12-05 20:10:33 +00:00
componentStore.Set<TComponent>(entity, component);
}
internal void WriteComponents()
{
foreach (var keyValuePair in componentWriteData)
{
var (entity, type) = keyValuePair.Key;
2019-12-05 20:10:33 +00:00
var component = keyValuePair.Value;
2019-12-05 20:10:33 +00:00
AddComponent(entity, component);
}
componentWriteData.Clear();
componentWritePriorities.Clear();
}
2019-12-05 20:10:33 +00:00
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
2019-06-19 21:14:44 +00:00
{
2019-12-05 20:10:33 +00:00
return componentStore.All<TComponent>().Select(pair => pair.Item2);
2019-06-19 21:14:44 +00:00
}
2019-12-05 20:10:33 +00:00
internal TComponent GetComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-12-05 20:10:33 +00:00
return componentStore.Get<TComponent>(entity);
}
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-06-19 21:14:44 +00:00
{
2019-12-05 20:10:33 +00:00
return componentStore.Has<TComponent>(entity);
2019-06-19 23:13:02 +00:00
}
2019-08-01 23:44:29 +00:00
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-12-05 20:10:33 +00:00
return componentStore.Any<TComponent>();
}
2019-12-05 20:10:33 +00:00
internal void MarkAllComponentsOnEntityForRemoval(Entity entity)
{
2019-12-05 20:10:33 +00:00
entitiesMarkedForRemoval.Add(entity);
}
internal void RemoveMarkedComponents()
{
2019-12-05 20:10:33 +00:00
foreach (var entity in entitiesMarkedForRemoval)
{
2019-12-05 20:10:33 +00:00
componentStore.Remove(entity);
drawLayerManager.UnRegisterComponentWithLayer(entity);
}
2019-12-05 20:10:33 +00:00
entitiesMarkedForRemoval.Clear();
}
2019-12-05 20:10:33 +00:00
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-12-05 20:10:33 +00:00
componentStore.Remove<TComponent>(entity);
}
2019-06-20 03:37:46 +00:00
2019-12-05 20:10:33 +00:00
private void Remove(Entity entity)
2019-06-20 03:37:46 +00:00
{
2019-12-05 20:10:33 +00:00
componentStore.Remove(entity);
2019-06-20 03:37:46 +00:00
}
}
}