using System; using System.Collections.Generic; using System.Threading; using Code.Infrastructure.UI; using Code.OfficeGame.BattleMode.Actions; using Cysharp.Threading.Tasks; using LitMotion; using LitMotion.Extensions; using UnityEngine; namespace Code.OfficeGame.BattleMode.UI { public class BattleActionDecisionPanel : UIPanel { [SerializeField] private float duration = 0.35f; [SerializeField] private BattleActionDecisionButton actionButtonPrefab; private List _actionButtons = new(); private MotionHandle _motionHandle; private UniTaskCompletionSource _decisionCompletionSource; public void SetEntityInfo(Dictionary battleActions) { foreach (var button in _actionButtons) Destroy(button.gameObject); _actionButtons.Clear(); foreach (var action in battleActions.Keys) { var button = Instantiate(actionButtonPrefab, transform); _actionButtons.Add(button); button.button.onClick.AddListener(() => _decisionCompletionSource.TrySetResult(action)); button.text.text = action.ToString(); } } public async UniTask GetDecision(CancellationToken cts) { try { _decisionCompletionSource = new(); var result = await _decisionCompletionSource.Task .AttachExternalCancellation(cts); return result; } catch (OperationCanceledException ex) { return BattleActionType.None; } } public override async UniTask Open(CancellationToken cts = default) { gameObject.SetActive(true); float startY = -((RectTransform)transform).rect.height; float targetY = 0f; _motionHandle = LMotion.Create(startY, targetY, duration) .WithEase(Ease.OutCubic) .WithScheduler(MotionScheduler.UpdateIgnoreTimeScale) .BindToAnchoredPositionY(((RectTransform)transform)); await _motionHandle.ToUniTask(cts); } public override async UniTask Close(CancellationToken cts = default) { float startY = ((RectTransform)transform).anchoredPosition.y; float targetY = -((RectTransform)transform).rect.height; _motionHandle = LMotion.Create(startY, targetY, duration) .WithEase(Ease.InBack) .WithScheduler(MotionScheduler.UpdateIgnoreTimeScale) .BindToAnchoredPositionY((RectTransform)transform); await _motionHandle.ToUniTask(cts); gameObject.SetActive(true); } } }