108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Code.Infrastructure.Input;
|
|
using Code.OfficeGame.BattleMode.Actions.UI;
|
|
using Cysharp.Threading.Tasks;
|
|
using R3;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.OfficeGame.BattleMode.Actions.Deciders
|
|
{
|
|
public class CountBattleActionDecider : BattleActionDecider
|
|
{
|
|
private WaitActionCountSelectorPanel _countSelectorPanel;
|
|
public override DeciderType DeciderType => DeciderType.Count;
|
|
|
|
private int _decidedCount;
|
|
private BattleActionDecisionResultCode _resultCode;
|
|
private CancellationTokenSource _waitForEnterTokenSource = new();
|
|
|
|
private IDisposable _sliderDisposable;
|
|
private IInputService _inputService;
|
|
|
|
public CountBattleActionDecider(WaitActionCountSelectorPanel countSelectorPanel, IInputService inputService)
|
|
{
|
|
_inputService = inputService;
|
|
_countSelectorPanel = countSelectorPanel;
|
|
}
|
|
|
|
public override async UniTask<BattleActionDecisionResult> GetDecision()
|
|
{
|
|
if (!IsMounted) throw new Exception("Decider is not mounted");
|
|
|
|
_waitForEnterTokenSource = new CancellationTokenSource();
|
|
_resultCode = BattleActionDecisionResultCode.Deciding;
|
|
_countSelectorPanel.CountSlider.enabled = true;
|
|
|
|
await UniTask.WaitUntilCanceled(_waitForEnterTokenSource.Token).SuppressCancellationThrow();
|
|
|
|
_countSelectorPanel.CountSlider.enabled = false;
|
|
|
|
_waitForEnterTokenSource.Dispose();
|
|
_waitForEnterTokenSource = null;
|
|
|
|
return new CountBattleActionDeciderResult()
|
|
{
|
|
Count = _decidedCount,
|
|
ResultCode = _resultCode
|
|
};
|
|
}
|
|
|
|
private void Cancel(InputAction.CallbackContext obj)
|
|
{
|
|
_resultCode = BattleActionDecisionResultCode.CancelDecision;
|
|
|
|
_waitForEnterTokenSource.Cancel();
|
|
}
|
|
|
|
private void Confirm(InputAction.CallbackContext obj)
|
|
{
|
|
_resultCode = BattleActionDecisionResultCode.Decided;
|
|
|
|
_waitForEnterTokenSource.Cancel();
|
|
}
|
|
|
|
public override async UniTask UnmountDecider()
|
|
{
|
|
if (!IsMounted) return;
|
|
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Confirm,
|
|
Confirm);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Cancel,
|
|
Cancel);
|
|
|
|
_sliderDisposable.Dispose();
|
|
|
|
await _countSelectorPanel.Close();
|
|
|
|
IsMounted = false;
|
|
}
|
|
|
|
public override async UniTask MountDecider(EntityProperties entityProperties)
|
|
{
|
|
if (IsMounted) return;
|
|
|
|
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Confirm,
|
|
Confirm);
|
|
_inputService.BindActionPerformed((int)ActionMappings.BattleMode, (int)BattleModeActions.Cancel,
|
|
Cancel);
|
|
|
|
_decidedCount = 0;
|
|
|
|
await _countSelectorPanel.Open();
|
|
|
|
_countSelectorPanel.SetRange(1, 150);
|
|
|
|
_sliderDisposable = _countSelectorPanel.CountSlider.OnValueChangedAsObservable()
|
|
.Subscribe(val =>
|
|
{
|
|
// if (_suppressSliderCallback) return;
|
|
_decidedCount = (int)val;
|
|
_waitForEnterTokenSource.Cancel();
|
|
});
|
|
|
|
IsMounted = true;
|
|
}
|
|
|
|
}
|
|
} |