mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 13:33:28 +08:00
refactor(docs): 将 Context 属性访问替换为扩展方法访问
- 将 Context.GetModel<T>() 调用替换为 this.GetModel<T>() - 将 Context.GetSystem<T>() 调用替换为 this.GetSystem<T>() - 将 Context.GetUtility<T>() 调用替换为 this.GetUtility<T>() - 将 Context.SendCommand() 调用替换为 this.SendCommand() - 将 Context.SendQuery() 调用替换为 this.SendQuery() - 将 Context.SendEvent() 调用替换为 this.SendEvent() - 将 Context.RegisterEvent<T>() 调用替换为 this.RegisterEvent<T>()
This commit is contained in:
parent
1b9e81bbdb
commit
25f7779b4e
@ -9,7 +9,7 @@
|
|||||||
/// 它的作用是标识一个类是控制器,用于协调 Model、System 和 UI 之间的交互。
|
/// 它的作用是标识一个类是控制器,用于协调 Model、System 和 UI 之间的交互。
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <strong>架构访问</strong>:控制器通常需要访问架构上下文。使用 [ContextAware] 特性
|
/// 架构访问 :控制器通常需要访问架构上下文。使用 [ContextAware] 特性
|
||||||
/// 自动生成上下文访问能力:
|
/// 自动生成上下文访问能力:
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <code>
|
/// <code>
|
||||||
@ -20,19 +20,19 @@
|
|||||||
/// {
|
/// {
|
||||||
/// public void Initialize()
|
/// public void Initialize()
|
||||||
/// {
|
/// {
|
||||||
/// // Context 属性由 [ContextAware] 自动生成
|
/// // [ContextAware] 实现 IContextAware 接口,可使用扩展方法
|
||||||
/// var playerModel = Context.GetModel<PlayerModel>();
|
/// var playerModel = this.GetModel<PlayerModel>();
|
||||||
/// var gameSystem = Context.GetSystem<GameSystem>();
|
/// var gameSystem = this.GetSystem<GameSystem>();
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// </code>
|
/// </code>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <strong>注意</strong>:
|
/// 注意:
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <list type="bullet">
|
/// <list type="bullet">
|
||||||
/// <item>必须添加 partial 关键字</item>
|
/// <item>必须添加 partial 关键字</item>
|
||||||
/// <item>[ContextAware] 特性会自动实现 IContextAware 接口</item>
|
/// <item>[ContextAware] 特性会自动实现 IContextAware 接口</item>
|
||||||
/// <item>Context 属性提供架构上下文访问</item>
|
/// <item>可使用 this.GetModel()、this.GetSystem() 等扩展方法访问架构</item>
|
||||||
/// </list>
|
/// </list>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public interface IController;
|
public interface IController;
|
||||||
@ -92,7 +92,7 @@ public partial class PlayerController : Node, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 监听数据变化,更新视图
|
// 监听数据变化,更新视图
|
||||||
_playerModel.Health.Register(UpdateHealthUI);
|
_playerModel.Health.Register(UpdateHealthUI);
|
||||||
@ -107,7 +107,7 @@ public partial class PlayerController : Node, IController
|
|||||||
if (keyEvent.Keycode == Key.Space)
|
if (keyEvent.Keycode == Key.Space)
|
||||||
{
|
{
|
||||||
// 发送命令修改 Model
|
// 发送命令修改 Model
|
||||||
Context.SendCommand(new AttackCommand());
|
this.SendCommand(new AttackCommand());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,7 +238,7 @@ public partial class PlayerView : Control, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_viewModel = Context.GetModel<PlayerViewModel>();
|
_viewModel = this.GetModel<PlayerViewModel>();
|
||||||
|
|
||||||
_healthLabel = GetNode<Label>("HealthLabel");
|
_healthLabel = GetNode<Label>("HealthLabel");
|
||||||
_healthBar = GetNode<ProgressBar>("HealthBar");
|
_healthBar = GetNode<ProgressBar>("HealthBar");
|
||||||
@ -331,7 +331,7 @@ public partial class ShopController : IController
|
|||||||
Quantity = quantity
|
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 input = new GetPlayerStatsInput { PlayerId = "player1" };
|
||||||
var query = new GetPlayerStatsQuery { Input = input };
|
var query = new GetPlayerStatsQuery { Input = input };
|
||||||
var stats = Context.SendQuery(query);
|
var stats = this.SendQuery(query);
|
||||||
|
|
||||||
// 显示统计信息
|
// 显示统计信息
|
||||||
DisplayStats(stats);
|
DisplayStats(stats);
|
||||||
@ -629,11 +629,11 @@ public partial class UIController : IController
|
|||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// 监听成就解锁事件
|
// 监听成就解锁事件
|
||||||
Context.RegisterEvent<AchievementUnlockedEvent>(OnAchievementUnlocked)
|
this.RegisterEvent<AchievementUnlockedEvent>(OnAchievementUnlocked)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
|
|
||||||
// 监听玩家死亡事件
|
// 监听玩家死亡事件
|
||||||
Context.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
this.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -883,11 +883,11 @@ public partial class MenuController : IController
|
|||||||
public void OnStartButtonClicked()
|
public void OnStartButtonClicked()
|
||||||
{
|
{
|
||||||
// 通过架构获取服务
|
// 通过架构获取服务
|
||||||
var gameModel = Context.GetModel<GameModel>();
|
var gameModel = this.GetModel<GameModel>();
|
||||||
gameModel.GameState.Value = GameState.Playing;
|
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()
|
public async Task StartGame()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
await stateMachine.ChangeToAsync<GameplayState>();
|
await stateMachine.ChangeToAsync<GameplayState>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task PauseGame()
|
public async Task PauseGame()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
await stateMachine.ChangeToAsync<PauseState>();
|
await stateMachine.ChangeToAsync<PauseState>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ResumeGame()
|
public async Task ResumeGame()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
await stateMachine.ChangeToAsync<GameplayState>();
|
await stateMachine.ChangeToAsync<GameplayState>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1708,7 +1708,7 @@ namespace Game.Controllers
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 监听用户输入
|
// 监听用户输入
|
||||||
SetProcessInput(true);
|
SetProcessInput(true);
|
||||||
@ -1723,7 +1723,7 @@ namespace Game.Controllers
|
|||||||
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
|
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
|
||||||
{
|
{
|
||||||
var direction = GetInputDirection(keyEvent);
|
var direction = GetInputDirection(keyEvent);
|
||||||
Context.SendEvent(new PlayerInputEvent { Direction = direction });
|
this.SendEvent(new PlayerInputEvent { Direction = direction });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -41,7 +41,7 @@ public partial class PlayerController : IController
|
|||||||
// Controller:连接 UI 和逻辑
|
// Controller:连接 UI 和逻辑
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
var player = Context.GetModel<PlayerModel>();
|
var player = this.GetModel<PlayerModel>();
|
||||||
player.Health.RegisterWithInitValue(OnHealthChanged);
|
player.Health.RegisterWithInitValue(OnHealthChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,10 +191,10 @@ public partial class MyController : IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
var model = Context.GetModel<PlayerModel>();
|
var model = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 注册事件并添加到注销列表
|
// 注册事件并添加到注销列表
|
||||||
Context.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
this.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
|
|
||||||
// 注册属性监听并添加到注销列表
|
// 注册属性监听并添加到注销列表
|
||||||
@ -243,7 +243,7 @@ public class GameManager
|
|||||||
// ❌ 低效:每次都查询
|
// ❌ 低效:每次都查询
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
var model = Context.GetModel<PlayerModel>();
|
var model = this.GetModel<PlayerModel>();
|
||||||
model.Health.Value -= 1;
|
model.Health.Value -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ private PlayerModel _playerModel;
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
@ -267,7 +267,7 @@ public void Update()
|
|||||||
// ❌ 低效:每帧创建新事件
|
// ❌ 低效:每帧创建新事件
|
||||||
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()
|
public void Update()
|
||||||
{
|
{
|
||||||
Context.SendEvent(_updateEvent);
|
this.SendEvent(_updateEvent);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -445,7 +445,7 @@ public class CombatSystem : AbstractSystem
|
|||||||
// ❌ 错误:可能导致内存泄漏
|
// ❌ 错误:可能导致内存泄漏
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
Context.RegisterEvent<Event1>(OnEvent1); // 未注销
|
this.RegisterEvent<Event1>(OnEvent1); // 未注销
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ 正确
|
// ✅ 正确
|
||||||
@ -453,7 +453,7 @@ private IUnRegisterList _unregisterList = new UnRegisterList();
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
Context.RegisterEvent<Event1>(OnEvent1)
|
this.RegisterEvent<Event1>(OnEvent1)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -289,10 +289,10 @@ public partial class PlayerController : Node, IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 使用 UnRegisterList 管理订阅
|
// 使用 UnRegisterList 管理订阅
|
||||||
Context.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged)
|
this.RegisterEvent<PlayerDamagedEvent>(OnPlayerDamaged)
|
||||||
.AddTo(_unRegisterList);
|
.AddTo(_unRegisterList);
|
||||||
|
|
||||||
_playerModel.Health.Register(OnHealthChanged)
|
_playerModel.Health.Register(OnHealthChanged)
|
||||||
@ -316,9 +316,9 @@ public partial class PlayerController : Node, IController
|
|||||||
public void Initialize()
|
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);
|
playerModel.Health.Register(OnHealthChanged);
|
||||||
|
|
||||||
// 当对象被销毁时,这些订阅仍然存在,导致内存泄漏
|
// 当对象被销毁时,这些订阅仍然存在,导致内存泄漏
|
||||||
|
|||||||
@ -184,17 +184,17 @@ public partial class GameController : IController
|
|||||||
{
|
{
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
// 获取 Model(通过 Context 属性访问架构)
|
// 获取 Model(使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口))
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 发送命令
|
// 发送命令
|
||||||
Context.SendCommand(new StartGameCommand());
|
this.SendCommand(new StartGameCommand());
|
||||||
|
|
||||||
// 发送查询
|
// 发送查询
|
||||||
var score = Context.SendQuery(new GetScoreQuery());
|
var score = this.SendQuery(new GetScoreQuery());
|
||||||
|
|
||||||
// 注册事件
|
// 注册事件
|
||||||
Context.RegisterEvent<PlayerDiedEvent>(OnPlayerDied);
|
this.RegisterEvent<PlayerDiedEvent>(OnPlayerDied);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPlayerDied(PlayerDiedEvent e)
|
private void OnPlayerDied(PlayerDiedEvent e)
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public partial class GameController : IController
|
|||||||
{
|
{
|
||||||
public void OnRestoreHealthButtonClicked()
|
public void OnRestoreHealthButtonClicked()
|
||||||
{
|
{
|
||||||
Context.SendCommand(new SimpleCommand());
|
this.SendCommand(new SimpleCommand());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -205,7 +205,7 @@ public partial class GameController : IController
|
|||||||
public void OnStartButtonClicked()
|
public void OnStartButtonClicked()
|
||||||
{
|
{
|
||||||
var input = new StartGameInput { LevelId = 1, PlayerName = "Player1" };
|
var input = new StartGameInput { LevelId = 1, PlayerName = "Player1" };
|
||||||
Context.SendCommand(new StartGameCommand { Input = input });
|
this.SendCommand(new StartGameCommand { Input = input });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -629,7 +629,7 @@ public partial class SettingsController : IController
|
|||||||
{
|
{
|
||||||
public void ApplyGraphicsSettings(int quality, bool fullscreen)
|
public void ApplyGraphicsSettings(int quality, bool fullscreen)
|
||||||
{
|
{
|
||||||
var config = Context.GetUtility<IConfigurationManager>();
|
var config = this.GetUtility<IConfigurationManager>();
|
||||||
|
|
||||||
// 更新配置(会自动触发监听器)
|
// 更新配置(会自动触发监听器)
|
||||||
config.SetConfig("graphics.quality", quality);
|
config.SetConfig("graphics.quality", quality);
|
||||||
@ -641,7 +641,7 @@ public partial class SettingsController : IController
|
|||||||
|
|
||||||
public void ResetToDefaults()
|
public void ResetToDefaults()
|
||||||
{
|
{
|
||||||
var config = Context.GetUtility<IConfigurationManager>();
|
var config = this.GetUtility<IConfigurationManager>();
|
||||||
|
|
||||||
// 清空所有配置
|
// 清空所有配置
|
||||||
config.Clear();
|
config.Clear();
|
||||||
|
|||||||
@ -243,7 +243,7 @@ public partial class GameController : IController
|
|||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
// 获取 World
|
// 获取 World
|
||||||
_world = Context.GetService<World>();
|
_world = this.GetService<World>();
|
||||||
|
|
||||||
// 创建玩家实体
|
// 创建玩家实体
|
||||||
var player = _world.Create(
|
var player = _world.Create(
|
||||||
|
|||||||
@ -364,13 +364,13 @@ public partial class GameController : IController
|
|||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// 注册多个事件
|
// 注册多个事件
|
||||||
Context.RegisterEvent<GameStartedEvent>(OnGameStarted)
|
this.RegisterEvent<GameStartedEvent>(OnGameStarted)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
|
|
||||||
Context.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
this.RegisterEvent<PlayerDiedEvent>(OnPlayerDied)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
|
|
||||||
Context.RegisterEvent<LevelCompletedEvent>(OnLevelCompleted)
|
this.RegisterEvent<LevelCompletedEvent>(OnLevelCompleted)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,7 +419,7 @@ onAnyDamage.Register(() =>
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// 只处理高伤害事件
|
// 只处理高伤害事件
|
||||||
Context.RegisterEvent<DamageDealtEvent>(e =>
|
this.RegisterEvent<DamageDealtEvent>(e =>
|
||||||
{
|
{
|
||||||
if (e.Damage >= 50)
|
if (e.Damage >= 50)
|
||||||
{
|
{
|
||||||
@ -461,7 +461,7 @@ public partial class TutorialController : IController
|
|||||||
{
|
{
|
||||||
// 只监听一次
|
// 只监听一次
|
||||||
IUnRegister unregister = null;
|
IUnRegister unregister = null;
|
||||||
unregister = Context.RegisterEvent<FirstEnemyKilledEvent>(e =>
|
unregister = this.RegisterEvent<FirstEnemyKilledEvent>(e =>
|
||||||
{
|
{
|
||||||
ShowTutorialComplete();
|
ShowTutorialComplete();
|
||||||
unregister?.UnRegister(); // 立即注销
|
unregister?.UnRegister(); // 立即注销
|
||||||
@ -513,10 +513,10 @@ public partial class MyController : IController
|
|||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// 所有注册都添加到列表
|
// 所有注册都添加到列表
|
||||||
Context.RegisterEvent<Event1>(OnEvent1)
|
this.RegisterEvent<Event1>(OnEvent1)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
|
|
||||||
Context.RegisterEvent<Event2>(OnEvent2)
|
this.RegisterEvent<Event2>(OnEvent2)
|
||||||
.AddToUnregisterList(_unregisterList);
|
.AddToUnregisterList(_unregisterList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -119,7 +119,7 @@ public partial class GameController : IController
|
|||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// 从架构中获取暂停管理器
|
// 从架构中获取暂停管理器
|
||||||
_pauseManager = Context.GetUtility<IPauseStackManager>();
|
_pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -137,7 +137,7 @@ public partial class PauseMenuController : IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_pauseManager = Context.GetUtility<IPauseStackManager>();
|
_pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OpenPauseMenu()
|
public void OpenPauseMenu()
|
||||||
@ -371,7 +371,7 @@ public partial class PauseIndicator : IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_pauseManager = Context.GetUtility<IPauseStackManager>();
|
_pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
// 订阅状态变化事件
|
// 订阅状态变化事件
|
||||||
_pauseManager.OnPauseStateChanged += OnPauseStateChanged;
|
_pauseManager.OnPauseStateChanged += OnPauseStateChanged;
|
||||||
@ -692,7 +692,7 @@ public partial class ProperCleanup : IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_pauseManager = Context.GetUtility<IPauseStackManager>();
|
_pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
_customHandler = new CustomPauseHandler();
|
_customHandler = new CustomPauseHandler();
|
||||||
|
|
||||||
_pauseManager.RegisterHandler(_customHandler);
|
_pauseManager.RegisterHandler(_customHandler);
|
||||||
|
|||||||
@ -212,7 +212,7 @@ public partial class PlayerUI : Control, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 绑定生命值到UI(立即显示当前值)
|
// 绑定生命值到UI(立即显示当前值)
|
||||||
playerModel.Health
|
playerModel.Health
|
||||||
@ -278,7 +278,7 @@ public partial class VolumeSlider : HSlider, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_volumeProperty = Context.GetModel<SettingsModel>().MasterVolume;
|
_volumeProperty = this.GetModel<SettingsModel>().MasterVolume;
|
||||||
|
|
||||||
// Model -> UI
|
// Model -> UI
|
||||||
_volumeProperty.RegisterWithInitValue(vol => Value = vol)
|
_volumeProperty.RegisterWithInitValue(vol => Value = vol)
|
||||||
@ -350,7 +350,7 @@ public partial class CombatController : Node, IController
|
|||||||
{
|
{
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 只在生命值低于30%时显示警告
|
// 只在生命值低于30%时显示警告
|
||||||
playerModel.Health.Register(hp =>
|
playerModel.Health.Register(hp =>
|
||||||
|
|||||||
@ -162,12 +162,12 @@ public partial class ShopUI : IController
|
|||||||
{
|
{
|
||||||
// 查询玩家金币
|
// 查询玩家金币
|
||||||
var query = new GetPlayerGoldQuery { Input = new GetPlayerGoldInput() };
|
var query = new GetPlayerGoldQuery { Input = new GetPlayerGoldInput() };
|
||||||
int playerGold = Context.SendQuery(query);
|
int playerGold = this.SendQuery(query);
|
||||||
|
|
||||||
if (playerGold >= _itemPrice)
|
if (playerGold >= _itemPrice)
|
||||||
{
|
{
|
||||||
// 发送购买命令
|
// 发送购买命令
|
||||||
Context.SendCommand(new BuyItemCommand { Input = new BuyItemInput { ItemId = "sword_01" } });
|
this.SendCommand(new BuyItemCommand { Input = new BuyItemInput { ItemId = "sword_01" } });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -136,7 +136,7 @@ public partial class GameController : IController
|
|||||||
{
|
{
|
||||||
public async Task StartGame()
|
public async Task StartGame()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
|
|
||||||
// 切换到游戏状态
|
// 切换到游戏状态
|
||||||
var success = await stateMachine.ChangeToAsync<GameplayState>();
|
var success = await stateMachine.ChangeToAsync<GameplayState>();
|
||||||
@ -228,7 +228,7 @@ public partial class GameController : IController
|
|||||||
{
|
{
|
||||||
public async Task NavigateBack()
|
public async Task NavigateBack()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
|
|
||||||
// 回退到上一个状态
|
// 回退到上一个状态
|
||||||
var success = await stateMachine.GoBackAsync();
|
var success = await stateMachine.GoBackAsync();
|
||||||
@ -241,7 +241,7 @@ public partial class GameController : IController
|
|||||||
|
|
||||||
public void ShowHistory()
|
public void ShowHistory()
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
|
|
||||||
// 获取状态历史
|
// 获取状态历史
|
||||||
var history = stateMachine.GetStateHistory();
|
var history = stateMachine.GetStateHistory();
|
||||||
|
|||||||
@ -194,8 +194,8 @@ public partial class GameController : IController
|
|||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
// 获取 System
|
// 获取 System
|
||||||
var combatSystem = Context.GetSystem<CombatSystem>();
|
var combatSystem = this.GetSystem<CombatSystem>();
|
||||||
var questSystem = Context.GetSystem<QuestSystem>();
|
var questSystem = this.GetSystem<QuestSystem>();
|
||||||
|
|
||||||
// 使用 System
|
// 使用 System
|
||||||
combatSystem.StartBattle();
|
combatSystem.StartBattle();
|
||||||
|
|||||||
@ -110,7 +110,7 @@ public partial class SaveController : IController
|
|||||||
{
|
{
|
||||||
public async Task SaveGame(int slot)
|
public async Task SaveGame(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
// 创建存档数据
|
// 创建存档数据
|
||||||
var saveData = new SaveData
|
var saveData = new SaveData
|
||||||
@ -131,7 +131,7 @@ public partial class SaveController : IController
|
|||||||
|
|
||||||
public async Task LoadGame(int slot)
|
public async Task LoadGame(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
// 检查存档是否存在
|
// 检查存档是否存在
|
||||||
if (!await saveRepo.ExistsAsync(slot))
|
if (!await saveRepo.ExistsAsync(slot))
|
||||||
@ -147,7 +147,7 @@ public partial class SaveController : IController
|
|||||||
|
|
||||||
public async Task DeleteSave(int slot)
|
public async Task DeleteSave(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
// 删除存档
|
// 删除存档
|
||||||
await saveRepo.DeleteAsync(slot);
|
await saveRepo.DeleteAsync(slot);
|
||||||
@ -190,7 +190,7 @@ public class GameArchitecture : Architecture
|
|||||||
```csharp
|
```csharp
|
||||||
public async Task ShowSaveList()
|
public async Task ShowSaveList()
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
// 获取所有存档槽位
|
// 获取所有存档槽位
|
||||||
var slots = await saveRepo.ListSlotsAsync();
|
var slots = await saveRepo.ListSlotsAsync();
|
||||||
@ -249,7 +249,7 @@ public partial class AutoSaveController : IController
|
|||||||
|
|
||||||
private async Task SaveGame(int slot)
|
private async Task SaveGame(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
var saveData = CreateSaveData();
|
var saveData = CreateSaveData();
|
||||||
await saveRepo.SaveAsync(slot, saveData);
|
await saveRepo.SaveAsync(slot, saveData);
|
||||||
}
|
}
|
||||||
@ -302,7 +302,7 @@ public class SaveDataMigrator
|
|||||||
// 加载时自动迁移
|
// 加载时自动迁移
|
||||||
public async Task<SaveDataV2> LoadWithMigration(int slot)
|
public async Task<SaveDataV2> LoadWithMigration(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveDataV2>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveDataV2>>();
|
||||||
var data = await saveRepo.LoadAsync(slot);
|
var data = await saveRepo.LoadAsync(slot);
|
||||||
|
|
||||||
if (data.Version < 2)
|
if (data.Version < 2)
|
||||||
@ -332,7 +332,7 @@ public partial class SettingsController : IController
|
|||||||
{
|
{
|
||||||
public async Task SaveSettings()
|
public async Task SaveSettings()
|
||||||
{
|
{
|
||||||
var dataRepo = Context.GetUtility<IDataRepository>();
|
var dataRepo = this.GetUtility<IDataRepository>();
|
||||||
|
|
||||||
var settings = new GameSettings
|
var settings = new GameSettings
|
||||||
{
|
{
|
||||||
@ -350,7 +350,7 @@ public partial class SettingsController : IController
|
|||||||
|
|
||||||
public async Task<GameSettings> LoadSettings()
|
public async Task<GameSettings> LoadSettings()
|
||||||
{
|
{
|
||||||
var dataRepo = Context.GetUtility<IDataRepository>();
|
var dataRepo = this.GetUtility<IDataRepository>();
|
||||||
var location = new DataLocation("settings", "game_settings.json");
|
var location = new DataLocation("settings", "game_settings.json");
|
||||||
|
|
||||||
// 检查是否存在
|
// 检查是否存在
|
||||||
@ -370,7 +370,7 @@ public partial class SettingsController : IController
|
|||||||
```csharp
|
```csharp
|
||||||
public async Task SaveAllGameData()
|
public async Task SaveAllGameData()
|
||||||
{
|
{
|
||||||
var dataRepo = Context.GetUtility<IDataRepository>();
|
var dataRepo = this.GetUtility<IDataRepository>();
|
||||||
|
|
||||||
var dataList = new List<(IDataLocation, IData)>
|
var dataList = new List<(IDataLocation, IData)>
|
||||||
{
|
{
|
||||||
@ -390,7 +390,7 @@ public async Task SaveAllGameData()
|
|||||||
```csharp
|
```csharp
|
||||||
public async Task BackupSave(int slot)
|
public async Task BackupSave(int slot)
|
||||||
{
|
{
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
if (!await saveRepo.ExistsAsync(slot))
|
if (!await saveRepo.ExistsAsync(slot))
|
||||||
{
|
{
|
||||||
@ -411,7 +411,7 @@ public async Task BackupSave(int slot)
|
|||||||
public async Task RestoreBackup(int slot)
|
public async Task RestoreBackup(int slot)
|
||||||
{
|
{
|
||||||
int backupSlot = slot + 100;
|
int backupSlot = slot + 100;
|
||||||
var saveRepo = Context.GetUtility<ISaveRepository<SaveData>>();
|
var saveRepo = this.GetUtility<ISaveRepository<SaveData>>();
|
||||||
|
|
||||||
if (!await saveRepo.ExistsAsync(backupSlot))
|
if (!await saveRepo.ExistsAsync(backupSlot))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -937,7 +937,7 @@ public partial class GameManager : Node, IController
|
|||||||
// 加载初始场景
|
// 加载初始场景
|
||||||
LoadInitialScene();
|
LoadInitialScene();
|
||||||
|
|
||||||
Context.SendEvent(new NewGameStartedEvent { PlayerName = playerName });
|
this.SendEvent(new NewGameStartedEvent { PlayerName = playerName });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadGame(int slotId)
|
public void LoadGame(int slotId)
|
||||||
@ -952,19 +952,19 @@ public partial class GameManager : Node, IController
|
|||||||
// 恢复游戏状态
|
// 恢复游戏状态
|
||||||
RestoreGameState(saveData);
|
RestoreGameState(saveData);
|
||||||
|
|
||||||
Context.SendEvent(new GameLoadedEvent { SlotId = slotId });
|
this.SendEvent(new GameLoadedEvent { SlotId = slotId });
|
||||||
Logger.Info("Game loaded successfully");
|
Logger.Info("Game loaded successfully");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Logger.Warning($"No save data found in slot {slotId}");
|
Logger.Warning($"No save data found in slot {slotId}");
|
||||||
Context.SendEvent(new GameLoadFailedEvent { SlotId = slotId });
|
this.SendEvent(new GameLoadFailedEvent { SlotId = slotId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Error($"Failed to load game: {ex.Message}");
|
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();
|
var saveData = CreateSaveData();
|
||||||
_dataManager.SaveGame(slotId, saveData);
|
_dataManager.SaveGame(slotId, saveData);
|
||||||
|
|
||||||
Context.SendEvent(new GameSavedEvent { SlotId = slotId });
|
this.SendEvent(new GameSavedEvent { SlotId = slotId });
|
||||||
Logger.Info("Game saved successfully");
|
Logger.Info("Game saved successfully");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Error($"Failed to save game: {ex.Message}");
|
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);
|
gameWorld.AddChild(player);
|
||||||
|
|
||||||
// 恢复其他游戏状态
|
// 恢复其他游戏状态
|
||||||
Context.GetModel<PlayerModel>().Health.Value = saveData.PlayerHealth;
|
this.GetModel<PlayerModel>().Health.Value = saveData.PlayerHealth;
|
||||||
Context.GetModel<GameModel>().CurrentLevel.Value = saveData.CurrentLevel;
|
this.GetModel<GameModel>().CurrentLevel.Value = saveData.CurrentLevel;
|
||||||
Context.GetModel<InventoryModel>().LoadFromData(saveData.Inventory);
|
this.GetModel<InventoryModel>().LoadFromData(saveData.Inventory);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SaveData CreateSaveData()
|
private SaveData CreateSaveData()
|
||||||
@ -1026,9 +1026,9 @@ public partial class GameManager : Node, IController
|
|||||||
return new SaveData
|
return new SaveData
|
||||||
{
|
{
|
||||||
PlayerPosition = player?.Position ?? Vector2.Zero,
|
PlayerPosition = player?.Position ?? Vector2.Zero,
|
||||||
PlayerHealth = Context.GetModel<PlayerModel>().Health.Value,
|
PlayerHealth = this.GetModel<PlayerModel>().Health.Value,
|
||||||
CurrentLevel = Context.GetModel<GameModel>().CurrentLevel.Value,
|
CurrentLevel = this.GetModel<GameModel>().CurrentLevel.Value,
|
||||||
Inventory = Context.GetModel<InventoryModel>().GetData(),
|
Inventory = this.GetModel<InventoryModel>().GetData(),
|
||||||
Timestamp = DateTime.UtcNow,
|
Timestamp = DateTime.UtcNow,
|
||||||
Version = 1
|
Version = 1
|
||||||
};
|
};
|
||||||
@ -1094,7 +1094,7 @@ public class AutoSaveSystem : AbstractSystem
|
|||||||
var saveData = CreateAutoSaveData();
|
var saveData = CreateAutoSaveData();
|
||||||
|
|
||||||
// 保存到自动存档槽
|
// 保存到自动存档槽
|
||||||
var storage = Context.GetUtility<IStorage>();
|
var storage = this.GetUtility<IStorage>();
|
||||||
storage.Write("autosave", saveData);
|
storage.Write("autosave", saveData);
|
||||||
storage.Write("autosave/timestamp", DateTime.UtcNow);
|
storage.Write("autosave/timestamp", DateTime.UtcNow);
|
||||||
|
|
||||||
@ -1330,7 +1330,7 @@ public class PlayerModule : AbstractModule
|
|||||||
private void OnPlayerDeath(PlayerDeathEvent e)
|
private void OnPlayerDeath(PlayerDeathEvent e)
|
||||||
{
|
{
|
||||||
// 触发保存模块的事件
|
// 触发保存模块的事件
|
||||||
Context.SendEvent(new RequestAutoSaveEvent { Reason = "Player Death" });
|
this.SendEvent(new RequestAutoSaveEvent { Reason = "Player Death" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -172,7 +172,7 @@ public partial class GameController : IController
|
|||||||
{
|
{
|
||||||
public async Task StartGame()
|
public async Task StartGame()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
// 替换当前场景(清空场景栈)
|
// 替换当前场景(清空场景栈)
|
||||||
await sceneRouter.ReplaceAsync("Gameplay");
|
await sceneRouter.ReplaceAsync("Gameplay");
|
||||||
@ -180,7 +180,7 @@ public partial class GameController : IController
|
|||||||
|
|
||||||
public async Task ShowPauseMenu()
|
public async Task ShowPauseMenu()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
// 压入新场景(保留当前场景)
|
// 压入新场景(保留当前场景)
|
||||||
await sceneRouter.PushAsync("Pause");
|
await sceneRouter.PushAsync("Pause");
|
||||||
@ -188,7 +188,7 @@ public partial class GameController : IController
|
|||||||
|
|
||||||
public async Task ClosePauseMenu()
|
public async Task ClosePauseMenu()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
// 弹出当前场景(恢复上一个场景)
|
// 弹出当前场景(恢复上一个场景)
|
||||||
await sceneRouter.PopAsync();
|
await sceneRouter.PopAsync();
|
||||||
@ -360,7 +360,7 @@ public partial class SceneNavigationController : IController
|
|||||||
{
|
{
|
||||||
public async Task NavigateToSettings()
|
public async Task NavigateToSettings()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
// 检查场景是否已在栈中
|
// 检查场景是否已在栈中
|
||||||
if (sceneRouter.Contains("Settings"))
|
if (sceneRouter.Contains("Settings"))
|
||||||
@ -375,7 +375,7 @@ public partial class SceneNavigationController : IController
|
|||||||
|
|
||||||
public void ShowSceneStack()
|
public void ShowSceneStack()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
Console.WriteLine("当前场景栈:");
|
Console.WriteLine("当前场景栈:");
|
||||||
foreach (var scene in sceneRouter.Stack)
|
foreach (var scene in sceneRouter.Stack)
|
||||||
@ -386,7 +386,7 @@ public partial class SceneNavigationController : IController
|
|||||||
|
|
||||||
public async Task ReturnToMainMenu()
|
public async Task ReturnToMainMenu()
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
// 清空所有场景并加载主菜单
|
// 清空所有场景并加载主菜单
|
||||||
await sceneRouter.ClearAsync();
|
await sceneRouter.ClearAsync();
|
||||||
@ -444,7 +444,7 @@ public partial class PreloadController : IController
|
|||||||
{
|
{
|
||||||
public async Task PreloadNextLevel()
|
public async Task PreloadNextLevel()
|
||||||
{
|
{
|
||||||
var sceneFactory = Context.GetUtility<ISceneFactory>();
|
var sceneFactory = this.GetUtility<ISceneFactory>();
|
||||||
|
|
||||||
// 预加载下一关场景
|
// 预加载下一关场景
|
||||||
var scene = sceneFactory.Create("Level2");
|
var scene = sceneFactory.Create("Level2");
|
||||||
@ -562,7 +562,7 @@ await sceneRouter.ReplaceAsync("Gameplay", new GameplayEnterParam
|
|||||||
2. **通过 Model**:
|
2. **通过 Model**:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var gameModel = Context.GetModel<GameModel>();
|
var gameModel = this.GetModel<GameModel>();
|
||||||
gameModel.CurrentLevel = 5;
|
gameModel.CurrentLevel = 5;
|
||||||
await sceneRouter.ReplaceAsync("Gameplay");
|
await sceneRouter.ReplaceAsync("Gameplay");
|
||||||
```
|
```
|
||||||
@ -570,7 +570,7 @@ await sceneRouter.ReplaceAsync("Gameplay");
|
|||||||
3. **通过事件**:
|
3. **通过事件**:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
Context.SendEvent(new LevelSelectedEvent { Level = 5 });
|
this.SendEvent(new LevelSelectedEvent { Level = 5 });
|
||||||
await sceneRouter.ReplaceAsync("Gameplay");
|
await sceneRouter.ReplaceAsync("Gameplay");
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -604,7 +604,7 @@ public class LoadingScreenHandler : ISceneTransitionHandler
|
|||||||
```csharp
|
```csharp
|
||||||
public async Task ChangeScene(string sceneKey)
|
public async Task ChangeScene(string sceneKey)
|
||||||
{
|
{
|
||||||
var sceneRouter = Context.GetSystem<ISceneRouter>();
|
var sceneRouter = this.GetSystem<ISceneRouter>();
|
||||||
|
|
||||||
if (sceneRouter.IsTransitioning)
|
if (sceneRouter.IsTransitioning)
|
||||||
{
|
{
|
||||||
@ -641,7 +641,7 @@ catch (Exception ex)
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// 在当前场景中预加载下一个场景
|
// 在当前场景中预加载下一个场景
|
||||||
var factory = Context.GetUtility<ISceneFactory>();
|
var factory = this.GetUtility<ISceneFactory>();
|
||||||
var nextScene = factory.Create("NextLevel");
|
var nextScene = factory.Create("NextLevel");
|
||||||
await nextScene.OnLoadAsync(null);
|
await nextScene.OnLoadAsync(null);
|
||||||
|
|
||||||
|
|||||||
@ -108,7 +108,7 @@ public partial class SaveController : IController
|
|||||||
{
|
{
|
||||||
public void SavePlayer()
|
public void SavePlayer()
|
||||||
{
|
{
|
||||||
var serializer = Context.GetUtility<ISerializer>();
|
var serializer = this.GetUtility<ISerializer>();
|
||||||
|
|
||||||
var player = new PlayerData
|
var player = new PlayerData
|
||||||
{
|
{
|
||||||
@ -132,7 +132,7 @@ public partial class SaveController : IController
|
|||||||
```csharp
|
```csharp
|
||||||
public void LoadPlayer()
|
public void LoadPlayer()
|
||||||
{
|
{
|
||||||
var serializer = Context.GetUtility<ISerializer>();
|
var serializer = this.GetUtility<ISerializer>();
|
||||||
|
|
||||||
string json = "{\"Name\":\"Player1\",\"Level\":10,\"Experience\":1000}";
|
string json = "{\"Name\":\"Player1\",\"Level\":10,\"Experience\":1000}";
|
||||||
|
|
||||||
@ -150,7 +150,7 @@ public void LoadPlayer()
|
|||||||
```csharp
|
```csharp
|
||||||
public void SerializeRuntimeType()
|
public void SerializeRuntimeType()
|
||||||
{
|
{
|
||||||
var serializer = Context.GetUtility<IRuntimeTypeSerializer>();
|
var serializer = this.GetUtility<IRuntimeTypeSerializer>();
|
||||||
|
|
||||||
object data = new PlayerData { Name = "Player1", Level = 10 };
|
object data = new PlayerData { Name = "Player1", Level = 10 };
|
||||||
Type dataType = data.GetType();
|
Type dataType = data.GetType();
|
||||||
@ -182,8 +182,8 @@ public partial class DataManager : IController
|
|||||||
{
|
{
|
||||||
public async Task SaveData()
|
public async Task SaveData()
|
||||||
{
|
{
|
||||||
var serializer = Context.GetUtility<ISerializer>();
|
var serializer = this.GetUtility<ISerializer>();
|
||||||
var storage = Context.GetUtility<IStorage>();
|
var storage = this.GetUtility<IStorage>();
|
||||||
|
|
||||||
var gameData = new GameData
|
var gameData = new GameData
|
||||||
{
|
{
|
||||||
@ -200,8 +200,8 @@ public partial class DataManager : IController
|
|||||||
|
|
||||||
public async Task<GameData> LoadData()
|
public async Task<GameData> LoadData()
|
||||||
{
|
{
|
||||||
var serializer = Context.GetUtility<ISerializer>();
|
var serializer = this.GetUtility<ISerializer>();
|
||||||
var storage = Context.GetUtility<IStorage>();
|
var storage = this.GetUtility<IStorage>();
|
||||||
|
|
||||||
// 从存储读取
|
// 从存储读取
|
||||||
string json = await storage.ReadAsync<string>("game_data");
|
string json = await storage.ReadAsync<string>("game_data");
|
||||||
|
|||||||
@ -134,7 +134,7 @@ public partial class UiController : IController
|
|||||||
{
|
{
|
||||||
public async Task ShowSettings()
|
public async Task ShowSettings()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 压入设置页面(保留当前页面)
|
// 压入设置页面(保留当前页面)
|
||||||
await uiRouter.PushAsync("Settings");
|
await uiRouter.PushAsync("Settings");
|
||||||
@ -142,7 +142,7 @@ public partial class UiController : IController
|
|||||||
|
|
||||||
public async Task CloseSettings()
|
public async Task CloseSettings()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 弹出当前页面(返回上一页)
|
// 弹出当前页面(返回上一页)
|
||||||
await uiRouter.PopAsync();
|
await uiRouter.PopAsync();
|
||||||
@ -150,7 +150,7 @@ public partial class UiController : IController
|
|||||||
|
|
||||||
public async Task ShowMainMenu()
|
public async Task ShowMainMenu()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 替换所有页面(清空 UI 栈)
|
// 替换所有页面(清空 UI 栈)
|
||||||
await uiRouter.ReplaceAsync("MainMenu");
|
await uiRouter.ReplaceAsync("MainMenu");
|
||||||
@ -166,7 +166,7 @@ public partial class UiController : IController
|
|||||||
{
|
{
|
||||||
public void ShowDialog()
|
public void ShowDialog()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 在 Modal 层显示对话框
|
// 在 Modal 层显示对话框
|
||||||
var handle = uiRouter.Show("ConfirmDialog", UiLayer.Modal);
|
var handle = uiRouter.Show("ConfirmDialog", UiLayer.Modal);
|
||||||
@ -174,7 +174,7 @@ public partial class UiController : IController
|
|||||||
|
|
||||||
public void ShowToast(string message)
|
public void ShowToast(string message)
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 在 Toast 层显示提示
|
// 在 Toast 层显示提示
|
||||||
var handle = uiRouter.Show("ToastMessage", UiLayer.Toast,
|
var handle = uiRouter.Show("ToastMessage", UiLayer.Toast,
|
||||||
@ -183,7 +183,7 @@ public partial class UiController : IController
|
|||||||
|
|
||||||
public void ShowLoading()
|
public void ShowLoading()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 在 Topmost 层显示加载界面
|
// 在 Topmost 层显示加载界面
|
||||||
var handle = uiRouter.Show("LoadingScreen", UiLayer.Topmost);
|
var handle = uiRouter.Show("LoadingScreen", UiLayer.Topmost);
|
||||||
@ -314,7 +314,7 @@ public partial class DialogController : IController
|
|||||||
|
|
||||||
public void ShowDialog()
|
public void ShowDialog()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 显示对话框并保存句柄
|
// 显示对话框并保存句柄
|
||||||
_dialogHandle = uiRouter.Show("ConfirmDialog", UiLayer.Modal);
|
_dialogHandle = uiRouter.Show("ConfirmDialog", UiLayer.Modal);
|
||||||
@ -324,7 +324,7 @@ public partial class DialogController : IController
|
|||||||
{
|
{
|
||||||
if (_dialogHandle.HasValue)
|
if (_dialogHandle.HasValue)
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 使用句柄关闭对话框
|
// 使用句柄关闭对话框
|
||||||
uiRouter.Hide(_dialogHandle.Value, UiLayer.Modal, destroy: true);
|
uiRouter.Hide(_dialogHandle.Value, UiLayer.Modal, destroy: true);
|
||||||
@ -345,7 +345,7 @@ public partial class NavigationController : IController
|
|||||||
{
|
{
|
||||||
public void ShowUiStack()
|
public void ShowUiStack()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
Console.WriteLine($"UI 栈深度: {uiRouter.Count}");
|
Console.WriteLine($"UI 栈深度: {uiRouter.Count}");
|
||||||
|
|
||||||
@ -358,13 +358,13 @@ public partial class NavigationController : IController
|
|||||||
|
|
||||||
public bool IsSettingsOpen()
|
public bool IsSettingsOpen()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
return uiRouter.Contains("Settings");
|
return uiRouter.Contains("Settings");
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsTopPage(string uiKey)
|
public bool IsTopPage(string uiKey)
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
return uiRouter.IsTop(uiKey);
|
return uiRouter.IsTop(uiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -381,7 +381,7 @@ public partial class LayerController : IController
|
|||||||
{
|
{
|
||||||
public void ShowMultipleToasts()
|
public void ShowMultipleToasts()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// Toast 层支持重入,可以同时显示多个
|
// Toast 层支持重入,可以同时显示多个
|
||||||
uiRouter.Show("Toast1", UiLayer.Toast);
|
uiRouter.Show("Toast1", UiLayer.Toast);
|
||||||
@ -391,7 +391,7 @@ public partial class LayerController : IController
|
|||||||
|
|
||||||
public void ClearAllToasts()
|
public void ClearAllToasts()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 清空 Toast 层的所有 UI
|
// 清空 Toast 层的所有 UI
|
||||||
uiRouter.ClearLayer(UiLayer.Toast, destroy: true);
|
uiRouter.ClearLayer(UiLayer.Toast, destroy: true);
|
||||||
@ -399,7 +399,7 @@ public partial class LayerController : IController
|
|||||||
|
|
||||||
public void HideAllDialogs()
|
public void HideAllDialogs()
|
||||||
{
|
{
|
||||||
var uiRouter = Context.GetSystem<IUiRouter>();
|
var uiRouter = this.GetSystem<IUiRouter>();
|
||||||
|
|
||||||
// 隐藏 Modal 层的所有对话框
|
// 隐藏 Modal 层的所有对话框
|
||||||
uiRouter.HideByKey("ConfirmDialog", UiLayer.Modal, hideAll: true);
|
uiRouter.HideByKey("ConfirmDialog", UiLayer.Modal, hideAll: true);
|
||||||
|
|||||||
@ -162,8 +162,8 @@ public partial class GameController : IController
|
|||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
_gameStateModel = Context.GetModel<GameStateModel>();
|
_gameStateModel = this.GetModel<GameStateModel>();
|
||||||
|
|
||||||
// 初始化事件监听
|
// 初始化事件监听
|
||||||
InitializeEventListeners();
|
InitializeEventListeners();
|
||||||
@ -180,18 +180,18 @@ public partial class GameController : IController
|
|||||||
public void StartGame()
|
public void StartGame()
|
||||||
{
|
{
|
||||||
_gameStateModel.IsGameRunning.Value = true;
|
_gameStateModel.IsGameRunning.Value = true;
|
||||||
Context.SendEvent(new GameStartEvent());
|
this.SendEvent(new GameStartEvent());
|
||||||
Console.WriteLine("Game started!");
|
Console.WriteLine("Game started!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MovePlayer(Vector2 direction)
|
public void MovePlayer(Vector2 direction)
|
||||||
{
|
{
|
||||||
Context.SendCommand(new MovePlayerCommand { Direction = direction });
|
this.SendCommand(new MovePlayerCommand { Direction = direction });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayerAttack(Vector2 target)
|
public void PlayerAttack(Vector2 target)
|
||||||
{
|
{
|
||||||
Context.SendCommand(new AttackCommand { TargetPosition = target });
|
this.SendCommand(new AttackCommand { TargetPosition = target });
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI 更新回调
|
// UI 更新回调
|
||||||
|
|||||||
@ -348,24 +348,21 @@ public partial class Player : CharacterBody2D, IController
|
|||||||
{
|
{
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 方式 1: 通过 Context 属性访问(由 [ContextAware] 生成)
|
// 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
var gameplaySystem = Context.GetSystem<GameplaySystem>();
|
var gameplaySystem = this.GetSystem<GameplaySystem>();
|
||||||
|
|
||||||
// 方式 2: 通过扩展方法访问(扩展方法基于 IContextAware)
|
|
||||||
var playerModel2 = this.GetModel<PlayerModel>();
|
|
||||||
|
|
||||||
// 发送事件
|
// 发送事件
|
||||||
Context.SendEvent(new PlayerSpawnedEvent());
|
this.SendEvent(new PlayerSpawnedEvent());
|
||||||
|
|
||||||
// 执行命令
|
// 执行命令
|
||||||
Context.SendCommand(new InitPlayerCommand());
|
this.SendCommand(new InitPlayerCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
// 在 Process 中使用架构组件
|
// 在 Process 中使用架构组件
|
||||||
var inputSystem = Context.GetSystem<InputSystem>();
|
var inputSystem = this.GetSystem<InputSystem>();
|
||||||
var movement = inputSystem.GetMovementInput();
|
var movement = inputSystem.GetMovementInput();
|
||||||
|
|
||||||
Velocity = movement * 200;
|
Velocity = movement * 200;
|
||||||
@ -530,12 +527,9 @@ public partial class Player : Node, IController
|
|||||||
{
|
{
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 通过 Context 属性访问架构(由 [ContextAware] 生成)
|
// 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)
|
||||||
var model = Context.GetModel<PlayerModel>();
|
var model = this.GetModel<PlayerModel>();
|
||||||
var system = Context.GetSystem<GameplaySystem>();
|
var system = this.GetSystem<GameplaySystem>();
|
||||||
|
|
||||||
// 或使用扩展方法(扩展方法基于 IContextAware)
|
|
||||||
var model2 = this.GetModel<PlayerModel>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -75,7 +75,7 @@ private System.Collections.IEnumerator WaitUntilCondition()
|
|||||||
GD.Print("等待生命值恢复");
|
GD.Print("等待生命值恢复");
|
||||||
|
|
||||||
// 等待生命值大于 50
|
// 等待生命值大于 50
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
yield return new WaitUntil(() => playerModel.Health.Value > 50);
|
yield return new WaitUntil(() => playerModel.Health.Value > 50);
|
||||||
|
|
||||||
GD.Print("生命值已恢复!");
|
GD.Print("生命值已恢复!");
|
||||||
@ -140,7 +140,7 @@ private System.Collections.IEnumerator EndOfFrameExample()
|
|||||||
```csharp
|
```csharp
|
||||||
private System.Collections.IEnumerator WaitUntilExample()
|
private System.Collections.IEnumerator WaitUntilExample()
|
||||||
{
|
{
|
||||||
var health = Context.GetModel<PlayerModel>().Health;
|
var health = this.GetModel<PlayerModel>().Health;
|
||||||
|
|
||||||
// 持续等待直到条件满足
|
// 持续等待直到条件满足
|
||||||
yield return new WaitUntil(() => health.Value > 0);
|
yield return new WaitUntil(() => health.Value > 0);
|
||||||
@ -156,7 +156,7 @@ private System.Collections.IEnumerator WaitUntilExample()
|
|||||||
```csharp
|
```csharp
|
||||||
private System.Collections.IEnumerator WaitWhileExample()
|
private System.Collections.IEnumerator WaitWhileExample()
|
||||||
{
|
{
|
||||||
var gameState = Context.GetModel<GameModel>();
|
var gameState = this.GetModel<GameModel>();
|
||||||
|
|
||||||
// 等待游戏不再暂停
|
// 等待游戏不再暂停
|
||||||
yield return new WaitWhile(() => gameState.IsPaused.Value);
|
yield return new WaitWhile(() => gameState.IsPaused.Value);
|
||||||
@ -174,7 +174,7 @@ private System.Collections.IEnumerator WaitWhileExample()
|
|||||||
```csharp
|
```csharp
|
||||||
private System.Collections.IEnumerator CombinedWait()
|
private System.Collections.IEnumerator CombinedWait()
|
||||||
{
|
{
|
||||||
var health = Context.GetModel<PlayerModel>().Health;
|
var health = this.GetModel<PlayerModel>().Health;
|
||||||
var button = GetNode<Button>("Button");
|
var button = GetNode<Button>("Button");
|
||||||
|
|
||||||
// 等待生命值恢复或按钮点击(任一条件满足即可)
|
// 等待生命值恢复或按钮点击(任一条件满足即可)
|
||||||
|
|||||||
@ -162,7 +162,7 @@ public partial class PlayerController : Node, IController
|
|||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 获取模型引用
|
// 获取模型引用
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 注册事件监听,自动与节点生命周期绑定
|
// 注册事件监听,自动与节点生命周期绑定
|
||||||
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
||||||
@ -185,7 +185,7 @@ public partial class PlayerController : Node, IController
|
|||||||
MovePlayer(1, 0);
|
MovePlayer(1, 0);
|
||||||
break;
|
break;
|
||||||
case "attack":
|
case "attack":
|
||||||
Context.SendCommand(new AttackCommand());
|
this.SendCommand(new AttackCommand());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,7 +312,7 @@ public partial class UIController : Node, IController
|
|||||||
// Godot 信号 -> 框架事件
|
// Godot 信号 -> 框架事件
|
||||||
this.CreateSignalBuilder(Button.SignalName.Pressed)
|
this.CreateSignalBuilder(Button.SignalName.Pressed)
|
||||||
.Connect(() => {
|
.Connect(() => {
|
||||||
Context.SendEvent(new UIButtonClickEvent { ButtonId = "start_game" });
|
this.SendEvent(new UIButtonClickEvent { ButtonId = "start_game" });
|
||||||
})
|
})
|
||||||
.UnRegisterWhenNodeExitTree(this);
|
.UnRegisterWhenNodeExitTree(this);
|
||||||
|
|
||||||
@ -392,7 +392,7 @@ public partial class WeaponController : Node, IController
|
|||||||
|
|
||||||
protected override void OnInit()
|
protected override void OnInit()
|
||||||
{
|
{
|
||||||
_bulletPool = Context.GetSystem<BulletPoolSystem>();
|
_bulletPool = this.GetSystem<BulletPoolSystem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Shoot(Vector3 direction)
|
public void Shoot(Vector3 direction)
|
||||||
@ -528,7 +528,7 @@ public partial class GameController : Node, IController
|
|||||||
Logger.Debug("Starting game");
|
Logger.Debug("Starting game");
|
||||||
|
|
||||||
// 发送游戏开始事件
|
// 发送游戏开始事件
|
||||||
Context.SendEvent(new GameStartEvent());
|
this.SendEvent(new GameStartEvent());
|
||||||
|
|
||||||
Logger.Info("Game started");
|
Logger.Info("Game started");
|
||||||
}
|
}
|
||||||
@ -536,7 +536,7 @@ public partial class GameController : Node, IController
|
|||||||
public void PauseGame()
|
public void PauseGame()
|
||||||
{
|
{
|
||||||
Logger.Info("Game paused");
|
Logger.Info("Game paused");
|
||||||
Context.SendEvent(new GamePauseEvent());
|
this.SendEvent(new GamePauseEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -610,7 +610,7 @@ public partial class PlayerController : CharacterBody2D, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 输入处理
|
// 输入处理
|
||||||
SetProcessInput(true);
|
SetProcessInput(true);
|
||||||
@ -639,7 +639,7 @@ public partial class PlayerController : CharacterBody2D, IController
|
|||||||
{
|
{
|
||||||
if (CanShoot())
|
if (CanShoot())
|
||||||
{
|
{
|
||||||
var bulletPool = Context.GetSystem<BulletPoolSystem>();
|
var bulletPool = this.GetSystem<BulletPoolSystem>();
|
||||||
var bullet = bulletPool.Spawn("player");
|
var bullet = bulletPool.Spawn("player");
|
||||||
|
|
||||||
if (bullet != null)
|
if (bullet != null)
|
||||||
@ -648,7 +648,7 @@ public partial class PlayerController : CharacterBody2D, IController
|
|||||||
bullet.Initialize(GlobalPosition, direction.Normalized());
|
bullet.Initialize(GlobalPosition, direction.Normalized());
|
||||||
GetTree().Root.AddChild(bullet);
|
GetTree().Root.AddChild(bullet);
|
||||||
|
|
||||||
Context.SendEvent(new BulletFiredEvent());
|
this.SendEvent(new BulletFiredEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -666,7 +666,7 @@ public partial class PlayerController : CharacterBody2D, IController
|
|||||||
private void Die()
|
private void Die()
|
||||||
{
|
{
|
||||||
Logger.Info("Player died");
|
Logger.Info("Player died");
|
||||||
Context.SendEvent(new PlayerDeathEvent());
|
this.SendEvent(new PlayerDeathEvent());
|
||||||
QueueFreeX();
|
QueueFreeX();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,17 +29,17 @@ public partial class PlayerController : IController
|
|||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// Context 属性自动生成,提供架构上下文访问
|
// 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
var combatSystem = Context.GetSystem<CombatSystem>();
|
var combatSystem = this.GetSystem<CombatSystem>();
|
||||||
|
|
||||||
Context.SendEvent(new PlayerInitializedEvent());
|
this.SendEvent(new PlayerInitializedEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Attack(Enemy target)
|
public void Attack(Enemy target)
|
||||||
{
|
{
|
||||||
var damage = Context.GetUtility<DamageCalculator>().Calculate(this, target);
|
var damage = this.GetUtility<DamageCalculator>().Calculate(this, target);
|
||||||
Context.SendCommand(new DealDamageCommand(target, damage));
|
this.SendCommand(new DealDamageCommand(target, damage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -218,8 +218,8 @@ public partial class GameFlowController : IController
|
|||||||
{
|
{
|
||||||
public async Task StartGame()
|
public async Task StartGame()
|
||||||
{
|
{
|
||||||
var saveSystem = Context.GetSystem<SaveSystem>();
|
var saveSystem = this.GetSystem<SaveSystem>();
|
||||||
var uiSystem = Context.GetSystem<UISystem>();
|
var uiSystem = this.GetSystem<UISystem>();
|
||||||
|
|
||||||
await saveSystem.LoadAsync();
|
await saveSystem.LoadAsync();
|
||||||
await uiSystem.ShowMainMenuAsync();
|
await uiSystem.ShowMainMenuAsync();
|
||||||
@ -240,9 +240,9 @@ public partial class PlayerController : Node, IController
|
|||||||
{
|
{
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// Context 属性由 [ContextAware] 自动生成
|
// 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
var combatSystem = Context.GetSystem<CombatSystem>();
|
var combatSystem = this.GetSystem<CombatSystem>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -264,7 +264,7 @@ public class PlayerModel : AbstractModel
|
|||||||
// AbstractModel 已经继承了 ContextAwareBase
|
// AbstractModel 已经继承了 ContextAwareBase
|
||||||
protected override void OnInit()
|
protected override void OnInit()
|
||||||
{
|
{
|
||||||
var config = Context.GetUtility<ConfigLoader>().Load<PlayerConfig>();
|
var config = this.GetUtility<ConfigLoader>().Load<PlayerConfig>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,7 +274,7 @@ public partial class SimpleHelper
|
|||||||
{
|
{
|
||||||
public void DoSomething()
|
public void DoSomething()
|
||||||
{
|
{
|
||||||
Context.SendEvent(new SomethingHappenedEvent());
|
this.SendEvent(new SomethingHappenedEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -334,13 +334,13 @@ public partial class MyController
|
|||||||
// ❌ 错误:构造函数执行时上下文可能未初始化
|
// ❌ 错误:构造函数执行时上下文可能未初始化
|
||||||
public MyController()
|
public MyController()
|
||||||
{
|
{
|
||||||
var model = Context.GetModel<SomeModel>(); // 可能为 null
|
var model = this.GetModel<SomeModel>(); // 可能为 null
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ 正确:在初始化方法中访问
|
// ✅ 正确:在初始化方法中访问
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
var model = Context.GetModel<SomeModel>(); // 安全
|
var model = this.GetModel<SomeModel>(); // 安全
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -353,8 +353,8 @@ public partial class MyController
|
|||||||
{
|
{
|
||||||
public void DoSomething()
|
public void DoSomething()
|
||||||
{
|
{
|
||||||
// ✅ 推荐:使用生成的 Context 属性
|
// ✅ 推荐:使用扩展方法
|
||||||
var model = Context.GetModel<SomeModel>();
|
var model = this.GetModel<SomeModel>();
|
||||||
|
|
||||||
// ❌ 不推荐:显式调用接口方法
|
// ❌ 不推荐:显式调用接口方法
|
||||||
var context = ((IContextAware)this).GetContext();
|
var context = ((IContextAware)this).GetContext();
|
||||||
|
|||||||
@ -172,11 +172,11 @@ public partial class PlayerController : IController
|
|||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// Context 属性自动生成,提供架构上下文访问
|
// 使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口)
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
var combatSystem = Context.GetSystem<CombatSystem>();
|
var combatSystem = this.GetSystem<CombatSystem>();
|
||||||
|
|
||||||
Context.SendEvent(new PlayerInitializedEvent());
|
this.SendEvent(new PlayerInitializedEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -284,10 +284,10 @@ public partial class AdvancedController : IController
|
|||||||
{
|
{
|
||||||
Logger.Info("Processing request");
|
Logger.Info("Processing request");
|
||||||
|
|
||||||
var model = Context.GetModel<PlayerModel>();
|
var model = this.GetModel<PlayerModel>();
|
||||||
Logger.Info($"Player health: {model.Health}");
|
Logger.Info($"Player health: {model.Health}");
|
||||||
|
|
||||||
Context.SendCommand(new ProcessCommand());
|
this.SendCommand(new ProcessCommand());
|
||||||
Logger.Debug("Command sent");
|
Logger.Debug("Command sent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -490,7 +490,7 @@ public partial class EfficientController : IController
|
|||||||
public void Process()
|
public void Process()
|
||||||
{
|
{
|
||||||
Logger.Info("Processing"); // 0 分配
|
Logger.Info("Processing"); // 0 分配
|
||||||
var model = Context.GetModel<PlayerModel>(); // 0 分配
|
var model = this.GetModel<PlayerModel>(); // 0 分配
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,8 +524,8 @@ public partial class GameController : Node, IController
|
|||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 初始化模型和系统引用
|
// 初始化模型和系统引用
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
_combatSystem = Context.GetSystem<CombatSystem>();
|
_combatSystem = this.GetSystem<CombatSystem>();
|
||||||
|
|
||||||
// 监听事件
|
// 监听事件
|
||||||
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
||||||
@ -555,7 +555,7 @@ public partial class GameController : Node, IController
|
|||||||
{
|
{
|
||||||
Logger.Info("Player attacks");
|
Logger.Info("Player attacks");
|
||||||
_combatSystem.ProcessAttack();
|
_combatSystem.ProcessAttack();
|
||||||
Context.SendEvent(new AttackEvent());
|
this.SendEvent(new AttackEvent());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -569,7 +569,7 @@ public partial class GameController : Node, IController
|
|||||||
{
|
{
|
||||||
Logger.Info("Player defends");
|
Logger.Info("Player defends");
|
||||||
_playerModel.IsDefending.Value = true;
|
_playerModel.IsDefending.Value = true;
|
||||||
Context.SendEvent(new DefendEvent());
|
this.SendEvent(new DefendEvent());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -612,7 +612,7 @@ public partial class CharacterController : Node, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_characterModel = Context.GetModel<CharacterModel>();
|
_characterModel = this.GetModel<CharacterModel>();
|
||||||
|
|
||||||
// 监听状态变化
|
// 监听状态变化
|
||||||
_characterModel.State.Register(OnStateChanged);
|
_characterModel.State.Register(OnStateChanged);
|
||||||
@ -658,7 +658,7 @@ public partial class CharacterController : Node, IController
|
|||||||
Logger.Info("Character died");
|
Logger.Info("Character died");
|
||||||
DisableInput();
|
DisableInput();
|
||||||
PlayDeathAnimation();
|
PlayDeathAnimation();
|
||||||
Context.SendEvent(new CharacterDeathEvent());
|
this.SendEvent(new CharacterDeathEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -792,14 +792,14 @@ public partial class RobustComponent : IComponent
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var model = Context.GetModel<RiskyModel>();
|
var model = this.GetModel<RiskyModel>();
|
||||||
model.PerformRiskyOperation();
|
model.PerformRiskyOperation();
|
||||||
Logger.Info("Operation completed successfully");
|
Logger.Info("Operation completed successfully");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Error($"Operation failed: {ex.Message}");
|
Logger.Error($"Operation failed: {ex.Message}");
|
||||||
Context.SendEvent(new OperationFailedEvent { Error = ex.Message });
|
this.SendEvent(new OperationFailedEvent { Error = ex.Message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -168,7 +168,7 @@ public partial class PlayerService : IController
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Context.SendCommand(command);
|
this.SendCommand(command);
|
||||||
Logger.Info($"Player {name} created successfully");
|
Logger.Info($"Player {name} created successfully");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -181,7 +181,7 @@ public partial class PlayerService : IController
|
|||||||
public PlayerData GetPlayer(string name)
|
public PlayerData GetPlayer(string name)
|
||||||
{
|
{
|
||||||
var query = new GetPlayerQuery { PlayerName = name };
|
var query = new GetPlayerQuery { PlayerName = name };
|
||||||
return Context.SendQuery(query);
|
return this.SendQuery(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PlayerData> GetAllPlayers(PlayerClass? classFilter = null, int? minLevel = null)
|
public List<PlayerData> GetAllPlayers(PlayerClass? classFilter = null, int? minLevel = null)
|
||||||
@ -191,13 +191,13 @@ public partial class PlayerService : IController
|
|||||||
FilterByClass = classFilter,
|
FilterByClass = classFilter,
|
||||||
MinLevel = minLevel
|
MinLevel = minLevel
|
||||||
};
|
};
|
||||||
return Context.SendQuery(query);
|
return this.SendQuery(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerStatistics GetPlayerStatistics(string playerName)
|
public PlayerStatistics GetPlayerStatistics(string playerName)
|
||||||
{
|
{
|
||||||
var query = new GetPlayerStatisticsQuery { PlayerName = playerName };
|
var query = new GetPlayerStatisticsQuery { PlayerName = playerName };
|
||||||
return Context.SendQuery(query);
|
return this.SendQuery(query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -1046,13 +1046,13 @@ public class PluginManager : IPluginContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 尝试从架构中获取
|
// 尝试从架构中获取
|
||||||
return _architecture.Context.GetUtility<T>();
|
return _architecture.this.GetUtility<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterEventHandler<T>(IEventHandler<T> handler) where T : IEvent
|
public void RegisterEventHandler<T>(IEventHandler<T> handler) where T : IEvent
|
||||||
{
|
{
|
||||||
_eventHandlers.Add(handler);
|
_eventHandlers.Add(handler);
|
||||||
_architecture.Context.RegisterEvent<T>(handler.Handle);
|
_architecture.this.RegisterEvent<T>(handler.Handle);
|
||||||
_logger.Debug($"Event handler for {typeof(T).Name} registered by plugin system");
|
_logger.Debug($"Event handler for {typeof(T).Name} registered by plugin system");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1167,7 +1167,7 @@ public class ChatService : IChatService
|
|||||||
chatChannel.AddMessage(chatMessage);
|
chatChannel.AddMessage(chatMessage);
|
||||||
|
|
||||||
// 发送事件
|
// 发送事件
|
||||||
_architecture.Context.SendEvent(new ChatMessageReceivedEvent(chatMessage));
|
_architecture.this.SendEvent(new ChatMessageReceivedEvent(chatMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendSystemMessage(string message)
|
public void SendSystemMessage(string message)
|
||||||
@ -1181,7 +1181,7 @@ public class ChatService : IChatService
|
|||||||
};
|
};
|
||||||
|
|
||||||
_channels["global"].AddMessage(systemMessage);
|
_channels["global"].AddMessage(systemMessage);
|
||||||
_architecture.Context.SendEvent(new ChatMessageReceivedEvent(systemMessage));
|
_architecture.this.SendEvent(new ChatMessageReceivedEvent(systemMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
private string FilterMessage(string message)
|
private string FilterMessage(string message)
|
||||||
@ -1444,7 +1444,7 @@ public class NetworkManager : Node, INetworkManager
|
|||||||
private void HandlePlayerPosition(PlayerPositionMessage message)
|
private void HandlePlayerPosition(PlayerPositionMessage message)
|
||||||
{
|
{
|
||||||
// 更新其他玩家位置
|
// 更新其他玩家位置
|
||||||
Context.SendEvent(new NetworkPlayerPositionEvent
|
this.SendEvent(new NetworkPlayerPositionEvent
|
||||||
{
|
{
|
||||||
PlayerId = message.PlayerId,
|
PlayerId = message.PlayerId,
|
||||||
Position = new Vector2(message.X, message.Y)
|
Position = new Vector2(message.X, message.Y)
|
||||||
@ -1454,7 +1454,7 @@ public class NetworkManager : Node, INetworkManager
|
|||||||
private void HandleChatMessage(ChatMessageMessage message)
|
private void HandleChatMessage(ChatMessageMessage message)
|
||||||
{
|
{
|
||||||
// 显示聊天消息
|
// 显示聊天消息
|
||||||
Context.SendEvent(new NetworkChatEvent
|
this.SendEvent(new NetworkChatEvent
|
||||||
{
|
{
|
||||||
PlayerName = message.PlayerName,
|
PlayerName = message.PlayerName,
|
||||||
Message = message.Content,
|
Message = message.Content,
|
||||||
@ -1465,7 +1465,7 @@ public class NetworkManager : Node, INetworkManager
|
|||||||
private void HandlePlayerAction(PlayerActionMessage message)
|
private void HandlePlayerAction(PlayerActionMessage message)
|
||||||
{
|
{
|
||||||
// 处理玩家动作
|
// 处理玩家动作
|
||||||
Context.SendEvent(new NetworkPlayerActionEvent
|
this.SendEvent(new NetworkPlayerActionEvent
|
||||||
{
|
{
|
||||||
PlayerId = message.PlayerId,
|
PlayerId = message.PlayerId,
|
||||||
Action = message.Action,
|
Action = message.Action,
|
||||||
@ -1476,7 +1476,7 @@ public class NetworkManager : Node, INetworkManager
|
|||||||
private void HandleGameState(GameStateMessage message)
|
private void HandleGameState(GameStateMessage message)
|
||||||
{
|
{
|
||||||
// 更新游戏状态
|
// 更新游戏状态
|
||||||
Context.SendEvent(new NetworkGameStateEvent
|
this.SendEvent(new NetworkGameStateEvent
|
||||||
{
|
{
|
||||||
State = message.State,
|
State = message.State,
|
||||||
Data = message.Data
|
Data = message.Data
|
||||||
@ -1574,7 +1574,7 @@ public partial class NetworkController : Node, IController
|
|||||||
{
|
{
|
||||||
var message = new PlayerPositionMessage
|
var message = new PlayerPositionMessage
|
||||||
{
|
{
|
||||||
PlayerId = Context.GetModel<PlayerModel>().PlayerId,
|
PlayerId = this.GetModel<PlayerModel>().PlayerId,
|
||||||
X = position.X,
|
X = position.X,
|
||||||
Y = position.Y,
|
Y = position.Y,
|
||||||
Rotation = 0f // 根据实际需要设置
|
Rotation = 0f // 根据实际需要设置
|
||||||
@ -1587,7 +1587,7 @@ public partial class NetworkController : Node, IController
|
|||||||
{
|
{
|
||||||
var chatMessage = new ChatMessageMessage
|
var chatMessage = new ChatMessageMessage
|
||||||
{
|
{
|
||||||
PlayerName = Context.GetModel<PlayerModel>().Name,
|
PlayerName = this.GetModel<PlayerModel>().Name,
|
||||||
Content = message,
|
Content = message,
|
||||||
Channel = channel
|
Channel = channel
|
||||||
};
|
};
|
||||||
@ -1598,13 +1598,13 @@ public partial class NetworkController : Node, IController
|
|||||||
private void OnNetworkConnected()
|
private void OnNetworkConnected()
|
||||||
{
|
{
|
||||||
Logger.Info("Connected to network server");
|
Logger.Info("Connected to network server");
|
||||||
Context.SendEvent(new NetworkConnectedEvent());
|
this.SendEvent(new NetworkConnectedEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnNetworkDisconnected(string reason)
|
private void OnNetworkDisconnected(string reason)
|
||||||
{
|
{
|
||||||
Logger.Info($"Disconnected from network server: {reason}");
|
Logger.Info($"Disconnected from network server: {reason}");
|
||||||
Context.SendEvent(new NetworkDisconnectedEvent { Reason = reason });
|
this.SendEvent(new NetworkDisconnectedEvent { Reason = reason });
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnNetworkMessageReceived(NetworkMessage message)
|
private void OnNetworkMessageReceived(NetworkMessage message)
|
||||||
@ -1615,7 +1615,7 @@ public partial class NetworkController : Node, IController
|
|||||||
private void OnConnectionFailed(string error)
|
private void OnConnectionFailed(string error)
|
||||||
{
|
{
|
||||||
Logger.Error($"Network connection failed: {error}");
|
Logger.Error($"Network connection failed: {error}");
|
||||||
Context.SendEvent(new NetworkConnectionFailedEvent { Error = error });
|
this.SendEvent(new NetworkConnectionFailedEvent { Error = error });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -492,8 +492,8 @@ namespace MyShooterGame.Controllers
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 监听玩家死亡(通过 Context 属性访问架构)
|
// 监听玩家死亡(使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口))
|
||||||
var playerModel = Context.GetModel<PlayerModel>();
|
var playerModel = this.GetModel<PlayerModel>();
|
||||||
playerModel.IsAlive.RegisterOnValueChanged(isAlive =>
|
playerModel.IsAlive.RegisterOnValueChanged(isAlive =>
|
||||||
{
|
{
|
||||||
if (!isAlive)
|
if (!isAlive)
|
||||||
@ -648,8 +648,8 @@ public partial class MenuController : Control, IController
|
|||||||
{
|
{
|
||||||
GD.Print("开始游戏");
|
GD.Print("开始游戏");
|
||||||
|
|
||||||
// 初始化游戏(通过 Context 属性访问架构)
|
// 初始化游戏(使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口))
|
||||||
var gameplaySystem = Context.GetSystem<GameplaySystem>();
|
var gameplaySystem = this.GetSystem<GameplaySystem>();
|
||||||
gameplaySystem.StartNewGame();
|
gameplaySystem.StartNewGame();
|
||||||
|
|
||||||
// 切换到游戏场景
|
// 切换到游戏场景
|
||||||
@ -687,13 +687,13 @@ public partial class GameScene : Node2D, IController
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 初始化生成系统(通过 Context 属性访问架构)
|
// 初始化生成系统(使用扩展方法访问架构([ContextAware] 实现 IContextAware 接口))
|
||||||
_spawnSystem = Context.GetSystem<SpawnSystem>();
|
_spawnSystem = this.GetSystem<SpawnSystem>();
|
||||||
_spawnSystem.Initialize(this, EnemyScene);
|
_spawnSystem.Initialize(this, EnemyScene);
|
||||||
_spawnSystem.StartSpawning();
|
_spawnSystem.StartSpawning();
|
||||||
|
|
||||||
// 监听游戏状态
|
// 监听游戏状态
|
||||||
var gameModel = Context.GetModel<GameModel>();
|
var gameModel = this.GetModel<GameModel>();
|
||||||
gameModel.IsPlaying.RegisterOnValueChanged(isPlaying =>
|
gameModel.IsPlaying.RegisterOnValueChanged(isPlaying =>
|
||||||
{
|
{
|
||||||
if (!isPlaying)
|
if (!isPlaying)
|
||||||
|
|||||||
@ -186,7 +186,7 @@ public partial class PlayerController : CharacterBody2D, IController
|
|||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
// 设置上下文
|
// 设置上下文
|
||||||
_playerModel = Context.GetModel<PlayerModel>();
|
_playerModel = this.GetModel<PlayerModel>();
|
||||||
|
|
||||||
// 注册事件监听,自动与节点生命周期绑定
|
// 注册事件监听,自动与节点生命周期绑定
|
||||||
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
this.RegisterEvent<PlayerInputEvent>(OnPlayerInput)
|
||||||
@ -396,7 +396,7 @@ public partial class SignalController : Node, IController
|
|||||||
_timer.Start();
|
_timer.Start();
|
||||||
|
|
||||||
// 发送框架事件
|
// 发送框架事件
|
||||||
Context.SendEvent(new ButtonClickEvent { ButtonId = "main_button" });
|
this.SendEvent(new ButtonClickEvent { ButtonId = "main_button" });
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTimerTimeout()
|
private void OnTimerTimeout()
|
||||||
@ -407,7 +407,7 @@ public partial class SignalController : Node, IController
|
|||||||
_progressBar.Value += 10;
|
_progressBar.Value += 10;
|
||||||
|
|
||||||
// 发送框架事件
|
// 发送框架事件
|
||||||
Context.SendEvent(new TimerTimeoutEvent());
|
this.SendEvent(new TimerTimeoutEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnProgressChanged(double value)
|
private void OnProgressChanged(double value)
|
||||||
@ -415,7 +415,7 @@ public partial class SignalController : Node, IController
|
|||||||
Logger.Debug($"Progress changed: {value}");
|
Logger.Debug($"Progress changed: {value}");
|
||||||
|
|
||||||
// 发送框架事件
|
// 发送框架事件
|
||||||
Context.SendEvent(new ProgressChangeEvent { Value = value });
|
this.SendEvent(new ProgressChangeEvent { Value = value });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -450,7 +450,7 @@ public partial class SignalEventBridge : Node, IController
|
|||||||
// Godot 信号 -> 框架事件
|
// Godot 信号 -> 框架事件
|
||||||
this.CreateSignalBuilder(_uiButton.SignalName.Pressed)
|
this.CreateSignalBuilder(_uiButton.SignalName.Pressed)
|
||||||
.Connect(() => {
|
.Connect(() => {
|
||||||
Context.SendEvent(new UIActionEvent {
|
this.SendEvent(new UIActionEvent {
|
||||||
Action = "button_click",
|
Action = "button_click",
|
||||||
Source = "main_button"
|
Source = "main_button"
|
||||||
});
|
});
|
||||||
@ -459,7 +459,7 @@ public partial class SignalEventBridge : Node, IController
|
|||||||
|
|
||||||
this.CreateSignalBuilder(_healthBar.SignalName.HealthDepleted)
|
this.CreateSignalBuilder(_healthBar.SignalName.HealthDepleted)
|
||||||
.Connect(() => {
|
.Connect(() => {
|
||||||
Context.SendEvent(new PlayerDeathEvent { Source = "health_system" });
|
this.SendEvent(new PlayerDeathEvent { Source = "health_system" });
|
||||||
})
|
})
|
||||||
.UnRegisterWhenNodeExitTree(this);
|
.UnRegisterWhenNodeExitTree(this);
|
||||||
}
|
}
|
||||||
@ -599,7 +599,7 @@ public partial class SmartResourceLoader : Node, IController
|
|||||||
Logger.Info($"Resource loaded: {request.Path}");
|
Logger.Info($"Resource loaded: {request.Path}");
|
||||||
|
|
||||||
// 发送资源加载完成事件
|
// 发送资源加载完成事件
|
||||||
Context.SendEvent(new ResourceLoadedEvent {
|
this.SendEvent(new ResourceLoadedEvent {
|
||||||
Path = request.Path,
|
Path = request.Path,
|
||||||
Resource = resource
|
Resource = resource
|
||||||
});
|
});
|
||||||
@ -608,7 +608,7 @@ public partial class SmartResourceLoader : Node, IController
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Error($"Failed to load resource {request.Path}: {ex.Message}");
|
Logger.Error($"Failed to load resource {request.Path}: {ex.Message}");
|
||||||
Context.SendEvent(new ResourceLoadFailedEvent {
|
this.SendEvent(new ResourceLoadFailedEvent {
|
||||||
Path = request.Path,
|
Path = request.Path,
|
||||||
Error = ex.Message
|
Error = ex.Message
|
||||||
});
|
});
|
||||||
@ -751,7 +751,7 @@ public partial class SceneResourcePreloader : Node, IController
|
|||||||
|
|
||||||
foreach (var assetPath in resourceSet.RequiredAssets)
|
foreach (var assetPath in resourceSet.RequiredAssets)
|
||||||
{
|
{
|
||||||
Context.SendEvent(new ResourceLoadRequestEvent {
|
this.SendEvent(new ResourceLoadRequestEvent {
|
||||||
Path = assetPath,
|
Path = assetPath,
|
||||||
Priority = ResourcePriority.High
|
Priority = ResourcePriority.High
|
||||||
});
|
});
|
||||||
@ -977,7 +977,7 @@ public partial class MemoryOptimizedController : Node, IController
|
|||||||
Logger.Warning($"High memory usage detected: {memoryUsage / 1024 / 1024} MB");
|
Logger.Warning($"High memory usage detected: {memoryUsage / 1024 / 1024} MB");
|
||||||
|
|
||||||
// 触发内存清理
|
// 触发内存清理
|
||||||
Context.SendEvent(new HighMemoryUsageEvent {
|
this.SendEvent(new HighMemoryUsageEvent {
|
||||||
CurrentUsage = memoryUsage,
|
CurrentUsage = memoryUsage,
|
||||||
Threshold = threshold
|
Threshold = threshold
|
||||||
});
|
});
|
||||||
@ -1163,7 +1163,7 @@ public class PlayingState : IGameState
|
|||||||
public void Enter(StateMachineController controller)
|
public void Enter(StateMachineController controller)
|
||||||
{
|
{
|
||||||
controller.GetTree().Paused = false;
|
controller.GetTree().Paused = false;
|
||||||
controller.Context.SendEvent(new GameStartEvent());
|
controller.this.SendEvent(new GameStartEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(StateMachineController controller, double delta) { }
|
public void Update(StateMachineController controller, double delta) { }
|
||||||
@ -1178,7 +1178,7 @@ public class PlayingState : IGameState
|
|||||||
|
|
||||||
public void Exit(StateMachineController controller)
|
public void Exit(StateMachineController controller)
|
||||||
{
|
{
|
||||||
controller.Context.SendEvent(new GamePauseEvent());
|
controller.this.SendEvent(new GamePauseEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -339,7 +339,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void ShowPauseStatus()
|
public void ShowPauseStatus()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
Console.WriteLine("\n=== 暂停状态 ===");
|
Console.WriteLine("\n=== 暂停状态 ===");
|
||||||
Console.WriteLine($"全局暂停: {pauseManager.IsPaused(PauseGroup.Global)}");
|
Console.WriteLine($"全局暂停: {pauseManager.IsPaused(PauseGroup.Global)}");
|
||||||
@ -369,7 +369,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void TestNestedPause()
|
public void TestNestedPause()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
Console.WriteLine("--- 测试嵌套暂停 ---\n");
|
Console.WriteLine("--- 测试嵌套暂停 ---\n");
|
||||||
|
|
||||||
@ -409,7 +409,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void TestPauseScope()
|
public void TestPauseScope()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
Console.WriteLine("--- 测试暂停作用域 ---\n");
|
Console.WriteLine("--- 测试暂停作用域 ---\n");
|
||||||
|
|
||||||
@ -437,7 +437,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void EmergencyClearAll()
|
public void EmergencyClearAll()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
Console.WriteLine("⚠️ 紧急清除所有暂停状态");
|
Console.WriteLine("⚠️ 紧急清除所有暂停状态");
|
||||||
pauseManager.ClearAll();
|
pauseManager.ClearAll();
|
||||||
@ -450,7 +450,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void ClearGroup(PauseGroup group)
|
public void ClearGroup(PauseGroup group)
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
Console.WriteLine($"清除 {group} 组的所有暂停");
|
Console.WriteLine($"清除 {group} 组的所有暂停");
|
||||||
pauseManager.ClearGroup(group);
|
pauseManager.ClearGroup(group);
|
||||||
@ -683,7 +683,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void TogglePauseMenu()
|
public void TogglePauseMenu()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
if (_pauseMenuToken.HasValue)
|
if (_pauseMenuToken.HasValue)
|
||||||
{
|
{
|
||||||
@ -705,7 +705,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void ShowDialog(string message)
|
public void ShowDialog(string message)
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
|
|
||||||
// 对话框使用全局暂停
|
// 对话框使用全局暂停
|
||||||
_dialogToken = pauseManager.Push($"对话框: {message}", PauseGroup.Global);
|
_dialogToken = pauseManager.Push($"对话框: {message}", PauseGroup.Global);
|
||||||
@ -723,7 +723,7 @@ namespace MyGame.Controllers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
pauseManager.Pop(_dialogToken.Value);
|
pauseManager.Pop(_dialogToken.Value);
|
||||||
_dialogToken = null;
|
_dialogToken = null;
|
||||||
Console.WriteLine("对话框已关闭");
|
Console.WriteLine("对话框已关闭");
|
||||||
@ -734,7 +734,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task RunGameLoop()
|
public async Task RunGameLoop()
|
||||||
{
|
{
|
||||||
var pauseManager = Context.GetUtility<IPauseStackManager>();
|
var pauseManager = this.GetUtility<IPauseStackManager>();
|
||||||
int frame = 0;
|
int frame = 0;
|
||||||
|
|
||||||
Console.WriteLine("\n=== 游戏循环开始 ===\n");
|
Console.WriteLine("\n=== 游戏循环开始 ===\n");
|
||||||
|
|||||||
@ -410,7 +410,7 @@ namespace MyGame.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task StartGame(int level = 1)
|
public async Task StartGame(int level = 1)
|
||||||
{
|
{
|
||||||
var stateMachine = Context.GetSystem<IStateMachineSystem>();
|
var stateMachine = this.GetSystem<IStateMachineSystem>();
|
||||||
|
|
||||||
// 设置加载状态的目标关卡
|
// 设置加载状态的目标关卡
|
||||||
var loadingState = stateMachine.GetState<LoadingState>();
|
var loadingState = stateMachine.GetState<LoadingState>();
|
||||||
|
|||||||
@ -832,7 +832,7 @@ public class ArchitectureIntegrationTests
|
|||||||
await _architecture.DestroyAsync();
|
await _architecture.DestroyAsync();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var system = _architecture.Context.GetSystem<TestSystem>();
|
var system = _architecture.this.GetSystem<TestSystem>();
|
||||||
Assert.That(system!.DestroyCalled, Is.True);
|
Assert.That(system!.DestroyCalled, Is.True);
|
||||||
Assert.That(_architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Destroyed));
|
Assert.That(_architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Destroyed));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user