using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class HealthUI : TargetedHudElement<HealthComponent>
{
    [SerializeField] protected Image _face;
    [SerializeField] protected Sprite[] _faceSprites;
    [SerializeField] protected Image _bar;
    [SerializeField] protected TextMeshProUGUI _percent;
    protected HealthComponent _hc;

    override public void Init (HealthComponent h)
    {
        _hc = h;
    }

    void Update()
    {
        float ratio = _hc.health / _hc.healthMax;
        float inverseratio = 1 - (ratio);
        _bar.fillAmount = inverseratio;
        int ind = Mathf.Clamp((Mathf.RoundToInt((ratio) * _faceSprites.Length)) - 1,0,_faceSprites.Length-1);
        _face.sprite = _faceSprites[ind];
        _percent.text = (int)(inverseratio * 100f) + "%";
    }
}
