2019-12-20 19:10:42 +00:00
|
|
|
using System;
|
2019-06-14 03:28:26 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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-06 06:32:09 +00:00
|
|
|
private readonly ComponentUpdateManager componentUpdateManager;
|
2019-06-20 00:40:01 +00:00
|
|
|
|
2019-12-22 09:15:58 +00:00
|
|
|
private readonly ComponentStore componentStore;
|
2019-12-24 02:45:49 +00:00
|
|
|
private readonly HashSet<int> entitiesMarkedForRemoval = new HashSet<int>();
|
2019-07-18 01:53:31 +00:00
|
|
|
|
2019-12-22 09:15:58 +00:00
|
|
|
public ComponentManager(DrawLayerManager drawLayerManager, ComponentUpdateManager componentUpdateManager, Dictionary<Type, int> typeToIndex)
|
2019-06-19 23:13:02 +00:00
|
|
|
{
|
2019-06-20 00:40:01 +00:00
|
|
|
this.drawLayerManager = drawLayerManager;
|
2019-12-06 06:32:09 +00:00
|
|
|
this.componentUpdateManager = componentUpdateManager;
|
2019-12-22 09:15:58 +00:00
|
|
|
componentStore = new ComponentStore(typeToIndex);
|
2019-06-19 23:13:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 07:28:02 +00:00
|
|
|
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
|
|
|
{
|
|
|
|
componentStore.RegisterComponentType<TComponent>();
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
internal void SetComponentStore(ComponentStore componentStore)
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-12-05 22:59:55 +00:00
|
|
|
this.componentStore.SwapWith(componentStore);
|
2019-08-11 00:34:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
internal void RegisterDrawableComponent<TComponent>(Entity entity, TComponent component, int layer) where TComponent : struct, IComponent
|
2019-08-11 00:34:00 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
drawLayerManager.RegisterComponentWithLayer(entity.ID, component, layer);
|
2019-08-11 00:34:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
internal void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
2019-08-21 22:13:48 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
componentStore.Set(entity.ID, component);
|
2019-06-20 05:38:56 +00:00
|
|
|
}
|
2019-06-20 00:40:01 +00:00
|
|
|
|
2019-08-21 02:25:59 +00:00
|
|
|
internal void WriteComponents()
|
2019-06-20 05:38:56 +00:00
|
|
|
{
|
2019-12-06 06:32:09 +00:00
|
|
|
componentStore.SwapWith(componentUpdateManager.UpToDateComponentStore);
|
2019-12-05 22:59:55 +00:00
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
|
2019-12-24 02:45:49 +00:00
|
|
|
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
2019-12-05 22:59:55 +00:00
|
|
|
{
|
2019-12-17 04:40:15 +00:00
|
|
|
return componentStore.All<TComponent>();
|
2019-08-11 00:34:00 +00:00
|
|
|
}
|
2019-08-02 06:09:41 +00:00
|
|
|
|
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-17 04:40:15 +00:00
|
|
|
foreach (var pair in componentStore.All<TComponent>())
|
|
|
|
{
|
|
|
|
yield return pair.Item1;
|
|
|
|
}
|
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-24 02:45:49 +00:00
|
|
|
return componentStore.Get<TComponent>(entity.ID);
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 00:34:00 +00:00
|
|
|
internal bool EntityHasComponentOfType<TComponent>(Entity entity) where TComponent : struct, IComponent
|
2019-06-19 21:14:44 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
return componentStore.Has<TComponent>(entity.ID);
|
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-07-18 01:12:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
internal void MarkAllComponentsOnEntityForRemoval(Entity entity)
|
2019-07-18 01:12:29 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
entitiesMarkedForRemoval.Add(entity.ID);
|
2019-07-18 01:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void RemoveMarkedComponents()
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
foreach (var entityID in entitiesMarkedForRemoval)
|
2019-07-18 01:12:29 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
componentStore.Remove(entityID);
|
|
|
|
drawLayerManager.UnRegisterEntityWithLayer(entityID);
|
2019-07-18 01:12:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
entitiesMarkedForRemoval.Clear();
|
2019-07-18 01:12:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 03:04:26 +00:00
|
|
|
public bool RemoveImmediate<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
|
2019-06-16 01:05:56 +00:00
|
|
|
{
|
2019-12-24 03:04:26 +00:00
|
|
|
if (componentUpdateManager.RemoveImmediate<TComponent>(entity, priority))
|
2019-12-24 02:01:49 +00:00
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
|
2019-12-24 02:01:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-27 05:25:24 +00:00
|
|
|
public void Remove<TComponent>(Entity entity, int priority) where TComponent : struct, IComponent
|
2019-12-24 02:01:49 +00:00
|
|
|
{
|
|
|
|
if (componentUpdateManager.Remove<TComponent>(entity, priority))
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entity.ID);
|
2019-12-24 02:01:49 +00:00
|
|
|
}
|
2019-06-14 03:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|