using System.Collections.Generic;
using UnityEngine;
using Game;
using Music;
using Player;
namespace Game
{
///
/// This class manages the health bars for all players in the game.
/// It creates, updates, and removes health bars as needed.
///
public class HealthBarManager : MonoBehaviour
{
///
/// The template used to create new health bars.
///
public GameObject healthBarPrefab;
///
/// A dictionary that links each player to their health bar.
///
private Dictionary playerHealthBars = new Dictionary();
///
/// Sets up event listeners for when the game starts and ends.
///
private void Start()
{
GameManager.Instance.StartGameEvent += OnGameStart;
GameManager.Instance.EndGameEvent += OnGameEnd;
}
///
/// Removes event listeners when this object is destroyed.
///
private void OnDestroy()
{
GameManager.Instance.StartGameEvent -= OnGameStart;
GameManager.Instance.EndGameEvent -= OnGameEnd;
}
///
/// Updates the position of each health bar to follow its player.
///
private void Update()
{
foreach (var kvp in playerHealthBars)
{
GameObject player = kvp.Key;
if (player == null) continue;
GameObject healthBar = kvp.Value;
// Position the health bar slightly above the player
healthBar.transform.SetPositionAndRotation(
new Vector3(player.transform.position.x, player.transform.position.y + 1.5f, player.transform.position.z),
Quaternion.identity
);
}
}
///
/// Creates health bars for all players when the game starts.
///
private void OnGameStart()
{
foreach (GameObject player in GameManager.players)
{
if (!playerHealthBars.ContainsKey(player))
{
CreateHealthBar(player);
// Listen for the player's death and respawn events
var damageable = player.GetComponent();
damageable.OnPlayerDeath += HandlePlayerDeath;
damageable.OnPlayerRespawn += HandlePlayerRespawn;
}
}
}
///
/// Creates a health bar for a specific player.
///
/// The player to create a health bar for.
private void CreateHealthBar(GameObject player)
{
// Create a new health bar and link it to the player
GameObject healthBar = Instantiate(healthBarPrefab);
healthBar.transform.localScale *= 1.5f; // Make the health bar slightly larger
healthBar.GetComponent().SetPlayer(player);
playerHealthBars[player] = healthBar;
}
///
/// Handles the player's respawn by recreating their health bar if needed.
///
/// The player who respawned.
private void HandlePlayerRespawn(GameObject player)
{
if (!playerHealthBars.ContainsKey(player))
{
CreateHealthBar(player);
}
}
///
/// Handles the player's death by removing their health bar.
///
/// The player who died.
private void HandlePlayerDeath(GameObject player)
{
if (playerHealthBars.TryGetValue(player, out GameObject healthBar))
{
Destroy(healthBar);
playerHealthBars.Remove(player);
}
}
///
/// Cleans up all health bars and unsubscribes from player events when the game ends.
///
private void OnGameEnd()
{
// Remove all health bars
foreach (var kvp in playerHealthBars)
{
Destroy(kvp.Value);
}
playerHealthBars.Clear();
// Unsubscribe from all player events
foreach (GameObject player in GameManager.players)
{
if (player != null && player.TryGetComponent(out var damageable))
{
damageable.OnPlayerDeath -= HandlePlayerDeath;
damageable.OnPlayerRespawn -= HandlePlayerRespawn;
}
}
}
}
}