Files
BABA_YAGA/Assets/Scripts/UI/HUDController.cs
2026-04-28 00:07:42 +07:00

81 lines
2.5 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");
_lastActionTime = Time.time;
}
public void UpdateHUD(float health, float stamina)
{
_healthBar.value = health;
_staminaBar.value = stamina;
WakeUpHUD();
}
public void UpdateStats(int ping, int fps)
{
root.Q<Label>("PingLabel").text = $"PING: {ping}ms";
root.Q<Label>("FPSLabel").text = $"FPS: {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 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;
}
}
}