This commit is contained in:
2026-04-28 00:07:42 +07:00
parent e051667037
commit 252489f48a
570 changed files with 2423 additions and 10875 deletions

View File

@@ -1,197 +1,80 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using PrimeTween;
using System.Threading.Tasks;
namespace UI
namespace Hallucinate.UI
{
public class HUDController : MonoBehaviour
public class HUDController : BaseUIController
{
[Header("UI Document")]
public UIDocument hudDocument;
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;
private VisualElement _healthFill;
private VisualElement _staminaFill;
private Label _healthText;
private VisualElement _interactionPrompt;
private Label _interactionLabel;
private VisualElement _statsArea;
private VisualElement _inventoryArea;
private VisualElement _infoArea;
private float _lastInputTime;
private bool _isHUDVisible = true;
public float autoHideDelay = 5f;
private void OnEnable()
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
if (hudDocument == null)
hudDocument = GetComponent<UIDocument>();
base.Initialize(uxmlRoot, manager);
var root = hudDocument.rootVisualElement;
_topLeft = root.Q<VisualElement>("TopLeft");
_bottomLeft = root.Q<VisualElement>("BottomLeft");
_healthBar = root.Q<ProgressBar>("HealthBar");
_staminaBar = root.Q<ProgressBar>("StaminaBar");
_healthFill = root.Q<VisualElement>("health-fill");
_staminaFill = root.Q<VisualElement>("stamina-fill");
_healthText = root.Q<Label>("health-text");
_interactionPrompt = root.Q<VisualElement>("interaction-prompt");
_interactionLabel = root.Q<Label>("interaction-text");
_statsArea = root.Q<VisualElement>("hud-stats");
_inventoryArea = root.Q<VisualElement>("hud-inventory");
_infoArea = root.Q<VisualElement>("hud-info");
_lastInputTime = Time.time;
_lastActionTime = Time.time;
}
private void Update()
public void UpdateHUD(float health, float stamina)
{
if (PlayerStateMachine.Local != null)
{
SubscribeToPlayer(PlayerStateMachine.Local);
}
HandleAutoHide();
HandleInventoryInput();
_healthBar.value = health;
_staminaBar.value = stamina;
WakeUpHUD();
}
private void HandleAutoHide()
public void UpdateStats(int ping, int fps)
{
bool inputDetected = false;
root.Q<Label>("PingLabel").text = $"PING: {ping}ms";
root.Q<Label>("FPSLabel").text = $"FPS: {fps}";
}
// Check for mouse movement
if (Mouse.current != null && Mouse.current.delta.ReadValue().sqrMagnitude > 0.01f)
inputDetected = true;
// Check for any key press (including mouse buttons)
if (!inputDetected && Keyboard.current != null && Keyboard.current.anyKey.isPressed)
inputDetected = true;
if (!inputDetected && Mouse.current != null && (Mouse.current.leftButton.isPressed || Mouse.current.rightButton.isPressed))
inputDetected = true;
if (inputDetected)
public void WakeUpHUD()
{
_lastActionTime = Time.time;
if (_isFaded)
{
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else if (Time.time - _lastInputTime > autoHideDelay)
{
SetHUDVisibility(false);
_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);
}
}
private void SetHUDVisibility(bool visible)
public void Update()
{
if (_isHUDVisible == visible) return;
_isHUDVisible = visible;
float targetOpacity = visible ? 1f : 0.2f;
_statsArea.style.opacity = targetOpacity;
_inventoryArea.style.opacity = targetOpacity;
_infoArea.style.opacity = targetOpacity;
_statsArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_statsArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
_inventoryArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_inventoryArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
_infoArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_infoArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
}
private void HandleInventoryInput()
{
if (Keyboard.current == null) return;
if (Keyboard.current.digit1Key.wasPressedThisFrame) SelectSlot(1);
if (Keyboard.current.digit2Key.wasPressedThisFrame) SelectSlot(2);
if (Keyboard.current.digit3Key.wasPressedThisFrame) SelectSlot(3);
}
private void SelectSlot(int index)
{
// Mock logic: Highlight the selected slot
var root = hudDocument.rootVisualElement;
for (int i = 1; i <= 3; i++)
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
{
var slot = root.Q<VisualElement>($"slot-{i}");
if (slot != null)
{
float width = (i == index) ? 2f : 1f;
Color color = (i == index) ? Color.white : new Color(0.5f, 0.5f, 0.5f);
slot.style.borderTopWidth = width;
slot.style.borderBottomWidth = width;
slot.style.borderLeftWidth = width;
slot.style.borderRightWidth = width;
slot.style.borderTopColor = color;
slot.style.borderBottomColor = color;
slot.style.borderLeftColor = color;
slot.style.borderRightColor = color;
}
}
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
private PlayerStateMachine _currentPlayer;
private void SubscribeToPlayer(PlayerStateMachine player)
{
if (_currentPlayer == player) return;
if (_currentPlayer != null)
{
_currentPlayer.OnHealthChanged -= UpdateHealth;
_currentPlayer.OnStaminaChanged -= UpdateStamina;
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
}
_currentPlayer = player;
_currentPlayer.OnHealthChanged += UpdateHealth;
_currentPlayer.OnStaminaChanged += UpdateStamina;
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
UpdateHealth(_currentPlayer.Health);
UpdateStamina(_currentPlayer.Stamina);
}
private void UpdateHealth(float health)
{
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
private void UpdateStamina(float stamina)
{
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
if (stamina < 99f) // Only wake up HUD if stamina is being used
{
_lastInputTime = Time.time;
SetHUDVisibility(true);
_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);
}
}
private void UpdateInteraction(IInteractable interactable)
public override Task PlayTransitionIn()
{
if (_interactionPrompt == null) return;
Show();
_topLeft.style.opacity = 1;
_bottomLeft.style.opacity = 1;
return Task.CompletedTask;
}
if (interactable != null)
{
_interactionPrompt.style.display = DisplayStyle.Flex;
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else
{
_interactionPrompt.style.display = DisplayStyle.None;
}
public override Task PlayTransitionOut()
{
Hide();
return Task.CompletedTask;
}
}
}