109 lines
3.8 KiB
C#
109 lines
3.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using PrimeTween;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Hallucinate.UI
|
|
{
|
|
public class HUDController : BaseUIController
|
|
{
|
|
private VisualElement _topLeft;
|
|
private VisualElement _bottomLeft;
|
|
|
|
private ProgressBar _healthBar;
|
|
private ProgressBar _staminaBar;
|
|
|
|
private float _lastActionTime;
|
|
private const float FADE_TIMEOUT = 5.0f;
|
|
private bool _isFaded = false;
|
|
|
|
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
|
{
|
|
base.Initialize(uxmlRoot, manager);
|
|
|
|
_topLeft = root.Q<VisualElement>("TopLeft");
|
|
_bottomLeft = root.Q<VisualElement>("BottomLeft");
|
|
_healthBar = root.Q<ProgressBar>("HealthBar");
|
|
_staminaBar = root.Q<ProgressBar>("StaminaBar");
|
|
|
|
if (LocalizationManager.Instance != null)
|
|
{
|
|
LocalizationManager.Instance.OnLanguageChanged += ApplyLocalization;
|
|
ApplyLocalization();
|
|
}
|
|
|
|
_lastActionTime = Time.time;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (LocalizationManager.Instance != null)
|
|
{
|
|
LocalizationManager.Instance.OnLanguageChanged -= ApplyLocalization;
|
|
}
|
|
}
|
|
|
|
private void ApplyLocalization()
|
|
{
|
|
if (LocalizationManager.Instance == null) return;
|
|
|
|
root.Query<Label>().ForEach(l => {
|
|
if (l.text == "HEALTH") l.text = LocalizationManager.Instance.GetLocalizedString("HUD_HEALTH");
|
|
if (l.text == "STAMINA") l.text = LocalizationManager.Instance.GetLocalizedString("HUD_STAMINA");
|
|
if (l.text == "MINIMAP") l.text = LocalizationManager.Instance.GetLocalizedString("HUD_MINIMAP");
|
|
});
|
|
}
|
|
|
|
public void UpdateHUD(float health, float stamina)
|
|
{
|
|
_healthBar.value = health;
|
|
_staminaBar.value = stamina;
|
|
WakeUpHUD();
|
|
}
|
|
|
|
public void UpdateStats(int ping, int fps)
|
|
{
|
|
string pingPrefix = LocalizationManager.Instance != null ? LocalizationManager.Instance.GetLocalizedString("HUD_PING_PREFIX") : "PING: ";
|
|
string fpsPrefix = LocalizationManager.Instance != null ? LocalizationManager.Instance.GetLocalizedString("HUD_FPS_PREFIX") : "FPS: ";
|
|
|
|
root.Q<Label>("PingLabel").text = $"{pingPrefix}{ping}ms";
|
|
root.Q<Label>("FPSLabel").text = $"{fpsPrefix}{fps}";
|
|
}
|
|
|
|
public void WakeUpHUD()
|
|
{
|
|
_lastActionTime = Time.time;
|
|
if (_isFaded)
|
|
{
|
|
_isFaded = false;
|
|
Tween.Custom(_topLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _topLeft.style.opacity = val);
|
|
Tween.Custom(_bottomLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _bottomLeft.style.opacity = val);
|
|
}
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
|
|
{
|
|
_isFaded = true;
|
|
Tween.Custom(_topLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _topLeft.style.opacity = val);
|
|
Tween.Custom(_bottomLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _bottomLeft.style.opacity = val);
|
|
}
|
|
}
|
|
|
|
public override Task PlayTransitionIn()
|
|
{
|
|
Show();
|
|
_topLeft.style.opacity = 1;
|
|
_bottomLeft.style.opacity = 1;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public override Task PlayTransitionOut()
|
|
{
|
|
Hide();
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|