mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 在ArchitectureContext中添加ECS世界和系统调度器支持 - 添加IEcsWorld和IEcsSystem接口定义 - 实现EcsWorld、EcsSystemBase和EcsSystemRunner核心类 - 添加Position和Velocity示例组件及MovementSystem示例 - 创建ECS使用示例代码 - 将多个项目的TargetFramework从netstandard2.0升级到netstandard2.1 - 添加Arch和Arch.System包依赖到核心项目 - 在测试项目中添加ECS相关接口的模拟实现
66 lines
1.5 KiB
C#
66 lines
1.5 KiB
C#
using GFramework.Core.Abstractions.ecs;
|
||
using GFramework.Core.system;
|
||
|
||
namespace GFramework.Core.ecs;
|
||
|
||
/// <summary>
|
||
/// ECS系统调度器,负责管理和更新所有ECS系统
|
||
/// </summary>
|
||
public sealed class EcsSystemRunner : AbstractSystem
|
||
{
|
||
private readonly List<IEcsSystem> _systems = new();
|
||
private bool _isRunning;
|
||
|
||
/// <summary>
|
||
/// 初始化调度器,从DI容器获取所有ECS系统
|
||
/// </summary>
|
||
protected override void OnInit()
|
||
{
|
||
// 从容器获取所有已注册的ECS系统
|
||
var systemsList = Context.GetService<IReadOnlyList<IEcsSystem>>();
|
||
if (systemsList != null && systemsList.Count > 0)
|
||
{
|
||
// 按优先级排序
|
||
_systems.AddRange(systemsList.OrderBy(s => s.Priority));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新所有ECS系统
|
||
/// </summary>
|
||
/// <param name="deltaTime">帧间隔时间</param>
|
||
public void Update(float deltaTime)
|
||
{
|
||
if (!_isRunning) return;
|
||
|
||
foreach (var system in _systems)
|
||
{
|
||
system.Update(deltaTime);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 启动调度器
|
||
/// </summary>
|
||
public void Start()
|
||
{
|
||
_isRunning = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 停止调度器
|
||
/// </summary>
|
||
public void Stop()
|
||
{
|
||
_isRunning = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 销毁调度器
|
||
/// </summary>
|
||
protected override void OnDestroy()
|
||
{
|
||
Stop();
|
||
_systems.Clear();
|
||
}
|
||
} |