mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 将 AbstractAssetCatalogSystem 从 GFramework.Godot 移动到 GFramework.Game - 引入 GameUnitId、TemplateId 和 AssetId 替代原有的 SceneId 和 ResourceId - 更新注册与查询接口以区分不同类型资源 - 修改相关系统类以适配新的资产标识符类型 - 调整项目引用依赖关系,确保正确的程序集链接 - 扩展资源工厂系统以处理新增的资源类别 - [no tag]
125 lines
4.4 KiB
C#
125 lines
4.4 KiB
C#
using GFramework.Core.events;
|
||
using GFramework.Core.extensions;
|
||
using GFramework.Core.system;
|
||
using GFramework.Game.assets;
|
||
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>
|
||
/// 注册游戏单位资源到资源管理系统中
|
||
/// </summary>
|
||
/// <typeparam name="T">游戏单位类型,必须继承自Node</typeparam>
|
||
/// <param name="sceneKey">场景键值,用于标识特定的游戏单位资源</param>
|
||
/// <param name="preload">是否预加载该资源,默认为false</param>
|
||
public void RegisterGameUnit<T>(
|
||
string sceneKey,
|
||
bool preload = false)
|
||
where T : Node
|
||
{
|
||
var id = _assetCatalogSystem!.GetGameUnit(sceneKey);
|
||
|
||
_registry!.Register(
|
||
sceneKey,
|
||
_resourceLoadSystem!.GetOrRegisterGameUnitFactory<T>(id),
|
||
preload
|
||
);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册模板资源到资源管理系统中
|
||
/// </summary>
|
||
/// <typeparam name="T">模板类型,必须继承自Node</typeparam>
|
||
/// <param name="templateKey">模板键值,用于标识特定的模板资源</param>
|
||
/// <param name="preload">是否预加载该资源,默认为false</param>
|
||
public void RegisterTemplate<T>(
|
||
string templateKey,
|
||
bool preload = false)
|
||
where T : Node
|
||
{
|
||
var id = _assetCatalogSystem!.GetTemplate(templateKey);
|
||
|
||
_registry!.Register(
|
||
templateKey,
|
||
_resourceLoadSystem!.GetOrRegisterTemplateFactory<T>(id),
|
||
preload
|
||
);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册通用资产资源到资源管理系统中
|
||
/// </summary>
|
||
/// <typeparam name="T">资产类型,必须继承自Resource</typeparam>
|
||
/// <param name="assetKey">资产键值,用于标识特定的资产资源</param>
|
||
/// <param name="preload">是否预加载该资源,默认为false</param>
|
||
public void RegisterAsset<T>(
|
||
string assetKey,
|
||
bool preload = false)
|
||
where T : Resource
|
||
{
|
||
var id = _assetCatalogSystem!.GetAsset(assetKey);
|
||
|
||
_registry!.Register(
|
||
assetKey,
|
||
_resourceLoadSystem!.GetOrRegisterAssetFactory<T>(id),
|
||
preload
|
||
);
|
||
}
|
||
|
||
#endregion
|
||
} |