Health Bar complete
health bar now gets destroyed when players die, and get created when players spawn/respawn. This fixes issue where health bar stays on screen after player dies.
This commit is contained in:
@@ -38,21 +38,58 @@ public class HealthBarManager : MonoBehaviour
|
||||
{
|
||||
if (!playerHealthBars.ContainsKey(player))
|
||||
{
|
||||
GameObject healthBar = Instantiate(healthBarPrefab);
|
||||
healthBar.transform.localScale *= 1.5f;
|
||||
healthBar.GetComponent<TerribleHealthBarScript>().SetPlayer(player);
|
||||
playerHealthBars[player] = healthBar;
|
||||
CreateHealthBar(player);
|
||||
|
||||
// Subscribe to the player's death and respawn events
|
||||
var damageable = player.GetComponent<Damageable>();
|
||||
damageable.OnPlayerDeath += HandlePlayerDeath;
|
||||
damageable.OnPlayerRespawn += HandlePlayerRespawn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGameEnd() // Destroys the health bars when the game ends
|
||||
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()
|
||||
{
|
||||
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<Damageable>(out var damageable))
|
||||
{
|
||||
damageable.OnPlayerDeath -= HandlePlayerDeath;
|
||||
damageable.OnPlayerRespawn -= HandlePlayerRespawn;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@ public class Damageable : MonoBehaviour
|
||||
public bool damageSelfDebug = false;
|
||||
public bool dying = false;
|
||||
public event System.Action<GameObject> OnPlayerPunched;
|
||||
public event System.Action<GameObject> OnPlayerDeath;
|
||||
public event System.Action<GameObject> OnPlayerRespawn;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -111,7 +113,7 @@ public class Damageable : MonoBehaviour
|
||||
}
|
||||
animator.SetBool("die", true);
|
||||
dying = true;
|
||||
|
||||
OnPlayerDeath?.Invoke(gameObject);
|
||||
AudioManager.Instance.PlaySound("Death Simple");
|
||||
}
|
||||
}
|
||||
@@ -135,6 +137,9 @@ public class Damageable : MonoBehaviour
|
||||
{
|
||||
damageable.ResetDamage();
|
||||
}
|
||||
|
||||
// Trigger the player respawn event
|
||||
OnPlayerRespawn?.Invoke(gameObject);
|
||||
}
|
||||
|
||||
public void ResetDamage()
|
||||
|
||||
Reference in New Issue
Block a user