Files
BABA_YAGA/Assets/Scripts/UI/HUDController.cs

109 lines
3.8 KiB
C#
Raw Permalink Normal View History

2026-04-23 23:09:54 +07:00
using UnityEngine;
using UnityEngine.UIElements;
2026-04-28 00:07:42 +07:00
using PrimeTween;
using System.Threading.Tasks;
2026-04-23 23:09:54 +07:00
2026-04-28 00:07:42 +07:00
namespace Hallucinate.UI
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
public class HUDController : BaseUIController
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
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)
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
base.Initialize(uxmlRoot, manager);
2026-04-26 04:39:59 +07:00
2026-04-28 00:07:42 +07:00
_topLeft = root.Q<VisualElement>("TopLeft");
_bottomLeft = root.Q<VisualElement>("BottomLeft");
_healthBar = root.Q<ProgressBar>("HealthBar");
_staminaBar = root.Q<ProgressBar>("StaminaBar");
2026-04-26 04:39:59 +07:00
2026-05-01 17:57:07 +07:00
if (LocalizationManager.Instance != null)
{
LocalizationManager.Instance.OnLanguageChanged += ApplyLocalization;
ApplyLocalization();
}
2026-04-28 00:07:42 +07:00
_lastActionTime = Time.time;
2026-04-26 04:39:59 +07:00
}
2026-05-01 17:57:07 +07:00
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");
});
}
2026-04-28 00:07:42 +07:00
public void UpdateHUD(float health, float stamina)
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
_healthBar.value = health;
_staminaBar.value = stamina;
WakeUpHUD();
2026-04-26 04:39:59 +07:00
}
2026-04-28 00:07:42 +07:00
public void UpdateStats(int ping, int fps)
2026-04-26 04:39:59 +07:00
{
2026-05-01 17:57:07 +07:00
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}";
2026-04-26 04:39:59 +07:00
}
2026-04-28 00:07:42 +07:00
public void WakeUpHUD()
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
_lastActionTime = Time.time;
if (_isFaded)
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
_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);
2026-04-26 04:39:59 +07:00
}
2026-04-23 23:09:54 +07:00
}
2026-04-29 13:10:00 +07:00
public override void Update()
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
_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);
2026-04-23 23:09:54 +07:00
}
}
2026-04-28 00:07:42 +07:00
public override Task PlayTransitionIn()
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
Show();
_topLeft.style.opacity = 1;
_bottomLeft.style.opacity = 1;
return Task.CompletedTask;
2026-04-23 23:09:54 +07:00
}
2026-04-28 00:07:42 +07:00
public override Task PlayTransitionOut()
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
Hide();
return Task.CompletedTask;
2026-04-23 23:09:54 +07:00
}
}
}