finalised crude ECS. Should now be able to create custom logic that gets executed each frame without touching the Engine project

This commit is contained in:
2025-09-08 20:46:59 +02:00
parent 97207ab2a3
commit 24cf84491f
4 changed files with 54 additions and 5 deletions

View File

@ -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;

View File

@ -1,4 +1,4 @@
using System.ComponentModel;
using System.Linq;
namespace EngineSharp.Core.ECS;
@ -7,6 +7,29 @@ public class Scene
private Dictionary<long, LogicComponent> _logicComponents = new();
private Dictionary<long, DataComponent> _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)

View File

@ -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

View File

@ -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<Scene> _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)