diff --git a/GFramework.Game/setting/README.md b/GFramework.Game/setting/README.md new file mode 100644 index 0000000..12a3f1a --- /dev/null +++ b/GFramework.Game/setting/README.md @@ -0,0 +1,204 @@ +# 设置系统 (Settings System) + +## 概述 + +设置系统是 GFramework.Game 的核心组件之一,负责管理游戏中各种设置配置。该系统采用了模型-系统分离的设计模式,支持设置部分(Section)的管理和设置应用器模式。 + +## 核心类 + +### SettingsModel + +设置模型类,继承自 `AbstractModel` 并实现 `ISettingsModel` 接口。 + +**主要功能:** + +- 管理不同类型的设置部分(Settings Section) +- 提供类型安全的设置访问 +- 支持可应用设置对象的注册 + +**关键方法:** + +- `Get()` - 获取或创建指定类型的设置部分 +- `TryGet(Type, out ISettingsSection)` - 尝试获取设置部分 +- `Register(IApplyAbleSettings)` - 注册可应用的设置对象 +- `All()` - 获取所有设置部分 + +### SettingsSystem + +设置系统类,继承自 `AbstractSystem` 并实现 `ISettingsSystem` 接口。 + +**主要功能:** + +- 应用设置配置到相应系统 +- 支持单个或批量设置应用 +- 自动识别可应用设置类型 + +**关键方法:** + +- `ApplyAll()` - 应用所有设置配置 +- `Apply()` - 应用指定类型的设置 +- `Apply(IEnumerable)` - 应用指定类型集合的设置 + +## 架构设计 + +```mermaid +graph TD + A[ISettingsModel] --> B[SettingsModel] + C[ISettingsSystem] --> D[SettingsSystem] + + B --> E[Dictionary] + D --> B + + F[ISettingsSection] --> G[IApplyAbleSettings] + H[AudioSettings] --> G + I[GraphicsSettings] --> G + + E --> H + E --> I + + J[Application] --> D + D --> G +``` + +## 使用示例 + +### 基本使用 + +```csharp +// 获取设置模型 +var settingsModel = this.GetModel(); + +// 获取或创建音频设置 +var audioSettings = settingsModel.Get(); +audioSettings.MasterVolume = 0.8f; +audioSettings.BgmVolume = 0.6f; +audioSettings.SfxVolume = 0.9f; + +// 注册设置到模型 +settingsModel.Register(audioSettings); +``` + +### 应用设置 + +```csharp +// 获取设置系统 +var settingsSystem = this.GetSystem(); + +// 应用所有设置 +await settingsSystem.ApplyAll(); + +// 应用特定类型设置 +await settingsSystem.Apply(); + +// 应用多个类型设置 +var types = new[] { typeof(GodotAudioSettings), typeof(GodotGraphicsSettings) }; +await settingsSystem.Apply(types); +``` + +### 创建自定义设置 + +```csharp +/// +/// 游戏设置类 +/// +public class GameSettings : ISettingsSection +{ + public float GameSpeed { get; set; } = 1.0f; + public int Difficulty { get; set; } = 1; + public bool AutoSave { get; set; } = true; +} + +// 使用自定义设置 +var gameSettings = settingsModel.Get(); +gameSettings.GameSpeed = 1.5f; +``` + +### 创建可应用设置 + +```csharp +/// +/// 游戏设置应用器 +/// +public class GameSettings : ISettingsSection, IApplyAbleSettings +{ + public float GameSpeed { get; set; } = 1.0f; + public int Difficulty { get; set; } = 1; + + public Task Apply() + { + // 应用游戏速度 + Time.timeScale = GameSpeed; + + // 应用难度设置 + GameDifficulty.Current = Difficulty; + + return Task.CompletedTask; + } +} +``` + +## 接口定义 + +### ISettingsSection + +```csharp +public interface ISettingsSection +{ + // 设置部分的标识接口 +} +``` + +### IApplyAbleSettings + +```csharp +public interface IApplyAbleSettings : ISettingsSection +{ + Task Apply(); +} +``` + +### ISettingsModel + +```csharp +public interface ISettingsModel +{ + T Get() where T : class, ISettingsSection, new(); + bool TryGet(Type type, out ISettingsSection section); + IEnumerable All(); + void Register(IApplyAbleSettings applyAble); +} +``` + +### ISettingsSystem + +```csharp +public interface ISettingsSystem +{ + Task ApplyAll(); + Task Apply() where T : class, ISettingsSection; + Task Apply(Type settingsType); + Task Apply(IEnumerable settingsTypes); +} +``` + +## 设计模式 + +该系统使用了以下设计模式: + +1. **Repository Pattern** - SettingsModel 作为设置数据的仓库 +2. **Command Pattern** - IApplyAbleSettings 的 Apply 方法作为命令 +3. **Factory Pattern** - Get() 方法创建设置实例 +4. **Template Method** - AbstractSystem 提供初始化模板 + +## 最佳实践 + +1. **设置分类** - 将相关设置组织到同一个设置类中 +2. **延迟应用** - 批量修改后再应用,而不是每次修改都应用 +3. **类型安全** - 使用泛型方法确保类型安全 +4. **可测试性** - 通过接口实现便于单元测试 + +## 相关链接 + +- [Godot 设置模块](../../GFramework.Godot/setting/README.md) +- [存储模块](../../GFramework.Godot/storage/README.md) +- [抽象接口定义](../../../GFramework.Core/Abstractions/) \ No newline at end of file diff --git a/GFramework.Godot/GFramework.Godot.csproj b/GFramework.Godot/GFramework.Godot.csproj index df05907..fd5c89c 100644 --- a/GFramework.Godot/GFramework.Godot.csproj +++ b/GFramework.Godot/GFramework.Godot.csproj @@ -17,9 +17,4 @@ - - - - - diff --git a/GFramework.Godot/extensions/README.md b/GFramework.Godot/extensions/README.md new file mode 100644 index 0000000..737c3fa --- /dev/null +++ b/GFramework.Godot/extensions/README.md @@ -0,0 +1,335 @@ +# Godot 扩展方法 (Godot Extensions) + +## 概述 + +Godot 扩展方法模块为 Godot 引擎提供了丰富的便捷扩展方法集合。这些扩展方法简化了常见的 Godot +开发任务,提高了代码的可读性和开发效率。该模块遵循流畅接口设计原则,支持链式调用。 + +## 模块结构 + +```mermaid +graph TD + A[Extensions] --> B[GodotPathExtensions] + A --> C[NodeExtensions] + A --> D[SignalFluentExtensions] + A --> E[UnRegisterExtension] + D --> F[SignalBuilder] + + B --> G[路径判断扩展] + C --> H[节点生命周期] + C --> I[节点查询] + C --> J[场景树操作] + C --> K[输入控制] + C --> L[调试工具] + D --> M[信号连接系统] + E --> N[事件管理] +``` + +## 扩展模块详解 + +### 1. 路径扩展 (GodotPathExtensions) + +提供 Godot 虚拟路径的判断和识别功能。 + +**主要方法:** + +- `IsUserPath()` - 判断是否为 `user://` 路径 +- `IsResPath()` - 判断是否为 `res://` 路径 +- `IsGodotPath()` - 判断是否为 Godot 虚拟路径 + +**使用示例:** + +```csharp +string savePath = "user://save.dat"; +string configPath = "res://config.json"; +string logPath = "C:/logs/debug.log"; + +if (savePath.IsUserPath()) Console.WriteLine("用户数据路径"); +if (configPath.IsResPath()) Console.WriteLine("资源路径"); +if (logPath.IsGodotPath()) Console.WriteLine("Godot 虚拟路径"); +else Console.WriteLine("文件系统路径"); +``` + +### 2. 节点扩展 (NodeExtensions) + +最丰富的扩展模块,提供全面的节点操作功能。 + +#### 节点生命周期管理 + +```csharp +// 安全释放节点 +node.QueueFreeX(); // 延迟释放 +node.FreeX(); // 立即释放 + +// 等待节点就绪 +await node.WaitUntilReady(); + +// 检查节点有效性 +if (node.IsValidNode()) Console.WriteLine("节点有效"); +if (node.IsInvalidNode()) Console.WriteLine("节点无效"); +``` + +#### 节点查询操作 + +```csharp +// 查找子节点 +var sprite = node.FindChildX("Sprite"); +var parent = node.GetParentX(); + +// 获取或创建节点 +var panel = parent.GetOrCreateNode("MainPanel"); + +// 遍历子节点 +node.ForEachChild(sprite => { + sprite.Modulate = Colors.White; +}); +``` + +#### 场景树操作 + +```csharp +// 获取根节点 +var root = node.GetRootNodeX(); + +// 异步添加子节点 +await parent.AddChildX(childNode); + +// 设置场景树暂停状态 +node.Paused(true); // 暂停 +node.Paused(false); // 恢复 +``` + +#### 输入控制 + +```csharp +// 标记输入事件已处理 +node.SetInputAsHandled(); + +// 禁用/启用输入 +node.DisableInput(); +node.EnableInput(); +``` + +#### 调试工具 + +```csharp +// 打印节点路径 +node.LogNodePath(); + +// 打印节点树 +node.PrintTreeX(); + +// 安全延迟调用 +node.SafeCallDeferred("UpdateUI"); +``` + +#### 类型转换 + +```csharp +// 安全的类型转换 +var button = node.OfType