using System.Collections.Generic;
using UnityEngine;
using Game;
using Music;
using Player;
using UnityEngine.UI;
namespace Game
{
///
/// This class manages the display of player lives, including creating life icons
/// and updating them based on the player's remaining lives.
///
public class LifeDisplayManager : MonoBehaviour
{
///
/// The parent object that contains all player life displays.
///
public GameObject players;
///
/// The prefab used to represent a player in the life display.
///
public GameObject playerPrefab;
///
/// The prefab used to represent a single life icon.
///
public GameObject lifePrefab;
///
/// A dictionary mapping each player's component
/// to their corresponding list of life icons.
///
public Dictionary> lifeDisplays = new Dictionary>();
///
/// Initializes the life display by creating life icons for each player.
///
private void Start()
{
// Only initialize life displays in free-for-all game mode
if (GameManager.gameMode == GameManager.GameMode.freeForAll)
{
foreach (GameObject player in GameManager.players)
{
// Create a parent object for the player's life icons
Transform parent = Instantiate(playerPrefab, players.transform).transform;
// Create life icons based on the player's number of lives
List lives = new List();
for (int i = 0; i < player.GetComponent().lives; i++)
{
GameObject life = Instantiate(lifePrefab, parent);
// Set the color of the life icon to match the player's color
life.transform.Find("LIFE").GetComponent().color = GameManager.playerColors[GameManager.players.IndexOf(player)];
lives.Add(life);
}
// Map the player's Damageable component to their life icons
lifeDisplays.Add(player.GetComponent(), lives);
}
}
}
///
/// Updates the life display to reflect the current number of lives for each player.
///
private void Update()
{
foreach (Damageable damageable in lifeDisplays.Keys)
{
// Enable or disable life icons based on the player's remaining lives
foreach (GameObject life in lifeDisplays[damageable])
{
life.SetActive(lifeDisplays[damageable].IndexOf(life) < damageable.lives);
}
}
}
///
/// Hides the life display by deactivating the parent object.
///
public void HideLifeDisplay()
{
players.SetActive(false);
}
}
}