Exemple #1
0
    public void DisplayHealth(int health, int maxHealth)
    {
        if (health == maxHealth)
        {
            AtFullHealth(0.5f);
            damageIndicator.MarkLowHealth(false);
        }
        else if (health <= 20 && health > 0 && !damageIndicator.LowHealth)
        {
            damageIndicator.MarkLowHealth(true);
        }
        else if (health > 20)
        {
            damageIndicator.MarkLowHealth(false);
        }

        Color c = health > 20 ? standardHealthColor : lowHealthColor;

        if (c != currentHealthbarColor)
        {
            ChangeHealthBarColor(c);
            currentHealthbarColor = c;
        }

        float divisor  = 10.0f * (maxHealth / 100.0f);
        int   barsUsed = (int)Mathf.Ceil(health / divisor);

        for (int i = 0; i < healthbars.Length; i++)
        {
            healthbars[i].fillAmount = 1;
            if (i <= barsUsed - 1)
            {
                healthbars[i].gameObject.SetActive(true);
            }
            else
            {
                healthbars[i].gameObject.SetActive(false);
            }
        }

        float diff = (barsUsed * divisor) - health;

        float fillAmount = diff > 0 ? 1 - diff / divisor : 1;

        int index = barsUsed > 0 ? barsUsed - 1 : 0;

        healthbars[index].fillAmount = fillAmount;
        healthText.text = health.ToString();
    }