GFramework/GFramework.Godot/architecture/AbstractArchitecture.cs
GwWuYou d1cd0cfb05 feat(architecture): 引入架构阶段管理机制
新增 `ArchitecturePhase` 枚举及生命周期接口,支持在不同阶段执行相应逻辑。
实现基于阶段的状态转换控制与校验,增强架构初始化流程的可控性与扩展性。
添加模块安装接口 `IArchitectureModule` 及相关感知接口,便于插件化集成。
完善系统与模型注册限制,禁止在就绪后注册组件,提升运行时稳定性。
移除旧版补丁注册机制,统一通过生命周期钩子进行扩展。
2025-12-21 11:39:35 +08:00

63 lines
1.9 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 GFramework.Core.architecture;
using Godot;
namespace GFramework.Godot.architecture;
/// <summary>
/// 抽象架构类,为特定类型的架构提供基础实现框架
/// </summary>
/// <typeparam name="T">架构的具体类型必须继承自Architecture且能被实例化</typeparam>
public abstract class AbstractArchitecture<T> : Architecture<T> where T : Architecture<T>, new()
{
private const string ArchitectureName = "__GFramework__Architecture__Anchor";
/// <summary>
/// 初始化架构,按顺序注册模型、系统和工具
/// </summary>
protected override void Init()
{
RegisterModels();
RegisterSystems();
RegisterUtilities();
AttachToGodotLifecycle();
}
/// <summary>
/// 将架构绑定到Godot生命周期中确保在场景树销毁时能够正确清理资源
/// 通过创建一个锚节点来监听场景树的销毁事件
/// </summary>
private void AttachToGodotLifecycle()
{
if (Engine.GetMainLoop() is not SceneTree tree)
return;
// 防止重复挂载(热重载 / 多次 Init
if (tree.Root.GetNodeOrNull(ArchitectureName) != null)
return;
var anchor = new ArchitectureAnchorNode
{
Name = ArchitectureName
};
anchor.Bind(Destroy);
tree.Root.CallDeferred(Node.MethodName.AddChild, anchor);
}
/// <summary>
/// 注册工具抽象方法,由子类实现具体的工具注册逻辑
/// </summary>
protected abstract void RegisterUtilities();
/// <summary>
/// 注册系统抽象方法,由子类实现具体系统注册逻辑
/// </summary>
protected abstract void RegisterSystems();
/// <summary>
/// 注册模型抽象方法,由子类实现具体模型注册逻辑
/// </summary>
protected abstract void RegisterModels();
}