Files
GhostEngine/Ghost.Entities/Systems/SystemStorage.cs
Misaki 40d333b004 Refactor project structure and enhance functionality
Changed the project namespace from `Ghost.Editor` to `Ghost.App` across multiple files.
Changed the `InternalsVisibleTo` attribute in `AssemblyInfo.cs` to include `Ghost.App`.
Changed the `ProjectRepository` class to add new asynchronous methods for retrieving projects by ID, name, and metadata path.
Changed the `ProjectService` class to utilize the new asynchronous project loading methods.
Changed the `SceneGraph` classes to improve node management and serialization.
Changed the `EntityManager` class to enhance entity management with new component handling methods.
Added new test classes, `EntityTest` and `SerializationTest`, to ensure reliability in entity and serialization systems.
Added the `Ghost.App` project file to establish a modular project structure.
Added the `Ghost.Generator` project for automated component serialization code generation.
Updated UI components to reflect the new namespace for proper functionality.
2025-06-07 20:54:07 +09:00

93 lines
2.1 KiB
C#

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ghost.Entities.Systems;
[SkipLocalsInit]
public readonly struct SystemStorage
{
private readonly List<Type> _systems = new();
private readonly List<ISystem> _executionList = new();
private readonly World _world;
internal ReadOnlySpan<Type> Systems => CollectionsMarshal.AsSpan(_systems);
internal SystemStorage(World world)
{
_world = world;
}
public readonly void AddSystem(Type systemType)
{
_systems.Add(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddSystem<T>()
where T : ISystem, new()
{
AddSystem(typeof(T));
}
public readonly void RemoveSystem(Type systemType)
{
_systems.Remove(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void RemoveSystem<T>()
where T : ISystem, new()
{
RemoveSystem(typeof(T));
}
internal void CreateSystems()
{
var builder = new SystemDependencyBuilder(_systems);
builder.BuildDependencyGraph();
var executionOrder = builder.BuildExecutionOrder();
var state = new SystemState()
{
World = _world,
};
foreach (var systemType in executionOrder)
{
var system = (ISystem?)Activator.CreateInstance(systemType) ?? throw new InvalidOperationException($"Failed to create instance of system type {systemType.Name}.");
_executionList.Add(system);
system.OnCreate(in state);
}
}
internal void UpdateSystems()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnUpdate(in state);
}
}
internal void Dispose()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnDestroy(in state);
}
_systems.Clear();
_executionList.Clear();
}
}