105 lines
3.5 KiB
C#
105 lines
3.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using OnlyScove.Scripts;
|
|
|
|
namespace UI
|
|
{
|
|
public class HUDController : MonoBehaviour
|
|
{
|
|
[Header("UI Document")]
|
|
public UIDocument hudDocument;
|
|
|
|
private VisualElement _healthFill;
|
|
private VisualElement _staminaFill;
|
|
private Label _healthText;
|
|
private Label _noiseLabel;
|
|
private Label _interactionLabel;
|
|
private VisualElement _interactionPrompt;
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (hudDocument == null)
|
|
hudDocument = GetComponent<UIDocument>();
|
|
|
|
var root = hudDocument.rootVisualElement;
|
|
|
|
// Tìm các thành phần UI theo Name (Bạn cần đặt tên này trong UXML)
|
|
_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");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// Kết nối với Local Player
|
|
if (PlayerStateMachine.Local != null)
|
|
{
|
|
SubscribeToPlayer(PlayerStateMachine.Local);
|
|
}
|
|
}
|
|
|
|
private PlayerStateMachine _currentPlayer;
|
|
|
|
private void SubscribeToPlayer(PlayerStateMachine player)
|
|
{
|
|
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";
|
|
}
|
|
|
|
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)}%";
|
|
}
|
|
|
|
private void UpdateInteraction(IInteractable interactable)
|
|
{
|
|
if (_interactionPrompt == null) return;
|
|
|
|
if (interactable != null)
|
|
{
|
|
_interactionPrompt.style.display = DisplayStyle.Flex;
|
|
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
|
|
}
|
|
else
|
|
{
|
|
_interactionPrompt.style.display = DisplayStyle.None;
|
|
}
|
|
}
|
|
}
|
|
}
|