diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs index c8a91ff..24fb507 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs @@ -9,9 +9,10 @@ namespace EngineSharp.Core.ECS; public struct Entity { public readonly long Id; - private Scene _scene; - + + public string Name { get; set; } + internal Entity(long id, Scene scene) { this.Id = id; diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs index 32cd7ff..745eed9 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Scene.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.Linq; namespace EngineSharp.Core.ECS; @@ -7,6 +7,29 @@ public class Scene private Dictionary _logicComponents = new(); private Dictionary _dataComponents = new(); + private long _nextSceneId; // ugly solution but it works + + public Entity CreateEntity(string name) + { + return new Entity(_nextSceneId++, this); + } + + internal void StartComponents() + { + foreach (var component in _logicComponents.Values) + { + component.Start(); + } + } + + internal void UpdateComponents(double deltaTime) + { + foreach (var component in _logicComponents.Values) + { + component.OnUpdate(deltaTime); + } + } + internal void AddComponent(Entity entity, IComponent component) { switch (component) diff --git a/src/EngineSharp.Core/EngineSharp.Core/IEngine.cs b/src/EngineSharp.Core/EngineSharp.Core/IEngine.cs index b885ca7..d716868 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/IEngine.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/IEngine.cs @@ -1,4 +1,5 @@ -using Silk.NET.Windowing; +using EngineSharp.Core.ECS; +using Silk.NET.Windowing; namespace EngineSharp.Core; @@ -6,6 +7,8 @@ public interface IEngine { void Start(); void Stop(); + + Scene CreateScene(); } public enum GraphicsAPI diff --git a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs index d0906e7..0cca0c7 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs @@ -1,4 +1,5 @@ using System.Drawing; +using EngineSharp.Core.ECS; using Silk.NET.Input; using Silk.NET.Maths; using Silk.NET.OpenGL; @@ -9,9 +10,11 @@ namespace EngineSharp.Core; internal class OpenGLEngine : IEngine { private readonly IWindow _window; + private readonly List _scenes; private GL _gl = null!; // because between constructing the engine and OnLoad being called nothing should be able to call these fields. Therefore, we do this to get rid of warnings private Input _input = null!; private PerspectiveCamera _camera = null!; + private Scene? _currentScene; public OpenGLEngine(WindowOptions windowOptions) { @@ -20,6 +23,8 @@ internal class OpenGLEngine : IEngine _window.Update += OnUpdate; _window.Render += OnRender; _window.Resize += OnResize; + + _scenes = new(); } public void Start() @@ -32,6 +37,19 @@ internal class OpenGLEngine : IEngine _window.Close(); } + public Scene CreateScene() + { + var scene = new Scene(); + _scenes.Add(scene); + return scene; + } + + public void SetCurrentScene(Scene scene) + { + _currentScene = scene; + _currentScene.StartComponents(); + } + private void OnLoad() { _gl = _window.CreateOpenGL(); @@ -49,7 +67,11 @@ internal class OpenGLEngine : IEngine { _window.Close(); } - // TODO: here call custom code / game logic + + if (_currentScene is not null) + { + _currentScene.UpdateComponents(deltaTime); + } } private void OnRender(double deltaTime)