2025-10-15 21:31:13 +08:00
|
|
|
using System;
|
|
|
|
using Core;
|
|
|
|
using Gameplay.Player;
|
2025-10-19 19:38:52 +08:00
|
|
|
using TMPro;
|
2025-10-15 21:31:13 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
namespace UI
|
|
|
|
{
|
|
|
|
public class PlayerHpViewer : UIBase
|
|
|
|
{
|
|
|
|
private PlayerController _playerController;
|
2025-10-19 19:38:52 +08:00
|
|
|
public TMP_Text hpText;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
2025-10-18 08:55:38 +08:00
|
|
|
private void Awake()
|
2025-10-15 21:31:13 +08:00
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
|
2025-10-15 21:31:13 +08:00
|
|
|
}
|
|
|
|
|
2025-10-18 15:41:43 +08:00
|
|
|
private void OnGet(PlayerController playerController)
|
2025-10-15 21:31:13 +08:00
|
|
|
{
|
2025-10-18 15:41:43 +08:00
|
|
|
_playerController = playerController;
|
|
|
|
_playerController.OnHealthChanged += UpdateHpText;
|
2025-10-19 19:38:52 +08:00
|
|
|
UpdateHpText(_playerController.CurrentHealth);
|
2025-10-15 21:31:13 +08:00
|
|
|
}
|
|
|
|
|
2025-10-18 15:41:43 +08:00
|
|
|
private void UpdateHpText(int currentHealth)
|
2025-10-15 21:31:13 +08:00
|
|
|
{
|
2025-10-19 19:38:52 +08:00
|
|
|
hpText.text = "HP: " + currentHealth.ToString();
|
2025-10-18 15:41:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
_playerController.OnHealthChanged -= UpdateHpText;
|
2025-10-15 21:31:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|