Merge branch 'main' of https://github.com/RochesterX/Sophomore-Project
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Assets/Scripts/Game/ObstacleEnd.cs
Normal file
27
Assets/Scripts/Game/ObstacleEnd.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Game;
|
||||
using UnityEngine;
|
||||
|
||||
public class ObstacleEnd : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
private void UpdateVisibility() // Sets object active if playing obstacle course
|
||||
{
|
||||
if (GameManager.gameMode == GameManager.GameMode.obstacleCourse)
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Game/ObstacleEnd.cs.meta
Normal file
2
Assets/Scripts/Game/ObstacleEnd.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69fc903d39ac3d24aab4ce7a68dba447
|
||||
Reference in New Issue
Block a user