2025-03-06 01:27:42 -05:00
|
|
|
using System.Collections.Generic;
|
2025-04-16 19:57:54 -04:00
|
|
|
using UnityEngine; using Game; using Music; using Player;
|
|
|
|
|
namespace Game
|
|
|
|
|
{
|
2025-03-06 01:27:42 -05:00
|
|
|
|
|
|
|
|
public class HealthBarManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public GameObject healthBarPrefab;
|
|
|
|
|
private Dictionary<GameObject, GameObject> playerHealthBars = new Dictionary<GameObject, GameObject>();
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
GameManager.Instance.StartGameEvent += OnGameStart;
|
|
|
|
|
GameManager.Instance.EndGameEvent += OnGameEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
GameManager.Instance.StartGameEvent -= OnGameStart;
|
|
|
|
|
GameManager.Instance.EndGameEvent -= OnGameEnd;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
void Update() // Updates position of health bars to follow each player
|
2025-03-06 01:27:42 -05:00
|
|
|
{
|
|
|
|
|
foreach (var kvp in playerHealthBars)
|
|
|
|
|
{
|
|
|
|
|
GameObject player = kvp.Key;
|
2025-03-08 13:33:19 -05:00
|
|
|
if (player == null) continue;
|
|
|
|
|
|
2025-03-06 01:27:42 -05:00
|
|
|
GameObject healthBar = kvp.Value;
|
2025-03-07 10:03:16 -05:00
|
|
|
healthBar.transform.SetPositionAndRotation(new Vector3(player.transform.position.x, player.transform.position.y + 1.5f, player.transform.position.z), Quaternion.identity);
|
2025-03-06 01:27:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void OnGameStart() // Creates health bars for each player
|
2025-03-06 01:27:42 -05:00
|
|
|
{
|
|
|
|
|
foreach (GameObject player in GameManager.players)
|
|
|
|
|
{
|
|
|
|
|
if (!playerHealthBars.ContainsKey(player))
|
|
|
|
|
{
|
2025-04-17 17:36:23 -04:00
|
|
|
CreateHealthBar(player);
|
|
|
|
|
|
|
|
|
|
// Subscribe to the player's death and respawn events
|
|
|
|
|
var damageable = player.GetComponent<Damageable>();
|
2025-04-17 18:56:54 -04:00
|
|
|
//damageable.OnPlayerDeath += HandlePlayerDeath;
|
|
|
|
|
//damageable.OnPlayerRespawn += HandlePlayerRespawn;
|
2025-03-06 01:27:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 17:36:23 -04:00
|
|
|
private void HandlePlayerRespawn(GameObject player)
|
|
|
|
|
{
|
|
|
|
|
if (!playerHealthBars.ContainsKey(player))
|
|
|
|
|
{
|
|
|
|
|
CreateHealthBar(player);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateHealthBar(GameObject player)
|
|
|
|
|
{
|
|
|
|
|
GameObject healthBar = Instantiate(healthBarPrefab);
|
|
|
|
|
healthBar.transform.localScale *= 1.5f;
|
|
|
|
|
healthBar.GetComponent<TerribleHealthBarScript>().SetPlayer(player);
|
|
|
|
|
playerHealthBars[player] = healthBar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandlePlayerDeath(GameObject player)
|
|
|
|
|
{
|
|
|
|
|
if (playerHealthBars.TryGetValue(player, out GameObject healthBar))
|
|
|
|
|
{
|
|
|
|
|
Destroy(healthBar);
|
|
|
|
|
playerHealthBars.Remove(player);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGameEnd()
|
2025-03-06 01:27:42 -05:00
|
|
|
{
|
|
|
|
|
foreach (var kvp in playerHealthBars)
|
|
|
|
|
{
|
|
|
|
|
Destroy(kvp.Value);
|
|
|
|
|
}
|
|
|
|
|
playerHealthBars.Clear();
|
2025-04-17 17:36:23 -04:00
|
|
|
|
|
|
|
|
// Unsubscribe from all player events
|
|
|
|
|
foreach (GameObject player in GameManager.players)
|
|
|
|
|
{
|
|
|
|
|
if (player != null && player.TryGetComponent<Damageable>(out var damageable))
|
|
|
|
|
{
|
2025-04-17 18:56:54 -04:00
|
|
|
//damageable.OnPlayerDeath -= HandlePlayerDeath;
|
|
|
|
|
//damageable.OnPlayerRespawn -= HandlePlayerRespawn;
|
2025-04-17 17:36:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-06 01:27:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-16 19:57:54 -04:00
|
|
|
}
|