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

public class HealthBar : MonoBehaviour {

    public Image bar;
    PlayerController host;

    bool isReady = false;

    public void SetPlayer(PlayerController _host, Color color)
    {
        host = _host;
        isReady = true;

        bar.color = color;
    }

    private void Update()
    {
        if (isReady)
        {
            if(host != null)
            {
                if (host.IsDead == false)
                {
                    transform.position = new Vector3(host.transform.position.x,
                                                    host.transform.position.y - 0.4f,
                                                    host.transform.position.z);
                }
            }

        }
    }

    public void AdjustHealthBar(int hpLeft, int maxHp)
    {
        float percent;
        if (hpLeft == 0)
        {
            percent = 0;
        }
        percent = (float)hpLeft / (float)maxHp;
        bar.fillAmount = percent;
    }
}
