131 lines
4.3 KiB
C#
131 lines
4.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using IndianOceanAssets.Engine2_5D;
|
|
using GameFramework;
|
|
|
|
namespace GameFramework
|
|
{
|
|
/// <summary>
|
|
/// 游戏内 HUD —— 显示血量、灵灯数量/CD、分数、暂停按钮。
|
|
/// 挂在 Gameplay 场景的 HUD Canvas 上。
|
|
/// </summary>
|
|
public class GameHUD : MonoBehaviour
|
|
{
|
|
[Header("血量")]
|
|
[SerializeField] private Slider healthSlider;
|
|
[SerializeField] private Text healthText;
|
|
|
|
[Header("灵灯")]
|
|
[SerializeField] private Text lanternCountText;
|
|
[SerializeField] private Text lanternCDText;
|
|
[SerializeField] private Image lanternCDFill; // 可选:CD 圆形填充
|
|
|
|
[Header("分数")]
|
|
[SerializeField] private Text scoreText;
|
|
|
|
[Header("暂停")]
|
|
[SerializeField] private Button pauseButton;
|
|
|
|
private HealthSystem _playerHealth;
|
|
private SpiritLanternSystem _lanternSystem;
|
|
private int _maxHealth;
|
|
|
|
void Start()
|
|
{
|
|
// 查找玩家
|
|
var player = GameObject.FindWithTag("Player");
|
|
if (player != null)
|
|
{
|
|
_playerHealth = player.GetComponent<HealthSystem>();
|
|
_lanternSystem = player.GetComponent<SpiritLanternSystem>();
|
|
|
|
// 获取 maxHealth
|
|
if (_playerHealth != null)
|
|
{
|
|
var field = typeof(HealthSystem).GetField("maxHealth",
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
if (field != null)
|
|
_maxHealth = (int)field.GetValue(_playerHealth);
|
|
else
|
|
_maxHealth = 100;
|
|
}
|
|
}
|
|
|
|
// 绑定暂停按钮
|
|
if (pauseButton != null)
|
|
pauseButton.onClick.AddListener(OnPauseClick);
|
|
|
|
// 订阅分数事件
|
|
ScoreManager.onScoreChanged += UpdateScore;
|
|
|
|
// 初始更新
|
|
UpdateScore(ScoreManager.Instance != null ? ScoreManager.Instance.CurrentScore : 0);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
ScoreManager.onScoreChanged -= UpdateScore;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
UpdateHealth();
|
|
UpdateLantern();
|
|
}
|
|
|
|
private void UpdateHealth()
|
|
{
|
|
if (_playerHealth == null) return;
|
|
|
|
// 获取当前血量
|
|
var field = typeof(HealthSystem).GetField("health",
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
int currentHealth = field != null ? (int)field.GetValue(_playerHealth) : _maxHealth;
|
|
|
|
if (healthSlider != null)
|
|
healthSlider.value = (float)currentHealth / _maxHealth;
|
|
|
|
if (healthText != null)
|
|
healthText.text = $"{currentHealth}/{_maxHealth}";
|
|
}
|
|
|
|
private void UpdateLantern()
|
|
{
|
|
if (_lanternSystem == null) return;
|
|
|
|
if (lanternCountText != null)
|
|
lanternCountText.text = $"灵灯: {_lanternSystem.RemainingLanterns}";
|
|
|
|
if (lanternCDText != null)
|
|
{
|
|
if (_lanternSystem.IsCooldownReady)
|
|
lanternCDText.text = "就绪";
|
|
else
|
|
lanternCDText.text = $"{_lanternSystem.CooldownRemaining:F1}s";
|
|
}
|
|
|
|
if (lanternCDFill != null)
|
|
{
|
|
// CD 填充:就绪时满,冷却中递减
|
|
var cdField = typeof(SpiritLanternSystem).GetField("cooldown",
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
float maxCD = cdField != null ? (float)cdField.GetValue(_lanternSystem) : 3f;
|
|
lanternCDFill.fillAmount = _lanternSystem.IsCooldownReady ? 1f :
|
|
1f - (_lanternSystem.CooldownRemaining / maxCD);
|
|
}
|
|
}
|
|
|
|
private void UpdateScore(int score)
|
|
{
|
|
if (scoreText != null)
|
|
scoreText.text = $"分数: {score}";
|
|
}
|
|
|
|
private void OnPauseClick()
|
|
{
|
|
if (TimeController.Instance != null)
|
|
TimeController.Instance.Pause();
|
|
}
|
|
}
|
|
}
|