Files
Crash-Course/Assets/Scripts/Game/HealthBarManager.cs

147 lines
5.0 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-04-18 15:54:50 -04:00
namespace Game
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class manages the health bars for all players in the game.
/// It creates, updates, and removes health bars as needed.
/// </summary>
public class HealthBarManager : MonoBehaviour
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// The template used to create new health bars.
/// </summary>
public GameObject healthBarPrefab;
2025-04-18 15:54:50 -04:00
/// <summary>
/// A dictionary that links each player to their health bar.
/// </summary>
private Dictionary<GameObject, GameObject> playerHealthBars = new Dictionary<GameObject, GameObject>();
2025-04-18 15:54:50 -04:00
/// <summary>
/// Sets up event listeners for when the game starts and ends.
/// </summary>
2025-04-20 12:35:58 -04:00
private void Awake()
{
2025-04-20 12:35:58 -04:00
print("Doing event stuff");
2025-04-18 15:54:50 -04:00
GameManager.Instance.StartGameEvent += OnGameStart;
GameManager.Instance.EndGameEvent += OnGameEnd;
2025-04-20 12:35:58 -04:00
print("Done event stuff");
2025-04-18 15:54:50 -04:00
}
2025-03-08 13:33:19 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Removes event listeners when this object is destroyed.
/// </summary>
private void OnDestroy()
{
GameManager.Instance.StartGameEvent -= OnGameStart;
GameManager.Instance.EndGameEvent -= OnGameEnd;
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Updates the position of each health bar to follow its player.
/// </summary>
private void Update()
{
2025-04-18 15:54:50 -04:00
foreach (var kvp in playerHealthBars)
{
2025-04-18 15:54:50 -04:00
GameObject player = kvp.Key;
if (player == null) continue;
GameObject healthBar = kvp.Value;
2025-04-18 15:54:50 -04:00
// 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
);
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Creates health bars for all players when the game starts.
/// </summary>
private void OnGameStart()
{
2025-04-20 12:35:58 -04:00
print("Ongame start called");
2025-04-18 15:54:50 -04:00
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>();
damageable.OnPlayerDeath += HandlePlayerDeath;
damageable.OnPlayerRespawn += HandlePlayerRespawn;
}
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Creates a health bar for a specific player.
/// </summary>
/// <param name="player">The player to create a health bar for.</param>
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<TerribleHealthBarScript>().SetPlayer(player);
playerHealthBars[player] = healthBar;
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Handles the player's respawn by recreating their health bar if needed.
/// </summary>
/// <param name="player">The player who respawned.</param>
private void HandlePlayerRespawn(GameObject player)
{
2025-04-18 15:54:50 -04:00
if (!playerHealthBars.ContainsKey(player))
{
CreateHealthBar(player);
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Handles the player's death by removing their health bar.
/// </summary>
/// <param name="player">The player who died.</param>
private void HandlePlayerDeath(GameObject player)
{
2025-04-18 15:54:50 -04:00
if (playerHealthBars.TryGetValue(player, out GameObject healthBar))
{
Destroy(healthBar);
playerHealthBars.Remove(player);
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Cleans up all health bars and unsubscribes from player events when the game ends.
/// </summary>
private void OnGameEnd()
{
2025-04-20 12:35:58 -04:00
print("Ongame end called");
2025-04-18 15:54:50 -04:00
// 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)
{
2025-04-18 15:54:50 -04:00
if (player != null && player.TryGetComponent<Damageable>(out var damageable))
{
damageable.OnPlayerDeath -= HandlePlayerDeath;
damageable.OnPlayerRespawn -= HandlePlayerRespawn;
}
}
}
}
2025-04-16 19:57:54 -04:00
}