refactor(architecture): 重构Godot架构扩展机制为模块机制

将原有的IGodotArchitectureExtension接口及其实现统一替换为IGodotModule接口,
以提供更清晰、更具扩展性的模块化支持。同时更新了相关字段和方法命名,
使其语义更加准确,提升代码可读性和维护性。
This commit is contained in:
GwWuYou 2025-12-21 13:31:39 +08:00
parent 86053f2aee
commit 259c4411c7
3 changed files with 42 additions and 21 deletions

View File

@ -21,7 +21,7 @@ public abstract class AbstractArchitecture<T> : Architecture<T> where T : Archit
/// 存储所有已安装的Godot架构扩展组件列表
/// 用于在架构销毁时正确清理所有扩展资源
/// </summary>
private readonly List<IGodotArchitectureExtension<T>> _extensions = [];
private readonly List<IGodotModule<T>> _extensions = [];
/// <summary>
/// 架构锚点节点引用
@ -81,32 +81,36 @@ public abstract class AbstractArchitecture<T> : Architecture<T> where T : Archit
tree.Root.CallDeferred(Node.MethodName.AddChild, _anchor);
}
/// <summary>
/// 安装Godot架构扩展组件
/// 安装Godot模块扩展
/// </summary>
/// <param name="extension">要安装的Godot架构扩展实例必须实现IGodotArchitectureExtension接口</param>
/// <returns>异步任务,表示扩展安装过程</returns>
protected async Task InstallGodotExtension(IGodotArchitectureExtension<T> extension)
/// <typeparam name="TModule">模块类型必须实现IGodotModule接口</typeparam>
/// <param name="module">要安装的模块实例</param>
/// <returns>异步任务</returns>
protected async Task InstallGodotModule<TModule>(TModule module) where TModule : IGodotModule<T>
{
module.Install(this);
// 检查锚点是否已初始化,未初始化则抛出异常
if (_anchor == null)
throw new InvalidOperationException("Anchor not initialized");
// 等待锚点准备就绪
await _anchor.WaitUntilReady();
// 延迟调用将扩展节点添加为锚点的子节点
_anchor.CallDeferred(Node.MethodName.AddChild, extension.Node);
_anchor.CallDeferred(Node.MethodName.AddChild, module.Node);
// 调用扩展的附加回调方法
extension.OnAttach(this);
module.OnAttach(this);
// 将扩展添加到扩展集合中
_extensions.Add(extension);
_extensions.Add(module);
}
/// <summary>
/// 销毁架构及其相关资源。
/// 调用所有已安装扩展的OnDetach方法并清空扩展列表。

View File

@ -1,11 +0,0 @@
using GFramework.Core.architecture;
using Godot;
namespace GFramework.Godot.architecture;
public interface IGodotArchitectureExtension<T> where T : Architecture<T>, new()
{
Node Node { get; }
void OnAttach(Architecture<T> architecture);
void OnDetach();
}

View File

@ -0,0 +1,28 @@
using GFramework.Core.architecture;
using Godot;
namespace GFramework.Godot.architecture;
/// <summary>
/// Godot模块接口定义了Godot引擎中模块的基本行为和属性
/// </summary>
/// <typeparam name="T">架构类型必须继承自Architecture&lt;T&gt;且具有无参构造函数</typeparam>
public interface IGodotModule<T> : IArchitectureModule where T : Architecture<T>, new()
{
/// <summary>
/// 获取模块关联的Godot节点
/// </summary>
Node Node { get; }
/// <summary>
/// 当模块被附加到架构时调用
/// </summary>
/// <param name="architecture">要附加到的架构实例</param>
void OnAttach(Architecture<T> architecture);
/// <summary>
/// 当模块从架构分离时调用
/// </summary>
void OnDetach();
}