using System;
using UnityEngine;

namespace Unicorn
{
    public class HpBar : MonoBehaviour
    {
        private LivingEntity _parent;
        public GameObject fill;
         
        private void Start()
        {
            if (!transform.parent.TryGetComponent(out _parent))
            {
                gameObject.SetActive(false);
                return;
            }
            UpdateHealth();
        }

        public void UpdateHealth()
        {
            var scale = Math.Max(_parent.health, 0f) / _parent.maxHealth;
            fill.transform.localScale = new Vector3(scale, 1, 1);
            fill.transform.localPosition = new Vector3((scale-1) / 2, 0, 0);
            
            //1 -> 0
            //0 -> -0.5
        }
    }
}