ensured only one component per type can be present on a single entity

This commit is contained in:
2025-11-08 14:33:01 +01:00
parent 2f07a00173
commit f2af90bcb0

View File

@ -5,6 +5,7 @@ namespace EngineSharp.Core.ECS;
public class Scene public class Scene
{ {
// TODO: Maybe instead of a List, use a HashSet instead. Maybe implement a equality comparer to ensure, only one element per type can be present
private readonly Dictionary<long, List<LogicComponent>> _logicComponents = new(); private readonly Dictionary<long, List<LogicComponent>> _logicComponents = new();
private readonly Dictionary<long, List<DataComponent>> _dataComponents = new(); private readonly Dictionary<long, List<DataComponent>> _dataComponents = new();
private readonly Dictionary<long, List<RenderComponent>> _renderComponents = new(); private readonly Dictionary<long, List<RenderComponent>> _renderComponents = new();
@ -40,6 +41,9 @@ public class Scene
} }
} }
/// <summary>
/// If the component already exists on the entity, it will not be added
/// </summary>
internal void AddComponent<T>(Entity entity, T component) internal void AddComponent<T>(Entity entity, T component)
where T : class, IComponent where T : class, IComponent
{ {
@ -81,11 +85,14 @@ public class Scene
return null; return null;
} }
/// <summary>
/// If the component already exists on the entity, it will not be added
/// </summary>
private static void AddComponent<TComponent, TArchetype>(long id, TComponent component, Dictionary<long, List<TArchetype>> componentStore) private static void AddComponent<TComponent, TArchetype>(long id, TComponent component, Dictionary<long, List<TArchetype>> componentStore)
where TArchetype : class, IComponent where TArchetype : class, IComponent
where TComponent : TArchetype where TComponent : TArchetype
{ {
if (componentStore.TryGetValue(id, out var components)) if (componentStore.TryGetValue(id, out var components) && !components.OfType<TComponent>().Any())
{ {
components.Add(component); components.Add(component);
} }