using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// GAME 1303 Tier 1 — On-screen HUD: health, lives, score, timer, item counters.
/// Every field is optional — leave empty what your game doesn't use.
///
/// SETUP
/// 1. Canvas -> add an empty "HUD" object with this script.
/// 2. Health: EITHER a Slider (Health Bar) OR a row of heart Images (Heart Icons list).
/// 3. Item counters: for each ItemType you want to show, add an entry with the
/// ItemType asset and a TMP_Text. Format uses {0} for the count: "x {0}".
/// 4. Wire events in the Inspector:
/// Player Health.OnHealthChanged -> HUDController.UpdateHealth
/// RespawnManager.OnLivesChanged -> HUDController.UpdateLives
/// CollectionTracker.OnCountChanged-> HUDController.UpdateItemCount
/// CollectionTracker.OnScoreChanged-> HUDController.UpdateScore
/// Timer updates itself from GameManager.ElapsedTime.
///
/// Requires TextMeshPro (Window -> TextMeshPro -> Import TMP Essentials).
/// </summary>
public class HUDController : MonoBehaviour
{
[Header("Health (pick one style)")]
public Slider healthBar;
public List<Image> heartIcons = new List<Image>();
public Sprite heartFull;
public Sprite heartEmpty;
[Header("Lives / Score / Timer")]
public TMP_Text livesText;
public string livesFormat = "Lives: {0}";
public TMP_Text scoreText;
public string scoreFormat = "Score: {0}";
public TMP_Text timerText;
[System.Serializable]
public class ItemCounter
{
public ItemType itemType;
public TMP_Text text;
public string format = "x {0}";
public Image icon; // optional; auto-filled from ItemType.icon
}
[Header("Item Counters")]
public List<ItemCounter> itemCounters = new List<ItemCounter>();
void Start()
{
foreach (var c in itemCounters)
{
if (c.icon != null && c.itemType != null && c.itemType.icon != null) c.icon.sprite = c.itemType.icon;
if (c.text != null) c.text.text = string.Format(c.format, 0);
}
if (scoreText) scoreText.text = string.Format(scoreFormat, 0);
}
void Update()
{
if (timerText != null && GameManager.Instance != null)
{
float t = GameManager.Instance.ElapsedTime;
timerText.text = $"{(int)(t / 60):00}:{(int)(t % 60):00}.{(int)(t * 100 % 100):00}";
}
}
public void UpdateHealth(int current, int max)
{
if (healthBar != null)
{
healthBar.maxValue = max;
healthBar.value = current;
}
for (int i = 0; i < heartIcons.Count; i++)
{
if (heartIcons[i] == null) continue;
heartIcons[i].enabled = i < max;
heartIcons[i].sprite = i < current ? heartFull : heartEmpty;
}
}
public void UpdateLives(int lives)
{
if (livesText) livesText.text = string.Format(livesFormat, lives);
}
public void UpdateScore(int score)
{
if (scoreText) scoreText.text = string.Format(scoreFormat, score);
}
public void UpdateItemCount(ItemType type, int count)
{
foreach (var c in itemCounters)
if (c.itemType == type && c.text != null)
c.text.text = string.Format(c.format, count);
}
}