From 25f7779b4e687271777e4d675645f5b9eab74a66 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sun, 8 Mar 2026 11:37:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(docs):=20=E5=B0=86=20Context=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E8=AE=BF=E9=97=AE=E6=9B=BF=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 Context.GetModel() 调用替换为 this.GetModel() - 将 Context.GetSystem() 调用替换为 this.GetSystem() - 将 Context.GetUtility() 调用替换为 this.GetUtility() - 将 Context.SendCommand() 调用替换为 this.SendCommand() - 将 Context.SendQuery() 调用替换为 this.SendQuery() - 将 Context.SendEvent() 调用替换为 this.SendEvent() - 将 Context.RegisterEvent() 调用替换为 this.RegisterEvent() --- .../controller/IController.cs | 12 +++---- .../best-practices/architecture-patterns.md | 28 +++++++-------- docs/zh-CN/best-practices/index.md | 18 +++++----- docs/zh-CN/best-practices/performance.md | 8 ++--- docs/zh-CN/core/architecture.md | 10 +++--- docs/zh-CN/core/command.md | 4 +-- docs/zh-CN/core/configuration.md | 4 +-- docs/zh-CN/core/ecs.md | 2 +- docs/zh-CN/core/events.md | 14 ++++---- docs/zh-CN/core/pause.md | 8 ++--- docs/zh-CN/core/property.md | 6 ++-- docs/zh-CN/core/query.md | 4 +-- docs/zh-CN/core/state-machine.md | 6 ++-- docs/zh-CN/core/system.md | 4 +-- docs/zh-CN/game/data.md | 22 ++++++------ docs/zh-CN/game/index.md | 28 +++++++-------- docs/zh-CN/game/scene.md | 22 ++++++------ docs/zh-CN/game/serialization.md | 14 ++++---- docs/zh-CN/game/ui.md | 28 +++++++-------- docs/zh-CN/getting-started/quick-start.md | 10 +++--- docs/zh-CN/godot/architecture.md | 24 +++++-------- docs/zh-CN/godot/coroutine.md | 8 ++--- docs/zh-CN/godot/index.md | 20 +++++------ .../context-aware-generator.md | 34 +++++++++---------- docs/zh-CN/source-generators/index.md | 30 ++++++++-------- docs/zh-CN/tutorials/advanced-patterns.md | 34 +++++++++---------- .../zh-CN/tutorials/godot-complete-project.md | 14 ++++---- docs/zh-CN/tutorials/godot-integration.md | 24 ++++++------- docs/zh-CN/tutorials/pause-system.md | 18 +++++----- .../zh-CN/tutorials/state-machine-tutorial.md | 2 +- docs/zh-CN/tutorials/unit-testing.md | 2 +- 31 files changed, 228 insertions(+), 234 deletions(-) diff --git a/GFramework.Core.Abstractions/controller/IController.cs b/GFramework.Core.Abstractions/controller/IController.cs index 050f474..3fd94f3 100644 --- a/GFramework.Core.Abstractions/controller/IController.cs +++ b/GFramework.Core.Abstractions/controller/IController.cs @@ -9,7 +9,7 @@ /// 它的作用是标识一个类是控制器,用于协调 Model、System 和 UI 之间的交互。 /// /// -/// 架构访问:控制器通常需要访问架构上下文。使用 [ContextAware] 特性 +/// 架构访问 :控制器通常需要访问架构上下文。使用 [ContextAware] 特性 /// 自动生成上下文访问能力: /// /// @@ -20,19 +20,19 @@ /// { /// public void Initialize() /// { -/// // Context 属性由 [ContextAware] 自动生成 -/// var playerModel = Context.GetModel<PlayerModel>(); -/// var gameSystem = Context.GetSystem<GameSystem>(); +/// // [ContextAware] 实现 IContextAware 接口,可使用扩展方法 +/// var playerModel = this.GetModel<PlayerModel>(); +/// var gameSystem = this.GetSystem<GameSystem>(); /// } /// } /// /// -/// 注意: +/// 注意: /// /// /// 必须添加 partial 关键字 /// [ContextAware] 特性会自动实现 IContextAware 接口 -/// Context 属性提供架构上下文访问 +/// 可使用 this.GetModel()、this.GetSystem() 等扩展方法访问架构 /// /// public interface IController; \ No newline at end of file diff --git a/docs/zh-CN/best-practices/architecture-patterns.md b/docs/zh-CN/best-practices/architecture-patterns.md index 316a65e..e743ca3 100644 --- a/docs/zh-CN/best-practices/architecture-patterns.md +++ b/docs/zh-CN/best-practices/architecture-patterns.md @@ -92,7 +92,7 @@ public partial class PlayerController : Node, IController public override void _Ready() { - _playerModel = Context.GetModel<PlayerModel>(); + _playerModel = this.GetModel<PlayerModel>(); // 监听数据变化,更新视图 _playerModel.Health.Register(UpdateHealthUI); @@ -107,7 +107,7 @@ public partial class PlayerController : Node, IController if (keyEvent.Keycode == Key.Space) { // 发送命令修改 Model - Context.SendCommand(new AttackCommand()); + this.SendCommand(new AttackCommand()); } } } @@ -238,7 +238,7 @@ public partial class PlayerView : Control, IController public override void _Ready() { - _viewModel = Context.GetModel<PlayerViewModel>(); + _viewModel = this.GetModel<PlayerViewModel>(); _healthLabel = GetNode<Label>("HealthLabel"); _healthBar = GetNode<ProgressBar>("HealthBar"); @@ -331,7 +331,7 @@ public partial class ShopController : IController Quantity = quantity }; - Context.SendCommand(new BuyItemCommand { Input = input }); + this.SendCommand(new BuyItemCommand { Input = input }); } } ``` @@ -462,7 +462,7 @@ public partial class CharacterPanelController : IController { var input = new GetPlayerStatsInput { PlayerId = "player1" }; var query = new GetPlayerStatsQuery { Input = input }; - var stats = Context.SendQuery(query); + var stats = this.SendQuery(query); // 显示统计信息 DisplayStats(stats); @@ -629,11 +629,11 @@ public partial class UIController : IController public void Initialize() { // 监听成就解锁事件 - Context.RegisterEvent(OnAchievementUnlocked) + this.RegisterEvent(OnAchievementUnlocked) .AddToUnregisterList(_unregisterList); // 监听玩家死亡事件 - Context.RegisterEvent(OnPlayerDied) + this.RegisterEvent(OnPlayerDied) .AddToUnregisterList(_unregisterList); } @@ -883,11 +883,11 @@ public partial class MenuController : IController public void OnStartButtonClicked() { // 通过架构获取服务 - var gameModel = Context.GetModel(); + var gameModel = this.GetModel(); gameModel.GameState.Value = GameState.Playing; // 发送命令 - Context.SendCommand(new StartGameCommand()); + this.SendCommand(new StartGameCommand()); } } ``` @@ -1263,19 +1263,19 @@ public partial class GameController : IController { public async Task StartGame() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); await stateMachine.ChangeToAsync(); } public async Task PauseGame() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); await stateMachine.ChangeToAsync(); } public async Task ResumeGame() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); await stateMachine.ChangeToAsync(); } } @@ -1708,7 +1708,7 @@ namespace Game.Controllers public override void _Ready() { - _playerModel = Context.GetModel(); + _playerModel = this.GetModel(); // 监听用户输入 SetProcessInput(true); @@ -1723,7 +1723,7 @@ namespace Game.Controllers if (@event is InputEventKey keyEvent && keyEvent.Pressed) { var direction = GetInputDirection(keyEvent); - Context.SendEvent(new PlayerInputEvent { Direction = direction }); + this.SendEvent(new PlayerInputEvent { Direction = direction }); } } diff --git a/docs/zh-CN/best-practices/index.md b/docs/zh-CN/best-practices/index.md index 6ed4b9e..6eb1b22 100644 --- a/docs/zh-CN/best-practices/index.md +++ b/docs/zh-CN/best-practices/index.md @@ -41,7 +41,7 @@ public partial class PlayerController : IController // Controller:连接 UI 和逻辑 public void Initialize() { - var player = Context.GetModel(); + var player = this.GetModel(); player.Health.RegisterWithInitValue(OnHealthChanged); } @@ -191,10 +191,10 @@ public partial class MyController : IController public void Initialize() { - var model = Context.GetModel(); + var model = this.GetModel(); // 注册事件并添加到注销列表 - Context.RegisterEvent(OnPlayerDied) + this.RegisterEvent(OnPlayerDied) .AddToUnregisterList(_unregisterList); // 注册属性监听并添加到注销列表 @@ -243,7 +243,7 @@ public class GameManager // ❌ 低效:每次都查询 public void Update() { - var model = Context.GetModel(); + var model = this.GetModel(); model.Health.Value -= 1; } @@ -252,7 +252,7 @@ private PlayerModel _playerModel; public void Initialize() { - _playerModel = Context.GetModel(); + _playerModel = this.GetModel(); } public void Update() @@ -267,7 +267,7 @@ public void Update() // ❌ 低效:每帧创建新事件 public void Update() { - Context.SendEvent(new UpdateEvent()); // 频繁分配内存 + this.SendEvent(new UpdateEvent()); // 频繁分配内存 } // ✅ 高效:复用事件或使用对象池 @@ -275,7 +275,7 @@ private UpdateEvent _updateEvent = new UpdateEvent(); public void Update() { - Context.SendEvent(_updateEvent); + this.SendEvent(_updateEvent); } ``` @@ -445,7 +445,7 @@ public class CombatSystem : AbstractSystem // ❌ 错误:可能导致内存泄漏 public void Initialize() { - Context.RegisterEvent(OnEvent1); // 未注销 + this.RegisterEvent(OnEvent1); // 未注销 } // ✅ 正确 @@ -453,7 +453,7 @@ private IUnRegisterList _unregisterList = new UnRegisterList(); public void Initialize() { - Context.RegisterEvent(OnEvent1) + this.RegisterEvent(OnEvent1) .AddToUnregisterList(_unregisterList); } diff --git a/docs/zh-CN/best-practices/performance.md b/docs/zh-CN/best-practices/performance.md index 5923079..375e23c 100644 --- a/docs/zh-CN/best-practices/performance.md +++ b/docs/zh-CN/best-practices/performance.md @@ -289,10 +289,10 @@ public partial class PlayerController : Node, IController public void Initialize() { - _playerModel = Context.GetModel<PlayerModel>(); + _playerModel = this.GetModel<PlayerModel>(); // 使用 UnRegisterList 管理订阅 - Context.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged) + this.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged) .AddTo(_unRegisterList); _playerModel.Health.Register(OnHealthChanged) @@ -316,9 +316,9 @@ public partial class PlayerController : Node, IController public void Initialize() { // 订阅事件但从不取消订阅 - Context.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged); + this.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged); - var playerModel = Context.GetModel<PlayerModel>(); + var playerModel = this.GetModel<PlayerModel>(); playerModel.Health.Register(OnHealthChanged); // 当对象被销毁时,这些订阅仍然存在,导致内存泄漏 diff --git a/docs/zh-CN/core/architecture.md b/docs/zh-CN/core/architecture.md index 603f744..ea4e6e4 100644 --- a/docs/zh-CN/core/architecture.md +++ b/docs/zh-CN/core/architecture.md @@ -184,17 +184,17 @@ public partial class GameController : IController { public void Start() { - // 获取 Model(通过 Context 属性访问架构) - var playerModel = Context.GetModel(); + // 获取 Model(使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)) + var playerModel = this.GetModel(); // 发送命令 - Context.SendCommand(new StartGameCommand()); + this.SendCommand(new StartGameCommand()); // 发送查询 - var score = Context.SendQuery(new GetScoreQuery()); + var score = this.SendQuery(new GetScoreQuery()); // 注册事件 - Context.RegisterEvent(OnPlayerDied); + this.RegisterEvent(OnPlayerDied); } private void OnPlayerDied(PlayerDiedEvent e) diff --git a/docs/zh-CN/core/command.md b/docs/zh-CN/core/command.md index 2caed75..cd0d132 100644 --- a/docs/zh-CN/core/command.md +++ b/docs/zh-CN/core/command.md @@ -64,7 +64,7 @@ public partial class GameController : IController { public void OnRestoreHealthButtonClicked() { - Context.SendCommand(new SimpleCommand()); + this.SendCommand(new SimpleCommand()); } } ``` @@ -205,7 +205,7 @@ public partial class GameController : IController public void OnStartButtonClicked() { var input = new StartGameInput { LevelId = 1, PlayerName = "Player1" }; - Context.SendCommand(new StartGameCommand { Input = input }); + this.SendCommand(new StartGameCommand { Input = input }); } } ``` diff --git a/docs/zh-CN/core/configuration.md b/docs/zh-CN/core/configuration.md index 894c26b..5f26fb8 100644 --- a/docs/zh-CN/core/configuration.md +++ b/docs/zh-CN/core/configuration.md @@ -629,7 +629,7 @@ public partial class SettingsController : IController { public void ApplyGraphicsSettings(int quality, bool fullscreen) { - var config = Context.GetUtility(); + var config = this.GetUtility(); // 更新配置(会自动触发监听器) config.SetConfig("graphics.quality", quality); @@ -641,7 +641,7 @@ public partial class SettingsController : IController public void ResetToDefaults() { - var config = Context.GetUtility(); + var config = this.GetUtility(); // 清空所有配置 config.Clear(); diff --git a/docs/zh-CN/core/ecs.md b/docs/zh-CN/core/ecs.md index 7e71ef5..527fa6a 100644 --- a/docs/zh-CN/core/ecs.md +++ b/docs/zh-CN/core/ecs.md @@ -243,7 +243,7 @@ public partial class GameController : IController public void Start() { // 获取 World - _world = Context.GetService(); + _world = this.GetService(); // 创建玩家实体 var player = _world.Create( diff --git a/docs/zh-CN/core/events.md b/docs/zh-CN/core/events.md index 527054a..82b43e7 100644 --- a/docs/zh-CN/core/events.md +++ b/docs/zh-CN/core/events.md @@ -364,13 +364,13 @@ public partial class GameController : IController public void Initialize() { // 注册多个事件 - Context.RegisterEvent(OnGameStarted) + this.RegisterEvent(OnGameStarted) .AddToUnregisterList(_unregisterList); - Context.RegisterEvent(OnPlayerDied) + this.RegisterEvent(OnPlayerDied) .AddToUnregisterList(_unregisterList); - Context.RegisterEvent(OnLevelCompleted) + this.RegisterEvent(OnLevelCompleted) .AddToUnregisterList(_unregisterList); } @@ -419,7 +419,7 @@ onAnyDamage.Register(() => ```csharp // 只处理高伤害事件 -Context.RegisterEvent(e => +this.RegisterEvent(e => { if (e.Damage >= 50) { @@ -461,7 +461,7 @@ public partial class TutorialController : IController { // 只监听一次 IUnRegister unregister = null; - unregister = Context.RegisterEvent(e => + unregister = this.RegisterEvent(e => { ShowTutorialComplete(); unregister?.UnRegister(); // 立即注销 @@ -513,10 +513,10 @@ public partial class MyController : IController public void Initialize() { // 所有注册都添加到列表 - Context.RegisterEvent(OnEvent1) + this.RegisterEvent(OnEvent1) .AddToUnregisterList(_unregisterList); - Context.RegisterEvent(OnEvent2) + this.RegisterEvent(OnEvent2) .AddToUnregisterList(_unregisterList); } diff --git a/docs/zh-CN/core/pause.md b/docs/zh-CN/core/pause.md index c37de6b..9e43304 100644 --- a/docs/zh-CN/core/pause.md +++ b/docs/zh-CN/core/pause.md @@ -119,7 +119,7 @@ public partial class GameController : IController public void Initialize() { // 从架构中获取暂停管理器 - _pauseManager = Context.GetUtility(); + _pauseManager = this.GetUtility(); } } ``` @@ -137,7 +137,7 @@ public partial class PauseMenuController : IController public void Initialize() { - _pauseManager = Context.GetUtility(); + _pauseManager = this.GetUtility(); } public void OpenPauseMenu() @@ -371,7 +371,7 @@ public partial class PauseIndicator : IController public void Initialize() { - _pauseManager = Context.GetUtility(); + _pauseManager = this.GetUtility(); // 订阅状态变化事件 _pauseManager.OnPauseStateChanged += OnPauseStateChanged; @@ -692,7 +692,7 @@ public partial class ProperCleanup : IController public void Initialize() { - _pauseManager = Context.GetUtility(); + _pauseManager = this.GetUtility(); _customHandler = new CustomPauseHandler(); _pauseManager.RegisterHandler(_customHandler); diff --git a/docs/zh-CN/core/property.md b/docs/zh-CN/core/property.md index a88cbb8..8c3cd97 100644 --- a/docs/zh-CN/core/property.md +++ b/docs/zh-CN/core/property.md @@ -212,7 +212,7 @@ public partial class PlayerUI : Control, IController public override void _Ready() { - var playerModel = Context.GetModel(); + var playerModel = this.GetModel(); // 绑定生命值到UI(立即显示当前值) playerModel.Health @@ -278,7 +278,7 @@ public partial class VolumeSlider : HSlider, IController public override void _Ready() { - _volumeProperty = Context.GetModel().MasterVolume; + _volumeProperty = this.GetModel().MasterVolume; // Model -> UI _volumeProperty.RegisterWithInitValue(vol => Value = vol) @@ -350,7 +350,7 @@ public partial class CombatController : Node, IController { public override void _Ready() { - var playerModel = Context.GetModel(); + var playerModel = this.GetModel(); // 只在生命值低于30%时显示警告 playerModel.Health.Register(hp => diff --git a/docs/zh-CN/core/query.md b/docs/zh-CN/core/query.md index cc57a80..43b62a6 100644 --- a/docs/zh-CN/core/query.md +++ b/docs/zh-CN/core/query.md @@ -162,12 +162,12 @@ public partial class ShopUI : IController { // 查询玩家金币 var query = new GetPlayerGoldQuery { Input = new GetPlayerGoldInput() }; - int playerGold = Context.SendQuery(query); + int playerGold = this.SendQuery(query); if (playerGold >= _itemPrice) { // 发送购买命令 - Context.SendCommand(new BuyItemCommand { Input = new BuyItemInput { ItemId = "sword_01" } }); + this.SendCommand(new BuyItemCommand { Input = new BuyItemInput { ItemId = "sword_01" } }); } else { diff --git a/docs/zh-CN/core/state-machine.md b/docs/zh-CN/core/state-machine.md index 5baf461..a757199 100644 --- a/docs/zh-CN/core/state-machine.md +++ b/docs/zh-CN/core/state-machine.md @@ -136,7 +136,7 @@ public partial class GameController : IController { public async Task StartGame() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); // 切换到游戏状态 var success = await stateMachine.ChangeToAsync(); @@ -228,7 +228,7 @@ public partial class GameController : IController { public async Task NavigateBack() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); // 回退到上一个状态 var success = await stateMachine.GoBackAsync(); @@ -241,7 +241,7 @@ public partial class GameController : IController public void ShowHistory() { - var stateMachine = Context.GetSystem(); + var stateMachine = this.GetSystem(); // 获取状态历史 var history = stateMachine.GetStateHistory(); diff --git a/docs/zh-CN/core/system.md b/docs/zh-CN/core/system.md index 491dedf..eb93282 100644 --- a/docs/zh-CN/core/system.md +++ b/docs/zh-CN/core/system.md @@ -194,8 +194,8 @@ public partial class GameController : IController public void Start() { // 获取 System - var combatSystem = Context.GetSystem(); - var questSystem = Context.GetSystem(); + var combatSystem = this.GetSystem(); + var questSystem = this.GetSystem(); // 使用 System combatSystem.StartBattle(); diff --git a/docs/zh-CN/game/data.md b/docs/zh-CN/game/data.md index 983889a..cd82606 100644 --- a/docs/zh-CN/game/data.md +++ b/docs/zh-CN/game/data.md @@ -110,7 +110,7 @@ public partial class SaveController : IController { public async Task SaveGame(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); // 创建存档数据 var saveData = new SaveData @@ -131,7 +131,7 @@ public partial class SaveController : IController public async Task LoadGame(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); // 检查存档是否存在 if (!await saveRepo.ExistsAsync(slot)) @@ -147,7 +147,7 @@ public partial class SaveController : IController public async Task DeleteSave(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); // 删除存档 await saveRepo.DeleteAsync(slot); @@ -190,7 +190,7 @@ public class GameArchitecture : Architecture ```csharp public async Task ShowSaveList() { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); // 获取所有存档槽位 var slots = await saveRepo.ListSlotsAsync(); @@ -249,7 +249,7 @@ public partial class AutoSaveController : IController private async Task SaveGame(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); var saveData = CreateSaveData(); await saveRepo.SaveAsync(slot, saveData); } @@ -302,7 +302,7 @@ public class SaveDataMigrator // 加载时自动迁移 public async Task LoadWithMigration(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); var data = await saveRepo.LoadAsync(slot); if (data.Version < 2) @@ -332,7 +332,7 @@ public partial class SettingsController : IController { public async Task SaveSettings() { - var dataRepo = Context.GetUtility(); + var dataRepo = this.GetUtility(); var settings = new GameSettings { @@ -350,7 +350,7 @@ public partial class SettingsController : IController public async Task LoadSettings() { - var dataRepo = Context.GetUtility(); + var dataRepo = this.GetUtility(); var location = new DataLocation("settings", "game_settings.json"); // 检查是否存在 @@ -370,7 +370,7 @@ public partial class SettingsController : IController ```csharp public async Task SaveAllGameData() { - var dataRepo = Context.GetUtility(); + var dataRepo = this.GetUtility(); var dataList = new List<(IDataLocation, IData)> { @@ -390,7 +390,7 @@ public async Task SaveAllGameData() ```csharp public async Task BackupSave(int slot) { - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); if (!await saveRepo.ExistsAsync(slot)) { @@ -411,7 +411,7 @@ public async Task BackupSave(int slot) public async Task RestoreBackup(int slot) { int backupSlot = slot + 100; - var saveRepo = Context.GetUtility>(); + var saveRepo = this.GetUtility>(); if (!await saveRepo.ExistsAsync(backupSlot)) { diff --git a/docs/zh-CN/game/index.md b/docs/zh-CN/game/index.md index dca6922..30ee558 100644 --- a/docs/zh-CN/game/index.md +++ b/docs/zh-CN/game/index.md @@ -937,7 +937,7 @@ public partial class GameManager : Node, IController // 加载初始场景 LoadInitialScene(); - Context.SendEvent(new NewGameStartedEvent { PlayerName = playerName }); + this.SendEvent(new NewGameStartedEvent { PlayerName = playerName }); } public void LoadGame(int slotId) @@ -952,19 +952,19 @@ public partial class GameManager : Node, IController // 恢复游戏状态 RestoreGameState(saveData); - Context.SendEvent(new GameLoadedEvent { SlotId = slotId }); + this.SendEvent(new GameLoadedEvent { SlotId = slotId }); Logger.Info("Game loaded successfully"); } else { Logger.Warning($"No save data found in slot {slotId}"); - Context.SendEvent(new GameLoadFailedEvent { SlotId = slotId }); + this.SendEvent(new GameLoadFailedEvent { SlotId = slotId }); } } catch (Exception ex) { Logger.Error($"Failed to load game: {ex.Message}"); - Context.SendEvent(new GameLoadFailedEvent { SlotId = slotId, Error = ex.Message }); + this.SendEvent(new GameLoadFailedEvent { SlotId = slotId, Error = ex.Message }); } } @@ -977,13 +977,13 @@ public partial class GameManager : Node, IController var saveData = CreateSaveData(); _dataManager.SaveGame(slotId, saveData); - Context.SendEvent(new GameSavedEvent { SlotId = slotId }); + this.SendEvent(new GameSavedEvent { SlotId = slotId }); Logger.Info("Game saved successfully"); } catch (Exception ex) { Logger.Error($"Failed to save game: {ex.Message}"); - Context.SendEvent(new GameSaveFailedEvent { SlotId = slotId, Error = ex.Message }); + this.SendEvent(new GameSaveFailedEvent { SlotId = slotId, Error = ex.Message }); } } @@ -1014,9 +1014,9 @@ public partial class GameManager : Node, IController gameWorld.AddChild(player); // 恢复其他游戏状态 - Context.GetModel().Health.Value = saveData.PlayerHealth; - Context.GetModel().CurrentLevel.Value = saveData.CurrentLevel; - Context.GetModel().LoadFromData(saveData.Inventory); + this.GetModel().Health.Value = saveData.PlayerHealth; + this.GetModel().CurrentLevel.Value = saveData.CurrentLevel; + this.GetModel().LoadFromData(saveData.Inventory); } private SaveData CreateSaveData() @@ -1026,9 +1026,9 @@ public partial class GameManager : Node, IController return new SaveData { PlayerPosition = player?.Position ?? Vector2.Zero, - PlayerHealth = Context.GetModel().Health.Value, - CurrentLevel = Context.GetModel().CurrentLevel.Value, - Inventory = Context.GetModel().GetData(), + PlayerHealth = this.GetModel().Health.Value, + CurrentLevel = this.GetModel().CurrentLevel.Value, + Inventory = this.GetModel().GetData(), Timestamp = DateTime.UtcNow, Version = 1 }; @@ -1094,7 +1094,7 @@ public class AutoSaveSystem : AbstractSystem var saveData = CreateAutoSaveData(); // 保存到自动存档槽 - var storage = Context.GetUtility(); + var storage = this.GetUtility(); storage.Write("autosave", saveData); storage.Write("autosave/timestamp", DateTime.UtcNow); @@ -1330,7 +1330,7 @@ public class PlayerModule : AbstractModule private void OnPlayerDeath(PlayerDeathEvent e) { // 触发保存模块的事件 - Context.SendEvent(new RequestAutoSaveEvent { Reason = "Player Death" }); + this.SendEvent(new RequestAutoSaveEvent { Reason = "Player Death" }); } } ``` diff --git a/docs/zh-CN/game/scene.md b/docs/zh-CN/game/scene.md index 68f8df0..e9062ba 100644 --- a/docs/zh-CN/game/scene.md +++ b/docs/zh-CN/game/scene.md @@ -172,7 +172,7 @@ public partial class GameController : IController { public async Task StartGame() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); // 替换当前场景(清空场景栈) await sceneRouter.ReplaceAsync("Gameplay"); @@ -180,7 +180,7 @@ public partial class GameController : IController public async Task ShowPauseMenu() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); // 压入新场景(保留当前场景) await sceneRouter.PushAsync("Pause"); @@ -188,7 +188,7 @@ public partial class GameController : IController public async Task ClosePauseMenu() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); // 弹出当前场景(恢复上一个场景) await sceneRouter.PopAsync(); @@ -360,7 +360,7 @@ public partial class SceneNavigationController : IController { public async Task NavigateToSettings() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); // 检查场景是否已在栈中 if (sceneRouter.Contains("Settings")) @@ -375,7 +375,7 @@ public partial class SceneNavigationController : IController public void ShowSceneStack() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); Console.WriteLine("当前场景栈:"); foreach (var scene in sceneRouter.Stack) @@ -386,7 +386,7 @@ public partial class SceneNavigationController : IController public async Task ReturnToMainMenu() { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); // 清空所有场景并加载主菜单 await sceneRouter.ClearAsync(); @@ -444,7 +444,7 @@ public partial class PreloadController : IController { public async Task PreloadNextLevel() { - var sceneFactory = Context.GetUtility(); + var sceneFactory = this.GetUtility(); // 预加载下一关场景 var scene = sceneFactory.Create("Level2"); @@ -562,7 +562,7 @@ await sceneRouter.ReplaceAsync("Gameplay", new GameplayEnterParam 2. **通过 Model**: ```csharp -var gameModel = Context.GetModel(); +var gameModel = this.GetModel(); gameModel.CurrentLevel = 5; await sceneRouter.ReplaceAsync("Gameplay"); ``` @@ -570,7 +570,7 @@ await sceneRouter.ReplaceAsync("Gameplay"); 3. **通过事件**: ```csharp -Context.SendEvent(new LevelSelectedEvent { Level = 5 }); +this.SendEvent(new LevelSelectedEvent { Level = 5 }); await sceneRouter.ReplaceAsync("Gameplay"); ``` @@ -604,7 +604,7 @@ public class LoadingScreenHandler : ISceneTransitionHandler ```csharp public async Task ChangeScene(string sceneKey) { - var sceneRouter = Context.GetSystem(); + var sceneRouter = this.GetSystem(); if (sceneRouter.IsTransitioning) { @@ -641,7 +641,7 @@ catch (Exception ex) ```csharp // 在当前场景中预加载下一个场景 -var factory = Context.GetUtility(); +var factory = this.GetUtility(); var nextScene = factory.Create("NextLevel"); await nextScene.OnLoadAsync(null); diff --git a/docs/zh-CN/game/serialization.md b/docs/zh-CN/game/serialization.md index 0ee97c3..4cb0537 100644 --- a/docs/zh-CN/game/serialization.md +++ b/docs/zh-CN/game/serialization.md @@ -108,7 +108,7 @@ public partial class SaveController : IController { public void SavePlayer() { - var serializer = Context.GetUtility(); + var serializer = this.GetUtility(); var player = new PlayerData { @@ -132,7 +132,7 @@ public partial class SaveController : IController ```csharp public void LoadPlayer() { - var serializer = Context.GetUtility(); + var serializer = this.GetUtility(); string json = "{\"Name\":\"Player1\",\"Level\":10,\"Experience\":1000}"; @@ -150,7 +150,7 @@ public void LoadPlayer() ```csharp public void SerializeRuntimeType() { - var serializer = Context.GetUtility(); + var serializer = this.GetUtility(); object data = new PlayerData { Name = "Player1", Level = 10 }; Type dataType = data.GetType(); @@ -182,8 +182,8 @@ public partial class DataManager : IController { public async Task SaveData() { - var serializer = Context.GetUtility(); - var storage = Context.GetUtility(); + var serializer = this.GetUtility(); + var storage = this.GetUtility(); var gameData = new GameData { @@ -200,8 +200,8 @@ public partial class DataManager : IController public async Task LoadData() { - var serializer = Context.GetUtility(); - var storage = Context.GetUtility(); + var serializer = this.GetUtility(); + var storage = this.GetUtility(); // 从存储读取 string json = await storage.ReadAsync("game_data"); diff --git a/docs/zh-CN/game/ui.md b/docs/zh-CN/game/ui.md index 54461b4..6597eef 100644 --- a/docs/zh-CN/game/ui.md +++ b/docs/zh-CN/game/ui.md @@ -134,7 +134,7 @@ public partial class UiController : IController { public async Task ShowSettings() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 压入设置页面(保留当前页面) await uiRouter.PushAsync("Settings"); @@ -142,7 +142,7 @@ public partial class UiController : IController public async Task CloseSettings() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 弹出当前页面(返回上一页) await uiRouter.PopAsync(); @@ -150,7 +150,7 @@ public partial class UiController : IController public async Task ShowMainMenu() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 替换所有页面(清空 UI 栈) await uiRouter.ReplaceAsync("MainMenu"); @@ -166,7 +166,7 @@ public partial class UiController : IController { public void ShowDialog() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 在 Modal 层显示对话框 var handle = uiRouter.Show("ConfirmDialog", UiLayer.Modal); @@ -174,7 +174,7 @@ public partial class UiController : IController public void ShowToast(string message) { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 在 Toast 层显示提示 var handle = uiRouter.Show("ToastMessage", UiLayer.Toast, @@ -183,7 +183,7 @@ public partial class UiController : IController public void ShowLoading() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 在 Topmost 层显示加载界面 var handle = uiRouter.Show("LoadingScreen", UiLayer.Topmost); @@ -314,7 +314,7 @@ public partial class DialogController : IController public void ShowDialog() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 显示对话框并保存句柄 _dialogHandle = uiRouter.Show("ConfirmDialog", UiLayer.Modal); @@ -324,7 +324,7 @@ public partial class DialogController : IController { if (_dialogHandle.HasValue) { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 使用句柄关闭对话框 uiRouter.Hide(_dialogHandle.Value, UiLayer.Modal, destroy: true); @@ -345,7 +345,7 @@ public partial class NavigationController : IController { public void ShowUiStack() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); Console.WriteLine($"UI 栈深度: {uiRouter.Count}"); @@ -358,13 +358,13 @@ public partial class NavigationController : IController public bool IsSettingsOpen() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); return uiRouter.Contains("Settings"); } public bool IsTopPage(string uiKey) { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); return uiRouter.IsTop(uiKey); } } @@ -381,7 +381,7 @@ public partial class LayerController : IController { public void ShowMultipleToasts() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // Toast 层支持重入,可以同时显示多个 uiRouter.Show("Toast1", UiLayer.Toast); @@ -391,7 +391,7 @@ public partial class LayerController : IController public void ClearAllToasts() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 清空 Toast 层的所有 UI uiRouter.ClearLayer(UiLayer.Toast, destroy: true); @@ -399,7 +399,7 @@ public partial class LayerController : IController public void HideAllDialogs() { - var uiRouter = Context.GetSystem(); + var uiRouter = this.GetSystem(); // 隐藏 Modal 层的所有对话框 uiRouter.HideByKey("ConfirmDialog", UiLayer.Modal, hideAll: true); diff --git a/docs/zh-CN/getting-started/quick-start.md b/docs/zh-CN/getting-started/quick-start.md index 5815e23..824b01b 100644 --- a/docs/zh-CN/getting-started/quick-start.md +++ b/docs/zh-CN/getting-started/quick-start.md @@ -162,8 +162,8 @@ public partial class GameController : IController public void Initialize() { - _playerModel = Context.GetModel(); - _gameStateModel = Context.GetModel(); + _playerModel = this.GetModel(); + _gameStateModel = this.GetModel(); // 初始化事件监听 InitializeEventListeners(); @@ -180,18 +180,18 @@ public partial class GameController : IController public void StartGame() { _gameStateModel.IsGameRunning.Value = true; - Context.SendEvent(new GameStartEvent()); + this.SendEvent(new GameStartEvent()); Console.WriteLine("Game started!"); } public void MovePlayer(Vector2 direction) { - Context.SendCommand(new MovePlayerCommand { Direction = direction }); + this.SendCommand(new MovePlayerCommand { Direction = direction }); } public void PlayerAttack(Vector2 target) { - Context.SendCommand(new AttackCommand { TargetPosition = target }); + this.SendCommand(new AttackCommand { TargetPosition = target }); } // UI 更新回调 diff --git a/docs/zh-CN/godot/architecture.md b/docs/zh-CN/godot/architecture.md index b65d4bc..28af1b0 100644 --- a/docs/zh-CN/godot/architecture.md +++ b/docs/zh-CN/godot/architecture.md @@ -348,24 +348,21 @@ public partial class Player : CharacterBody2D, IController { public override void _Ready() { - // 方式 1: 通过 Context 属性访问(由 [ContextAware] 生成) - var playerModel = Context.GetModel(); - var gameplaySystem = Context.GetSystem(); - - // 方式 2: 通过扩展方法访问(扩展方法基于 IContextAware) - var playerModel2 = this.GetModel(); + // 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口) + var playerModel = this.GetModel(); + var gameplaySystem = this.GetSystem(); // 发送事件 - Context.SendEvent(new PlayerSpawnedEvent()); + this.SendEvent(new PlayerSpawnedEvent()); // 执行命令 - Context.SendCommand(new InitPlayerCommand()); + this.SendCommand(new InitPlayerCommand()); } public override void _Process(double delta) { // 在 Process 中使用架构组件 - var inputSystem = Context.GetSystem(); + var inputSystem = this.GetSystem(); var movement = inputSystem.GetMovementInput(); Velocity = movement * 200; @@ -530,12 +527,9 @@ public partial class Player : Node, IController { public override void _Ready() { - // 通过 Context 属性访问架构(由 [ContextAware] 生成) - var model = Context.GetModel(); - var system = Context.GetSystem(); - - // 或使用扩展方法(扩展方法基于 IContextAware) - var model2 = this.GetModel(); + // 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口) + var model = this.GetModel(); + var system = this.GetSystem(); } } diff --git a/docs/zh-CN/godot/coroutine.md b/docs/zh-CN/godot/coroutine.md index b994f96..fd36b24 100644 --- a/docs/zh-CN/godot/coroutine.md +++ b/docs/zh-CN/godot/coroutine.md @@ -75,7 +75,7 @@ private System.Collections.IEnumerator WaitUntilCondition() GD.Print("等待生命值恢复"); // 等待生命值大于 50 - var playerModel = Context.GetModel(); + var playerModel = this.GetModel(); yield return new WaitUntil(() => playerModel.Health.Value > 50); GD.Print("生命值已恢复!"); @@ -140,7 +140,7 @@ private System.Collections.IEnumerator EndOfFrameExample() ```csharp private System.Collections.IEnumerator WaitUntilExample() { - var health = Context.GetModel().Health; + var health = this.GetModel().Health; // 持续等待直到条件满足 yield return new WaitUntil(() => health.Value > 0); @@ -156,7 +156,7 @@ private System.Collections.IEnumerator WaitUntilExample() ```csharp private System.Collections.IEnumerator WaitWhileExample() { - var gameState = Context.GetModel(); + var gameState = this.GetModel(); // 等待游戏不再暂停 yield return new WaitWhile(() => gameState.IsPaused.Value); @@ -174,7 +174,7 @@ private System.Collections.IEnumerator WaitWhileExample() ```csharp private System.Collections.IEnumerator CombinedWait() { - var health = Context.GetModel().Health; + var health = this.GetModel().Health; var button = GetNode