跳到主要内容

IBattleSystem

用于从游戏代码与战斗系统交互的契约。提供战斗状态检查和事件订阅。

ibattle system

你只需开始战斗并监听。接口背后的一切都可以改变 —— 包括把整场战斗搬到服务器上 —— 而无需改动你的代码。

命名空间:OpenMon.MCE.SDK

定义​

public interface IBattleSystem
{
bool IsInBattle { get; }
event Action OnBattleStarted;
event Action<bool> OnBattleEnded;
}
引擎钩子

engine.Battle 是一个 BattleSystemFacade。当引擎调用它的钩子 NotifyBattleStarted() 和 NotifyBattleEnded(bool playerWon) 时,它会设置 IsInBattle,并触发 OnBattleStarted 和 OnBattleEnded。在当前的引擎中,还没有任何代码调用这些钩子,因此 IsInBattle 始终为 false,这两个事件也不会触发。在它们接通之前,请通过战斗启动器来观察战斗;它已在 DI 中绑定,并且确实会发出通知:IBattleLauncher.BattleInProgress、SubscribeToBattleLaunched、SubscribeToBattleToUnload 和 SubscribeToBattleEnded(Action<BattleResultParameters>)。

属性​

IsInBattle​

当前是否有战斗正在进行。

类型:bool

从战斗场景加载的那一刻起,到战斗结果处理完毕、回到世界场景为止,一直返回 true。

事件​

OnBattleStarted​

战斗开始时触发,发生在战斗场景加载完成且 BattleManager 初始化之后。

签名:event Action OnBattleStarted

OnBattleEnded​

战斗结束时触发,发生在战斗场景卸载之前。

签名:event Action<bool> OnBattleEnded

参数类型描述
playerWonbool玩家获胜为 true;玩家落败、逃跑或被击败为 false

用法示例​

using OpenMon.MCE.SDK;
using Zenject;

public class BattleTracker : MonoBehaviour
{
// Only IMonsterCaptureEngine is bound in DI; reach subsystems through it.
[Inject] private IMonsterCaptureEngine engine;

private IBattleSystem battle;

private int totalBattles;
private int wins;

private void Awake()
{
battle = engine.Battle;
}

private void OnEnable()
{
battle.OnBattleStarted += OnBattleStart;
battle.OnBattleEnded += OnBattleEnd;
}

private void OnDisable()
{
battle.OnBattleStarted -= OnBattleStart;
battle.OnBattleEnded -= OnBattleEnd;
}

private void OnBattleStart()
{
totalBattles++;
}

private void OnBattleEnd(bool playerWon)
{
if (playerWon) wins++;
Debug.Log($"Record: {wins}/{totalBattles}");
}

private void Update()
{
// Disable certain UI when in battle
if (battle.IsInBattle)
{
// Hide overworld HUD elements
}
}
}
内存管理

请务必在 OnDisable() 或 OnDestroy() 中取消事件订阅,以防止内存泄漏。这对于可能在场景切换期间被销毁的 MonoBehaviour 尤其重要。