Files
Kursach_StealthAction/Assets/Scripts/PlayerAmmoUI.cs
T
2026-10-23 15:44:09 +03:00

126 lines
3.5 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class PlayerAmmoUI : MonoBehaviour
{
[Header("Ссылки")]
public PlayerGun playerGun;
public Text ammoText;
public Text statusText;
public Image ammoIcon;
[Header("Цвета")]
public Color normalColor = Color.white;
public Color lowAmmoColor = new Color(1f, 0.6f, 0.1f, 1f); // Оранжевый при малом запасе
public Color noAmmoColor = new Color(0.95f, 0.2f, 0.2f, 1f); // Красный когда пусто
public Color reloadingColor = new Color(1f, 0.85f, 0.2f, 1f); // Желтый при перезарядке
void Start()
{
if (playerGun == null)
{
playerGun = FindAnyObjectByType<PlayerGun>();
}
UpdateAmmoUI();
}
void Update()
{
UpdateAmmoUI();
}
private void UpdateAmmoUI()
{
if (playerGun == null)
{
playerGun = FindAnyObjectByType<PlayerGun>();
if (playerGun == null) return;
}
bool isEquipped = playerGun.IsEquipped;
int current = playerGun.CurrentAmmo;
int reserve = playerGun.ReserveAmmo;
bool reloading = playerGun.IsReloading;
// Если оружие убрано (в руках кулаки)
if (!isEquipped)
{
if (ammoIcon != null)
{
ammoIcon.color = new Color(1f, 1f, 1f, 0.35f);
}
if (ammoText != null)
{
ammoText.text = "КУЛАКИ";
ammoText.color = normalColor;
}
if (statusText != null)
{
statusText.text = "";
statusText.gameObject.SetActive(false);
}
return;
}
// Оружие в руках (огнестрел активен)
if (ammoIcon != null)
{
ammoIcon.color = Color.white;
}
// Обновление основного текста
if (ammoText != null)
{
ammoText.text = $"{current} / {reserve}";
if (reloading)
{
ammoText.color = reloadingColor;
}
else if (current == 0)
{
ammoText.color = noAmmoColor;
}
else if (current <= 2)
{
ammoText.color = lowAmmoColor;
}
else
{
ammoText.color = normalColor;
}
}
// Обновление статуса (перезарядка / подсказка / убрать в кобуру)
if (statusText != null)
{
if (reloading)
{
statusText.gameObject.SetActive(true);
statusText.text = "ПЕРЕЗАРЯДКА...";
statusText.color = reloadingColor;
}
else if (current == 0 && reserve > 0)
{
statusText.gameObject.SetActive(true);
statusText.text = "[R] ПЕРЕЗАРЯДИТЬ";
statusText.color = noAmmoColor;
}
else if (current == 0 && reserve == 0)
{
statusText.gameObject.SetActive(true);
statusText.text = "НЕТ ПАТРОНОВ!";
statusText.color = noAmmoColor;
}
else
{
statusText.text = "";
statusText.gameObject.SetActive(false);
}
}
}
}