87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using Code.Gameplay.Character.Core;
|
|
using Code.Gameplay.Character.Inventory;
|
|
using Code.Gameplay.Character.Stats;
|
|
|
|
namespace Code.UI
|
|
{
|
|
public class GameUI : MonoBehaviour
|
|
{
|
|
[SerializeField] private TMP_Text weaponNameText;
|
|
[SerializeField] private TMP_Text healthText;
|
|
[SerializeField] private TMP_Text ammoText;
|
|
|
|
[SerializeField] private string weaponNameFormat = "CURRENT WEAPON: {0}";
|
|
[SerializeField] private string healthFormat = "HP: {0}/{1}";
|
|
[SerializeField] private string ammoFormat = "AMMO: {0}/{1}";
|
|
|
|
private CharacterProvider _player;
|
|
private HealthSystem _playerHealth;
|
|
private WeaponInventory _weaponInventory;
|
|
|
|
private void Start()
|
|
{
|
|
_player = FindFirstObjectByType<CharacterProvider>();
|
|
|
|
if (_player != null)
|
|
{
|
|
_playerHealth = _player.GetComponent<HealthSystem>();
|
|
_weaponInventory = _player.GetComponent<WeaponInventory>();
|
|
}
|
|
|
|
if (_playerHealth != null)
|
|
{
|
|
_playerHealth.OnHealthChanged += UpdateHealth;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateWeaponName();
|
|
UpdateHealth();
|
|
UpdateAmmo();
|
|
}
|
|
|
|
private void UpdateWeaponName()
|
|
{
|
|
if (!weaponNameText || !_weaponInventory?.CurrentWeapon) return;
|
|
|
|
var weaponName = _weaponInventory.CurrentWeapon.WeaponName;
|
|
weaponNameText.text = string.Format(weaponNameFormat, weaponName.ToUpper());
|
|
}
|
|
|
|
private void UpdateHealth()
|
|
{
|
|
if (!healthText || !_playerHealth) return;
|
|
|
|
healthText.text = string.Format(healthFormat,
|
|
_playerHealth.CurrentHealth,
|
|
_playerHealth.MaxHealth);
|
|
}
|
|
|
|
private void UpdateHealth(float current, float max)
|
|
{
|
|
if (!healthText) return;
|
|
healthText.text = string.Format(healthFormat, current, max);
|
|
}
|
|
|
|
private void UpdateAmmo()
|
|
{
|
|
if (!ammoText || !_weaponInventory?.CurrentWeapon) return;
|
|
|
|
var weapon = _weaponInventory.CurrentWeapon;
|
|
ammoText.text = string.Format(ammoFormat,
|
|
weapon.CurrentAmmo,
|
|
weapon.ReserveAmmo);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_playerHealth != null)
|
|
{
|
|
_playerHealth.OnHealthChanged -= UpdateHealth;
|
|
}
|
|
}
|
|
}
|
|
} |