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:
89
Ghost.Entities/System.cs
Normal file
89
Ghost.Entities/System.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
namespace Ghost.Entities;
|
||||
|
||||
public abstract class SystemBase
|
||||
{
|
||||
public virtual int ExecutionOrder => 0;
|
||||
|
||||
public virtual bool Enable
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = true;
|
||||
|
||||
public World World
|
||||
{
|
||||
get;
|
||||
init;
|
||||
} = null!;
|
||||
|
||||
public virtual void OnCreate()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnDestroy()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class SystemStorage : IDisposable
|
||||
{
|
||||
private readonly List<SystemBase> _systems = new();
|
||||
private readonly List<SystemBase> _executionList = new();
|
||||
|
||||
public void AddSystem<T>(T system)
|
||||
where T : SystemBase
|
||||
{
|
||||
_systems.Add(system);
|
||||
if (system.Enable)
|
||||
{
|
||||
system.OnCreate();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveSystem<T>(T system)
|
||||
where T : SystemBase
|
||||
{
|
||||
_systems.Remove(system);
|
||||
if (system.Enable)
|
||||
{
|
||||
system.OnDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void RebuildExecutionList()
|
||||
{
|
||||
_executionList.Clear();
|
||||
_executionList.AddRange(_systems.OrderBy(s => s.ExecutionOrder));
|
||||
}
|
||||
|
||||
public void UpdateSystems()
|
||||
{
|
||||
foreach (var system in _systems)
|
||||
{
|
||||
if (!system.Enable)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
system.OnUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var system in _systems)
|
||||
{
|
||||
if (!system.Enable)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
system.OnDestroy();
|
||||
}
|
||||
|
||||
_systems.Clear();
|
||||
_executionList.Clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user