AlicizaX/Client/Assets/Scripts/New/Example/Battle/ExampleBattleScopeController.cs
2026-03-25 18:33:59 +08:00

60 lines
1.7 KiB
C#

using UnityEngine;
namespace Aliciza.Services.Example
{
public sealed class ExampleBattleScopeController : MonoBehaviour
{
[SerializeField] private int _scopeOrder = 100;
[SerializeField] private bool _toggleByInput = true;
private ServiceScope _battleScope;
public bool HasBattleScope => _battleScope != null && !_battleScope.IsDisposed;
private void Update()
{
if (!_toggleByInput) return;
if (!GameServices.TryGet<IInputService>(out var inputService)) return;
if (!inputService.ToggleBattlePressedThisFrame) return;
if (HasBattleScope) ExitBattle();
else EnterBattle();
}
private void OnDestroy()
{
ExitBattle();
}
[ContextMenu("Enter Battle")]
public void EnterBattle()
{
if (HasBattleScope) return;
if (!GameServices.HasWorld)
{
Debug.LogWarning("[ExampleBattleScopeController] Cannot create Battle scope: ServiceWorld does not exist.");
return;
}
_battleScope = GameServices.CreateScope<BattleScope>(_scopeOrder);
_battleScope.Register(new BattleLoopService());
Debug.Log("[ExampleBattleScopeController] Created BattleScope.");
}
[ContextMenu("Exit Battle")]
public void ExitBattle()
{
if (_battleScope == null) return;
if (GameServices.HasWorld)
GameServices.DestroyScope<BattleScope>();
_battleScope = null;
Debug.Log("[ExampleBattleScopeController] Destroyed BattleScope.");
}
}
}