Update
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using OnlyScove.Scripts;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@@ -12,9 +14,16 @@ namespace UI
|
||||
private VisualElement _healthFill;
|
||||
private VisualElement _staminaFill;
|
||||
private Label _healthText;
|
||||
private Label _noiseLabel;
|
||||
private Label _interactionLabel;
|
||||
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()
|
||||
{
|
||||
@@ -26,18 +35,106 @@ namespace UI
|
||||
_healthFill = root.Q<VisualElement>("health-fill");
|
||||
_staminaFill = root.Q<VisualElement>("stamina-fill");
|
||||
_healthText = root.Q<Label>("health-text");
|
||||
_noiseLabel = root.Q<Label>("noise-label");
|
||||
_interactionLabel = root.Q<Label>("interaction-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;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Kết nối với Local Player
|
||||
if (PlayerStateMachine.Local != null)
|
||||
{
|
||||
SubscribeToPlayer(PlayerStateMachine.Local);
|
||||
}
|
||||
|
||||
HandleAutoHide();
|
||||
HandleInventoryInput();
|
||||
}
|
||||
|
||||
private void HandleAutoHide()
|
||||
{
|
||||
bool inputDetected = false;
|
||||
|
||||
// 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)
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else if (Time.time - _lastInputTime > autoHideDelay)
|
||||
{
|
||||
SetHUDVisibility(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetHUDVisibility(bool visible)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
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;
|
||||
@@ -46,43 +143,38 @@ namespace UI
|
||||
{
|
||||
if (_currentPlayer == player) return;
|
||||
|
||||
// Hủy đăng ký player cũ nếu có
|
||||
if (_currentPlayer != null)
|
||||
{
|
||||
_currentPlayer.OnHealthChanged -= UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged -= UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged -= UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
|
||||
}
|
||||
|
||||
_currentPlayer = player;
|
||||
|
||||
// Đăng ký player mới
|
||||
_currentPlayer.OnHealthChanged += UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged += UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged += UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
|
||||
|
||||
// Cập nhật giá trị ban đầu
|
||||
UpdateHealth(_currentPlayer.Health);
|
||||
UpdateStamina(_currentPlayer.Stamina);
|
||||
UpdateNoise(_currentPlayer.NoiseLevel);
|
||||
}
|
||||
|
||||
private void UpdateHealth(float health)
|
||||
{
|
||||
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
|
||||
if (_healthText != null) _healthText.text = $"HEALTH: {Mathf.RoundToInt(health)}/100";
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
|
||||
private void UpdateStamina(float stamina)
|
||||
{
|
||||
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
|
||||
}
|
||||
|
||||
private void UpdateNoise(float noise)
|
||||
{
|
||||
if (_noiseLabel != null) _noiseLabel.text = $"NOISE: {Mathf.RoundToInt(noise)}%";
|
||||
if (stamina < 99f) // Only wake up HUD if stamina is being used
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateInteraction(IInteractable interactable)
|
||||
@@ -93,6 +185,8 @@ namespace UI
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.Flex;
|
||||
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user