GFramework/GFramework.Godot/system/AbstractResourceFactorySystem.cs
GwWuYou 2baa29aed6 feat(godot): 添加Godot游戏开发相关组件和系统
- 新增架构层支持,包括AbstractArchitecture和ArchitectureAnchorNode
- 实现拖拽功能组件AbstractDragDrop2DComponentBase和AbstractDragDropArea2DComponent
- 添加节点扩展方法类NodeExtensions,提供多种实用的节点操作方法
- 新增资源目录系统AbstractAssetCatalogSystem用于管理游戏资源
- 实现音频管理系统AbstractAudioManagerSystem支持背景音乐和音效播放
- 添加取消注册扩展方法UnRegisterExtension
- 创建GFramework.Game项目模块
- 重构项目结构,聚合核心模块并优化依赖引用
- [no tag]
2025-12-19 22:36:42 +08:00

110 lines
3.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.events;
using GFramework.Core.extensions;
using GFramework.Core.system;
using Godot;
namespace GFramework.Godot.system;
/// <summary>
/// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。
/// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。
/// </summary>
public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceFactorySystem
{
private ResourceFactory.Registry? _registry;
private IResourceLoadSystem? _resourceLoadSystem;
private IAssetCatalogSystem? _assetCatalogSystem;
/// <summary>
/// 系统初始化方法,在系统启动时执行一次。
/// 初始化资源注册表,并获取依赖的资源加载系统和资产目录系统。
/// 最后执行所有已注册资源的预加载操作。
/// </summary>
protected override void OnInit()
{
_registry = new ResourceFactory.Registry();
_resourceLoadSystem = this.GetSystem<IResourceLoadSystem>();
_assetCatalogSystem = this.GetSystem<IAssetCatalogSystem>();
// 监听架构初始化事件
this.RegisterEvent<ArchitectureEvents.ArchitectureInitializedEvent>(_ =>
{
// 注册资源
RegisterResources();
// 预加载所有资源
_registry.PreloadAll();
});
}
/// <summary>
/// 注册系统所需的各种资源类型。由子类实现具体注册逻辑。
/// </summary>
protected abstract void RegisterResources();
/// <summary>
/// 根据指定的键获取资源工厂函数。
/// </summary>
/// <typeparam name="T">资源类型</typeparam>
/// <param name="key">资源键</param>
/// <returns>返回创建指定类型资源的工厂函数</returns>
public Func<T> GetFactory<T>(string key)=>_registry!.ResolveFactory<T>(key);
/// <summary>
/// 根据资产目录映射信息获取资源工厂函数。
/// </summary>
/// <typeparam name="T">资源类型</typeparam>
/// <param name="mapping">资产目录映射信息</param>
/// <returns>返回创建指定类型资源的工厂函数</returns>
public Func<T> GetFactory<T>(AssetCatalog.AssetCatalogMapping mapping) => _registry!.ResolveFactory<T>(mapping.Key);
#region Register Helpers
/// <summary>
/// 注册场景资源工厂。
/// 根据场景键名获取场景ID并将场景加载工厂注册到注册表中。
/// </summary>
/// <typeparam name="T">场景节点类型必须继承自Node</typeparam>
/// <param name="sceneKey">场景在资产目录中的键名</param>
/// <param name="preload">是否需要预加载该场景资源</param>
protected void RegisterScene<T>(
string sceneKey,
bool preload = false)
where T : Node
{
var id = _assetCatalogSystem!.GetScene(sceneKey);
_registry!.Register(
sceneKey,
_resourceLoadSystem!.GetOrRegisterSceneFactory<T>(id),
preload
);
}
/// <summary>
/// 注册普通资源工厂。
/// 根据资源键名获取资源ID并将资源加载工厂注册到注册表中。
/// </summary>
/// <typeparam name="T">资源类型必须继承自Resource</typeparam>
/// <param name="resourceKey">资源在资产目录中的键名</param>
/// <param name="duplicate">是否需要复制资源实例</param>
/// <param name="preload">是否需要预加载该资源</param>
protected void RegisterResource<T>(
string resourceKey,
bool duplicate = false,
bool preload = false)
where T : Resource
{
var id = _assetCatalogSystem!.GetResource(resourceKey);
_registry!.Register(
resourceKey,
_resourceLoadSystem!.GetOrRegisterResourceFactory<T>(id, duplicate),
preload
);
}
#endregion
}