using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour
{
    private static PlayerUI instance;
    public static PlayerUI Instance
    {
        get
        {
            if(instance == null)
            {
                instance = FindAnyObjectByType<PlayerUI>();
            }
            return instance;
        }
    }

    //血条
    public Image HealthBar1;
    public Image HealthBar2;
    //血量值
    public float maxHealth = 100f;
    public float hp1 = 100f;
    public float hp2 = 100f;

    private void Update()
    {
        UpdateHealthBar();
    }

    void UpdateHealthBar()
    {
        if (HealthBar1 != null)
        {
            float FillAmountPlayer1 = hp1 / maxHealth;
            FillAmountPlayer1 = Mathf.Clamp01(FillAmountPlayer1);
            HealthBar1.fillAmount = FillAmountPlayer1;
            HealthBar1.rectTransform.localScale = new Vector3( FillAmountPlayer1,1, 1);
            //HealthBar1.rectTransform.localPosition = new Vector3(0, HealthBar1.rectTransform.localPosition.y,
                //HealthBar1.rectTransform.localPosition.z);
                //HealthBar1.rectTransform.anchorMin = new Vector2(0, 0.5f);
                //HealthBar1.rectTransform.anchorMax = new Vector2(0, 0.5f);
            HealthBar1.rectTransform.pivot = new Vector2(0, 0.5f);
        }

        if (HealthBar2 != null)
        {
            float FillAmountPlayer2 = hp2 / maxHealth;
            FillAmountPlayer2 = Mathf.Clamp01(FillAmountPlayer2);
            HealthBar2.fillAmount = FillAmountPlayer2;
            HealthBar2.rectTransform.localScale = new Vector3( FillAmountPlayer2, 1,1);
            HealthBar2.rectTransform.pivot = new Vector2(1, 0.5f);
        }
    }

    public void SetHealth1(float health)
    {
        if (hp1 == 0) return;
        hp1 = Mathf.Clamp(hp1 - health, 0, maxHealth);
        if (hp1 == 0) Manager.Instance.Result(1);
    }
    public void SetHealth2(float health)
    {
        if (hp2 == 0) return;
        hp2 = Mathf.Clamp(hp2 - health, 0, maxHealth);
        if (hp2 == 0) Manager.Instance.Result(0);
    }
}
