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

49 lines
1.7 KiB
C#
Raw Normal View History

2025-03-07 16:25:59 -05:00
using System.Collections.Generic;
2025-04-16 19:57:54 -04:00
using UnityEngine; using Game; using Music; using Player;
2025-03-07 16:25:59 -05:00
using UnityEngine.UI;
2025-04-16 19:57:54 -04:00
namespace Game
{
2025-03-07 16:25:59 -05:00
public class LifeDisplayManager : MonoBehaviour
{
public GameObject players;
public GameObject playerPrefab;
public GameObject lifePrefab;
public Dictionary<Damageable, List<GameObject>> lifeDisplays = new Dictionary<Damageable, List<GameObject>>();
private void Start() // Creates life icons for each player
2025-03-07 16:25:59 -05:00
{
2025-03-20 14:45:29 -04:00
if (GameManager.gameMode == GameManager.GameMode.freeForAll)
2025-03-07 16:25:59 -05:00
{
2025-03-20 14:45:29 -04:00
foreach (GameObject player in GameManager.players)
2025-03-07 16:25:59 -05:00
{
2025-03-20 14:45:29 -04:00
Transform parent = Instantiate(playerPrefab, players.transform).transform;
List<GameObject> lives = new List<GameObject>();
for (int i = 0; i < player.GetComponent<Damageable>().lives; i++)
{
GameObject life = Instantiate(lifePrefab, parent);
2025-03-28 13:00:37 -04:00
life.transform.Find("LIFE").GetComponent<Image>().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
2025-03-20 14:45:29 -04:00
lives.Add(life);
}
lifeDisplays.Add(player.GetComponent<Damageable>(), lives);
2025-03-07 16:25:59 -05:00
}
}
}
private void Update() // Updates the lives displayed based on player lives
2025-03-07 16:25:59 -05:00
{
foreach (Damageable damageable in lifeDisplays.Keys)
{
foreach (GameObject life in lifeDisplays[damageable])
{
life.SetActive(lifeDisplays[damageable].IndexOf(life) < damageable.lives);
}
}
}
2025-03-08 13:33:19 -05:00
public void HideLifeDisplay() // Hides life display
2025-03-08 13:33:19 -05:00
{
players.SetActive(false);
}
2025-03-07 16:25:59 -05:00
}
2025-04-16 19:57:54 -04:00
}