using UnityEngine;

public class Character : MonoBehaviour
{
    // [Range(0 , 3)]
    public float MaxHP = 3;
    public GameObject HPGauge;

    float HP;

    private float HPMaxWidth;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        HP = MaxHP;
        
        if (HPGauge != null)
        {
            HPMaxWidth = HPGauge.GetComponent<RectTransform>().sizeDelta.x;
        }
    }

    public void Initalize()
    {
        HP = MaxHP;
    }
    
    /*
     * 살아있으면 true를 리턴한다.
     */
    public bool Hit(float damage)
    {
        HP -= damage;

        if (HP < 0)
        {
            HP = 0;
        }
        
        if(HPGauge != null)
        {
            HPGauge.GetComponent<RectTransform>().sizeDelta = new Vector2(HP/MaxHP * HPMaxWidth, HPGauge.GetComponent<RectTransform>().sizeDelta.y);
        }
        return HP > 0;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
