103 lines
2.4 KiB
C#
103 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 战斗管理器:监听双方召唤师死亡,触发胜负判定并显示结果 UI。
|
|
/// </summary>
|
|
public class BattleManager : MonoBehaviour
|
|
{
|
|
[Header("召唤师引用")]
|
|
public GameObject playerSummoner;
|
|
public GameObject enemySummoner;
|
|
|
|
[Header("胜负 UI")]
|
|
public GameObject victoryPanel;
|
|
public GameObject defeatPanel;
|
|
|
|
private WorldHealthBar playerHealthBar;
|
|
private WorldHealthBar enemyHealthBar;
|
|
private bool battleEnded = false;
|
|
|
|
private void Start()
|
|
{
|
|
// 获取双方召唤师的血条组件
|
|
if (playerSummoner != null)
|
|
{
|
|
playerHealthBar = playerSummoner.GetComponent<WorldHealthBar>();
|
|
}
|
|
|
|
if (enemySummoner != null)
|
|
{
|
|
enemyHealthBar = enemySummoner.GetComponent<WorldHealthBar>();
|
|
}
|
|
|
|
// 隐藏胜负 UI
|
|
if (victoryPanel != null) victoryPanel.SetActive(false);
|
|
if (defeatPanel != null) defeatPanel.SetActive(false);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (battleEnded) return;
|
|
|
|
// 检测己方召唤师死亡
|
|
if (playerHealthBar != null && playerHealthBar.IsDead)
|
|
{
|
|
OnBattleEnd(false);
|
|
return;
|
|
}
|
|
|
|
// 检测敌方召唤师死亡
|
|
if (enemyHealthBar != null && enemyHealthBar.IsDead)
|
|
{
|
|
OnBattleEnd(true);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void OnBattleEnd(bool victory)
|
|
{
|
|
battleEnded = true;
|
|
|
|
// 暂停游戏
|
|
Time.timeScale = 0f;
|
|
|
|
// 显示对应 UI
|
|
if (victory)
|
|
{
|
|
Debug.Log("胜利!");
|
|
if (victoryPanel != null)
|
|
{
|
|
victoryPanel.SetActive(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("失败!");
|
|
if (defeatPanel != null)
|
|
{
|
|
defeatPanel.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 重新开始按钮回调
|
|
public void OnRestartButtonClicked()
|
|
{
|
|
Time.timeScale = 1f;
|
|
UnityEngine.SceneManagement.SceneManager.LoadScene(
|
|
UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
|
|
}
|
|
|
|
// 退出按钮回调
|
|
public void OnQuitButtonClicked()
|
|
{
|
|
Time.timeScale = 1f;
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#else
|
|
Application.Quit();
|
|
#endif
|
|
}
|
|
}
|