SamsonGame/Assets/Scripts/Game/UI/Elements/RewardTimerButton.cs

84 lines
2.2 KiB
C#
Raw Permalink Normal View History

2021-12-29 20:50:11 +03:00
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace RND
{
public class RewardTimerButton : RewardButton
{
[Header("Fields")] [SerializeField] private Image _timer;
[SerializeField] private Text _power;
[Header("Settings")] [SerializeField, Range(1, 100)] private int _maximumPower = 5;
[SerializeField, Range(1, 100)] private int _minimumPower = 2;
[SerializeField] private Color _timerLoadColour;
public int Power { get; private set; } = 5;
private Color _textColour;
protected override void Init()
{
_textColour = _power.color;
}
public override void Begin()
{
base.Begin();
Power = _maximumPower;
_minimumPower = Mathf.Clamp(_minimumPower, 1, _maximumPower);
_timer.fillAmount = 0f;
_timer.gameObject.Hide();
_power.gameObject.Hide();
}
protected override void OnRewardLoaded()
{
base.OnRewardLoaded();
StartCoroutine(PowerReduceRoutine());
}
private IEnumerator PowerReduceRoutine()
{
_power.text = $"x{Power}";
_power.color = _timerLoadColour;
_power.gameObject.Show();
_timer.fillAmount = 0f;
_timer.color = _timerLoadColour;
_timer.gameObject.Show();
const float firstReduceDelay = 0.33f;
yield return new WaitForSeconds(firstReduceDelay);
while (Power > _minimumPower)
{
_power.text = $"x{Power}";
float currentFill = _timer.fillAmount;
const float mainReduceDelay = 1f;
yield return Tween.DoTween(mainReduceDelay,
t => { _timer.fillAmount = Mathf.Lerp(currentFill, 1f, t); });
_timer.fillAmount = 0f;
Power--;
}
_power.text = $"x{Power}";
_power.color = _textColour;
_timer.color = _textColour;
yield return null;
}
protected void OnDisable()
{
StopAllCoroutines();
}
}
}