GFramework/GFramework.Core/ecs/EcsSystemBase.cs
GeWuYou 07e2a80de5 refactor(ecs): 将EcsSystemBase中的EcsWorld类型改为接口
- 将EcsWorld属性的类型从具体类EcsWorld改为接口IEcsWorld
- 修改OnInit方法中的服务获取逻辑以使用接口类型
- 提高代码的抽象性和可扩展性
2026-02-23 12:27:16 +08:00

63 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Arch.Core;
using GFramework.Core.Abstractions.ecs;
using GFramework.Core.extensions;
using GFramework.Core.system;
namespace GFramework.Core.ecs;
/// <summary>
/// ECS系统基类继承自AbstractSystem以集成到现有架构
/// </summary>
public abstract class EcsSystemBase : AbstractSystem, IEcsSystem
{
/// <summary>
/// ECS世界实例
/// </summary>
protected IEcsWorld EcsWorld { get; private set; } = null!;
/// <summary>
/// 快捷访问内部World
/// </summary>
protected World World => EcsWorld.InternalWorld;
/// <summary>
/// 系统优先级默认为0
/// </summary>
public virtual int Priority => 0;
/// <summary>
/// 每帧更新(子类实现)
/// </summary>
public abstract void Update(float deltaTime);
/// <summary>
/// 系统初始化
/// </summary>
protected override void OnInit()
{
EcsWorld = this.GetService<IEcsWorld>() ?? throw new InvalidOperationException(
"EcsWorld not found in context. Make sure ECS is properly initialized.");
OnEcsInit();
}
/// <summary>
/// 系统销毁
/// </summary>
protected override void OnDestroy()
{
OnEcsDestroy();
}
/// <summary>
/// ECS系统初始化子类实现
/// </summary>
protected abstract void OnEcsInit();
/// <summary>
/// ECS系统销毁子类可选实现
/// </summary>
protected virtual void OnEcsDestroy()
{
}
}