Files
GhostEngine/Ghost.Entities/Systems/SystemStorage.cs
Misaki bab3be2508 Refactor project structure and improve performance
Changed the `ProjectRepository` class to be static for easier usage.
Changed `ProjectService` constants to public properties for accessibility.
Changed `App.xaml` to consolidate theme resources into `Override.xaml`.
Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management.
Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure.
Changed `ProjectPage` to use the `ExplorerItem` model for asset display.
Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface.
Changed the `Logger` class to introduce structured logging functionality.
Changed the system architecture to support dependency management for better organization.
Changed the `QueryEnumerable` class to allow for more flexible entity queries.
Changed the `TypeHandle` class to improve efficiency in retrieving type handles.
Changed the `World` class to support robust world management and multiple worlds.
Updated the `Test` class to demonstrate the new entity and component management system.
2025-06-05 21:45:50 +09:00

95 lines
2.2 KiB
C#

using System.Runtime.CompilerServices;
namespace Ghost.Entities.Systems;
[SkipLocalsInit]
public struct SystemStorage
{
private readonly List<Type> _systems = new();
private readonly List<ISystem> _executionList = new();
private readonly World _world;
public event Action<Type>? SystemAdded;
public event Action<Type>? SystemRemoved;
internal SystemStorage(World world)
{
_world = world;
}
public readonly void AddSystem(Type systemType)
{
_systems.Add(systemType);
SystemAdded?.Invoke(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);
SystemRemoved?.Invoke(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();
}
}