initial commit
This commit is contained in:
108
Assets/Scripts/UIController.cs
Normal file
108
Assets/Scripts/UIController.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user