Files
LowPolyFPS/Assets/Scripts/UIController.cs

109 lines
2.6 KiB
C#
Raw Permalink Normal View History

2026-10-12 21:28:47 +01:00
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIController : MonoBehaviour
{
public static UIController Instance { get; private set; }
[Header("HUD Elements")]
[SerializeField] private Slider healthBar;
[SerializeField] private TextMeshProUGUI ammoText;
[SerializeField] private TextMeshProUGUI zombieCountText;
[SerializeField] private TextMeshProUGUI notificationText;
[SerializeField] private GameObject gameOverPanel;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
private void Start()
{
if (PlayerHealthController.Instance != null)
{
PlayerHealthController.Instance.OnHealthChanged += UpdateHealthBar;
UpdateHealthBar(PlayerHealthController.Instance.CurrentHealth, PlayerHealthController.Instance.MaxHealth);
}
if (gameOverPanel != null)
{
gameOverPanel.SetActive(false);
}
}
private void OnDestroy()
{
if (PlayerHealthController.Instance != null)
{
PlayerHealthController.Instance.OnHealthChanged -= UpdateHealthBar;
}
}
public void UpdateHealthBar(int current, int max)
{
if (healthBar != null && max > 0)
{
healthBar.value = (float)current / max;
}
}
public void UpdateAmmoDisplay(int current, int max)
{
if (ammoText != null)
{
ammoText.text = $"{current} / {max}";
}
}
public void UpdateZombiesRemaining(int count)
{
if (zombieCountText != null)
{
zombieCountText.text = $"Zombies Left: {count}";
}
}
public void ShowNotification(string message)
{
if (notificationText != null)
{
notificationText.text = message;
CancelInvoke(nameof(ClearNotification));
Invoke(nameof(ClearNotification), 4f);
}
}
private void ClearNotification()
{
if (notificationText != null)
{
notificationText.text = "";
}
}
public void ShowGameOverScreen()
{
if (gameOverPanel != null)
{
gameOverPanel.SetActive(true);
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
public void RestartCurrentScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}