KitchenChaos/Assets/Scripts/UI/ProgressBarUI.cs

57 lines
1.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using static StoveCounter;
namespace UI
{
public class ProgressBarUI : MonoBehaviour
{
[SerializeField] private GameObject hasProgressGameObject;
[SerializeField] private Image barImage;
private IHasProgress hasProgress;
private void Start()
{
if (!hasProgressGameObject.TryGetComponent<IHasProgress>(out hasProgress))
{
Debug.LogError($"Game Object {hasProgressGameObject} does not have a component that implements IHasProgress!");
}
else
{
hasProgress.OnProgressChanged += HasProgress_OnProgressChanged;
}
barImage.fillAmount = 0f;
Hide();
}
private void HasProgress_OnProgressChanged(object sender, IHasProgress.ProgressChangedEventArgs e)
{
barImage.fillAmount = e.ProgressNormalized;
barImage.color = e.State == State.Fried
? Color.Lerp(Color.green, Color.red, t: e.ProgressNormalized)
: Color.Lerp(Color.blue, Color.green, t: e.ProgressNormalized);
if (e.ProgressNormalized is 0f or 1f)
{
Hide();
}
else
{
Show();
}
}
private void Show()
{
gameObject.SetActive(true);
}
private void Hide()
{
gameObject.SetActive(false);
}
}
}