IBattleSystem
Contract for interacting with the battle system from game code. Provides battle state checking and event subscription.
Namespace: OpenMon.MCE.SDK
定义
public interface IBattleSystem
{
bool IsInBattle { get; }
event Action OnBattleStarted;
event Action<bool> OnBattleEnded;
}
属性
IsInBattle
Whether a battle is currently in progress.
Type: bool
Returns true from the moment a battle scene loads until the battle results are processed and the overworld resumes.
事件
OnBattleStarted
Fired when a battle begins, after the battle scene has loaded and the BattleManager has initialized.
Signature: event Action OnBattleStarted
OnBattleEnded
Fired when a battle concludes, before the battle scene unloads.
Signature: event Action<bool> OnBattleEnded
| 参数 | 类型 | 描述 |
|---|---|---|
playerWon | bool | true if the player won, false if the player lost, fled, or was defeated |
用法示例
using OpenMon.MCE.SDK;
using Zenject;
public class BattleTracker : MonoBehaviour
{
[Inject] private IBattleSystem battle;
private int totalBattles;
private int wins;
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
}
}
}
Memory Management
Always unsubscribe from events in OnDisable() or OnDestroy() to prevent memory leaks. This is especially important for MonoBehaviours that can be destroyed during scene transitions.