From f2af90bcb079a4180bc9e07d0e68536d5a0f2b8e Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 8 Nov 2025 14:33:01 +0100 Subject: [PATCH] ensured only one component per type can be present on a single entity --- src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs index a777a32..8fcd6b4 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs @@ -5,6 +5,7 @@ namespace EngineSharp.Core.ECS; 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> _logicComponents = new(); private readonly Dictionary> _dataComponents = new(); private readonly Dictionary> _renderComponents = new(); @@ -40,6 +41,9 @@ public class Scene } } + /// + /// If the component already exists on the entity, it will not be added + /// internal void AddComponent(Entity entity, T component) where T : class, IComponent { @@ -81,11 +85,14 @@ public class Scene return null; } + /// + /// If the component already exists on the entity, it will not be added + /// private static void AddComponent(long id, TComponent component, Dictionary> componentStore) where TArchetype : class, IComponent where TComponent : TArchetype { - if (componentStore.TryGetValue(id, out var components)) + if (componentStore.TryGetValue(id, out var components) && !components.OfType().Any()) { components.Add(component); }