using System.Collections; using System.Collections.Generic; using UnityEngine; using Game; using Music; using Player; using TMPro; namespace Game { /// /// This class manages the health bar visuals for a player, including updating /// the health bar's size, position, and color based on the player's current health. /// public class TerribleHealthBarScript : MonoBehaviour { /// /// The color of the health bar when the player is at full health. /// public Color fullHealthColor; /// /// The color of the health bar when the player is at zero health. /// public Color fullDeathColor; /// /// The color used to represent health subtraction. /// public Color subtractionColor; /// /// The visual representation of the health bar. /// public GameObject healthVisual; /// /// The actual health bar that reflects the player's current health. /// public GameObject actualHealthVisual; /// /// The visual representation of the player's death state. /// public GameObject deathVisual; /// /// The speed at which the health bar updates smoothly. /// public float smoothSpeed = 0.1f; /// /// The text element displaying the player's current and maximum health. /// public TextMeshProUGUI text; /// /// The component of the player, used to track health. /// private Damageable healthScript; /// /// The initial scale of the health bar. /// private Vector3 initialScale; /// /// The initial position of the health bar. /// private Vector3 initialPosition; /// /// The target scale of the health bar based on the player's current health. /// private Vector3 targetScale; /// /// The target position of the health bar based on the player's current health. /// private Vector3 targetPosition; /// /// The target color of the actual health bar based on the player's current health. /// private Color targetActualColor; /// /// The player associated with this health bar. /// public GameObject player; /// /// Initializes the health bar for the specified player. /// private void Start() { InitializePlayer(player); } /// /// Updates the health bar visuals to reflect the player's current health. /// private void Update() { if (player == null || healthScript == null) { return; } // Calculate the health ratio and update the health bar visuals float healthRatio = (healthScript.maxDamage - healthScript.damage) / healthScript.maxDamage; targetActualColor = Color.Lerp(fullDeathColor, fullHealthColor, healthRatio); targetScale = new Vector3(Mathf.Lerp(0, 1, healthRatio) * initialScale.x, healthVisual.transform.localScale.y, healthVisual.transform.localScale.z); targetPosition = new Vector3(Mathf.Lerp(-0.5f, 0, healthRatio), healthVisual.transform.localPosition.y, healthVisual.transform.localPosition.z); text.text = (healthScript.maxDamage - healthScript.damage).ToString() + "/" + healthScript.maxDamage.ToString(); // Smoothly update the health bar's scale, position, and color actualHealthVisual.transform.localScale = targetScale; actualHealthVisual.transform.localPosition = targetPosition; healthVisual.transform.localScale = Vector3.Lerp(healthVisual.transform.localScale, targetScale, smoothSpeed); healthVisual.transform.localPosition = Vector3.Lerp(healthVisual.transform.localPosition, targetPosition, smoothSpeed); actualHealthVisual.GetComponent().color = Color.Lerp(actualHealthVisual.GetComponent().color, targetActualColor, smoothSpeed); deathVisual.GetComponent().color = Color.Lerp(deathVisual.GetComponent().color, targetActualColor * 0.5f, smoothSpeed); healthVisual.GetComponent().color = subtractionColor; } /// /// Sets the player associated with this health bar. /// /// The player to associate with this health bar. public void SetPlayer(GameObject player) { InitializePlayer(player); } /// /// Initializes the health bar for the specified player. /// /// The player to initialize the health bar for. private void InitializePlayer(GameObject player) { this.player = player; if (this.player == null) { return; } // Get the Damageable component of the player healthScript = player.GetComponent(); if (healthScript == null) { return; } Initialize(); } /// /// Sets up the initial state of the health bar. /// private void Initialize() { initialScale = healthVisual.transform.localScale; initialPosition = healthVisual.transform.position; targetScale = initialScale; targetPosition = initialPosition; targetActualColor = actualHealthVisual.GetComponent().color; } } }