forked from Misaki/GhostEngine
Refactor entity-component system and related classes
Changed the `Component` class to an interface `IComponentData` to support a data-oriented design. Changed the `Transform` class from a class to a struct, implementing `IComponentData` and updating properties. Changed the `GameObject` class to use a dictionary for components and added properties for state management. Changed the `PlayerLoopService` class to `GameLoopService` and updated methods to integrate with the new `SceneManager`. Changed the `World` class to manage multiple worlds and enhance entity management with new querying methods. Added the `Scene` class to manage root game objects and their lifecycle. Added new utility classes like `ComponentMask`, `Box<T>`, and `TypeHandle<T>` for better component management. Added the `ScriptComponent` class to allow for modular scriptable components attached to entities. Added the `QueryEnumerable` class to facilitate flexible querying of entities with specific components. Updated the `Test` class in `Program.cs` to demonstrate the new entity and component management system. Updated project files to include new references and settings supporting the changes made in the codebase.
This commit is contained in:
@@ -1,26 +1,13 @@
|
||||
|
||||
namespace Ghost.Engine.Services;
|
||||
namespace Ghost.Engine.Services;
|
||||
|
||||
internal static class GameLoopService
|
||||
internal static class PlayerLoopService
|
||||
{
|
||||
private readonly static HashSet<GameObject> _gameObjects = new();
|
||||
|
||||
private static Timer? _timer;
|
||||
private static bool _isRunning = false;
|
||||
|
||||
// TODO: Implement the actual time system
|
||||
public static float fixedDeltaTime = 0.02f;
|
||||
|
||||
public static void RegisterGameObject(GameObject gameObject)
|
||||
{
|
||||
_gameObjects.Add(gameObject);
|
||||
}
|
||||
|
||||
public static void UnregisterGameObject(GameObject gameObject)
|
||||
{
|
||||
_gameObjects.Remove(gameObject);
|
||||
}
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
if (_isRunning)
|
||||
@@ -28,13 +15,8 @@ internal static class GameLoopService
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var gameObject in _gameObjects)
|
||||
foreach (var gameObject in SceneManager.QueryRootGameObjects())
|
||||
{
|
||||
if (!gameObject.isActive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
gameObject.Start();
|
||||
}
|
||||
|
||||
@@ -48,27 +30,21 @@ internal static class GameLoopService
|
||||
|
||||
private static void Update()
|
||||
{
|
||||
foreach (var gameObject in _gameObjects)
|
||||
foreach (var gameObject in SceneManager.QueryRootGameObjects())
|
||||
{
|
||||
if (!gameObject.isActive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
gameObject.Update();
|
||||
}
|
||||
|
||||
foreach (var gameObject in SceneManager.QueryRootGameObjects())
|
||||
{
|
||||
gameObject.LateUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private static void FixedUpdate(object? state)
|
||||
{
|
||||
foreach (var gameObject in _gameObjects)
|
||||
foreach (var gameObject in SceneManager.QueryRootGameObjects())
|
||||
{
|
||||
if (!gameObject.isActive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
gameObject.FixedUpdate();
|
||||
}
|
||||
}
|
||||
50
Ghost.Engine/Services/SceneManager.cs
Normal file
50
Ghost.Engine/Services/SceneManager.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Ghost.Engine.Models;
|
||||
|
||||
namespace Ghost.Engine.Services;
|
||||
|
||||
public enum SceneLoadMode
|
||||
{
|
||||
Single,
|
||||
Additive
|
||||
}
|
||||
|
||||
public static class SceneManager
|
||||
{
|
||||
private readonly static HashSet<Scene> _activeScenes = new();
|
||||
|
||||
internal static IEnumerable<GameObject> QueryRootGameObjects()
|
||||
{
|
||||
foreach (var scene in _activeScenes)
|
||||
{
|
||||
foreach (var gameObject in scene.RootObjects)
|
||||
{
|
||||
if (!gameObject.IsActive)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return gameObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void LoadScene(Scene scene, SceneLoadMode loadMode)
|
||||
{
|
||||
if (loadMode == SceneLoadMode.Single)
|
||||
{
|
||||
foreach (var activeScene in _activeScenes)
|
||||
{
|
||||
activeScene.Unload();
|
||||
}
|
||||
_activeScenes.Clear();
|
||||
}
|
||||
|
||||
_activeScenes.Add(scene);
|
||||
scene.Load();
|
||||
}
|
||||
|
||||
public static Task LoadSceneAsync(Scene scene, SceneLoadMode loadMode)
|
||||
{
|
||||
return Task.Run(() => LoadScene(scene, loadMode));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user