SamsonGame/Assets/Scripts/Game/UI/LevelRewardFiller.cs

60 lines
1.9 KiB
C#

using System;
using DG.Tweening;
using game;
using UnityEngine;
using UnityEngine.UI;
namespace RND
{
public class LevelRewardFiller : MonoBehaviour
{
public event Action OnCompletedFilling;
private float _fillingTime = 2f;
private float _startProgress;
private Image _backgroundImage;
private Image _fillingImage;
private Text _progressText;
private void Awake()
{
_backgroundImage = transform.FindRecursive("BackgroundImg").GetComponent<Image>();
_fillingImage = transform.FindRecursive("FilledImg").GetComponent<Image>();
_progressText = transform.FindRecursive("ProgressText").GetComponent<Text>();
}
public void SetItem(ConfLevelRewardItem item, float startProgress)
{
_startProgress = startProgress;
_backgroundImage.sprite = Assets.Create<Sprite>(item.hidedPreviewIcon);
_fillingImage.sprite = Assets.Create<Sprite>(item.openPreviewIcon);
_fillingImage.fillAmount = _startProgress / 100f;
SetProgressText(_startProgress / 100f);
}
public void FillTo(float progress)
{
Sequence seq = DOTween.Sequence();
seq.Append(_fillingImage.DOFillAmount(progress / 100f, _fillingTime).SetEase(Ease.OutQuad));
seq.Join(FillTextSequence(progress, _fillingTime, Ease.OutQuad));
seq.AppendCallback(OnFillEnded);
seq.Play();
}
private void OnFillEnded() => OnCompletedFilling?.Invoke();
private Tweener FillTextSequence(float to, float duration, Ease curve) =>
DOTween.To(SetProgressText, _startProgress, to, duration).SetEase(curve);
private void SetProgressText(float progress)
{
_progressText.text = $"{progress:F0}%";
}
}
}