diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/DataComponent.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/DataComponent.cs new file mode 100644 index 0000000..ed9af42 --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/DataComponent.cs @@ -0,0 +1,6 @@ +namespace EngineSharp.Core.ECS; + +public abstract class DataComponent : IComponent +{ + +} \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs index 3d12846..c8a91ff 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs @@ -1,11 +1,33 @@ -namespace EngineSharp.Core.ECS; +using System.ComponentModel; + +namespace EngineSharp.Core.ECS; // Maybe look at this C# ECS to get an idea of how to implement one myself // https://github.com/friflo/Friflo.Engine.ECS/tree/main +// OR: https://github.com/genaray/Arch public struct Entity { public readonly long Id; - private List _logicComponents; + private Scene _scene; + + internal Entity(long id, Scene scene) + { + this.Id = id; + _scene = scene; + } + + /// + /// If the component already exists on the entity, it will not be added. + /// + public void AddComponent(IComponent component) + { + _scene.AddComponent(this, component); + } + + public IComponent? GetComponent(IComponent component) + { + return _scene.GetComponent(this, component); + } } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/IComponent.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/IComponent.cs new file mode 100644 index 0000000..ba64139 --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/IComponent.cs @@ -0,0 +1,6 @@ +namespace EngineSharp.Core.ECS; + +public interface IComponent +{ + +} \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/LogicComponent.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/LogicComponent.cs index 7439bde..12b1532 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/LogicComponent.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/LogicComponent.cs @@ -2,10 +2,8 @@ // TODO: A LogicComponent would have a list of Components. A LogicComponent is basically what will be stored in the scene graph and all components of all LogicComponents will have their Update methods etc. be called -public abstract class LogicComponent +public abstract class LogicComponent : IComponent { - internal Entity _entity; // the entity this component belongs to - public abstract void Start(); public abstract void OnUpdate(double deltaTime); } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs new file mode 100644 index 0000000..32cd7ff --- /dev/null +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs @@ -0,0 +1,55 @@ +using System.ComponentModel; + +namespace EngineSharp.Core.ECS; + +public class Scene +{ + private Dictionary _logicComponents = new(); + private Dictionary _dataComponents = new(); + + internal void AddComponent(Entity entity, IComponent component) + { + switch (component) + { + case LogicComponent logic: + _logicComponents.TryAdd(entity.Id, logic); + break; + case DataComponent data: + _dataComponents.TryAdd(entity.Id, data); + break; + } + } + + internal IComponent? GetComponent(Entity entity, IComponent component) + { + switch (component) + { + case LogicComponent logic: + return GetComponent(entity.Id, logic); + case DataComponent data: + return GetComponent(entity.Id, data); + default: + return null; + } + } + + private LogicComponent? GetComponent(long id, LogicComponent component) + { + if (_logicComponents.TryGetValue(id, out var logic)) + { + return logic; + } + + return null; + } + + private DataComponent? GetComponent(long id, DataComponent component) + { + if (_dataComponents.TryGetValue(id, out var data)) + { + return data; + } + + return null; + } +} \ No newline at end of file