89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
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;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Code.OfficeGame.BattleMode.UI
|
|
{
|
|
public class BattleActionDecisionPanel : UIPanel
|
|
{
|
|
[SerializeField] private float duration = 0.35f;
|
|
[SerializeField] private Button actionButtonPrefab;
|
|
|
|
private List<Button> _actionButtons = new();
|
|
|
|
private UniTaskCompletionSource<BattleActionType> _decisionCompletionSource;
|
|
private MotionHandle _motionHandle;
|
|
|
|
public void SetEntityInfo(Dictionary<BattleActionType, BattleAction> battleActions)
|
|
{
|
|
_decisionCompletionSource = new();
|
|
|
|
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.onClick.AddListener(() => _decisionCompletionSource.TrySetResult(action));
|
|
|
|
//button.GetComponentInChildren<Text>().text = action.ToString();
|
|
}
|
|
}
|
|
|
|
public async UniTask<BattleActionType> GetDecision(CancellationToken cts)
|
|
{
|
|
try
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
} |