88 lines
3.6 KiB
C#
88 lines
3.6 KiB
C#
using UnityEngine;
|
||
using TMPro; // Необходимо для работы с TextMeshPro
|
||
|
||
/// <summary>
|
||
/// Управляет отображением информации о квесте в UI.
|
||
/// Подписывается на события QuestManager и обновляет текстовые поля.
|
||
/// </summary>
|
||
public class QuestUIController : MonoBehaviour
|
||
{
|
||
[Header("UI Элементы")]
|
||
[Tooltip("Родительский объект всего UI квеста, чтобы его можно было скрывать и показывать.")]
|
||
[SerializeField] private GameObject questUIPanel;
|
||
|
||
[Tooltip("Текстовое поле для описания задания. Например: 'Соберите мусор'.")]
|
||
[SerializeField] private TextMeshProUGUI taskDescriptionText;
|
||
|
||
[Tooltip("Текстовое поле для отображения прогресса. Например: '1 / 3'.")]
|
||
[SerializeField] private TextMeshProUGUI taskProgressText;
|
||
|
||
|
||
void Start()
|
||
{
|
||
// Проверяем, найден ли QuestManager
|
||
if (QuestManager.Instance == null)
|
||
{
|
||
Debug.LogError("QuestUIController не может найти QuestManager.Instance! UI не будет работать.");
|
||
// Выключаем панель, чтобы не было видно пустого UI
|
||
if(questUIPanel != null) questUIPanel.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
// Подписываемся на события QuestManager
|
||
QuestManager.Instance.OnQuestStarted += HandleQuestStarted;
|
||
QuestManager.Instance.OnQuestCompleted += HandleQuestCompleted;
|
||
QuestManager.Instance.OnQuestStepChanged += UpdateQuestDescription;
|
||
|
||
// Изначально скрываем UI
|
||
if(questUIPanel != null) questUIPanel.SetActive(false);
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
// Отписываемся от событий, чтобы избежать ошибок
|
||
if (QuestManager.Instance != null)
|
||
{
|
||
QuestManager.Instance.OnQuestStarted -= HandleQuestStarted;
|
||
QuestManager.Instance.OnQuestCompleted -= HandleQuestCompleted;
|
||
QuestManager.Instance.OnQuestStepChanged -= UpdateQuestDescription;
|
||
}
|
||
}
|
||
|
||
private void HandleQuestStarted(IQuest quest)
|
||
{
|
||
if(questUIPanel != null) questUIPanel.SetActive(true);
|
||
UpdateQuestDescription(quest.Description);
|
||
}
|
||
|
||
private void HandleQuestCompleted(IQuest quest)
|
||
{
|
||
// Когда квест завершен, просто скрываем панель.
|
||
// Можно добавить анимацию или сообщение "Квест выполнен!"
|
||
if(questUIPanel != null) questUIPanel.SetActive(false);
|
||
}
|
||
|
||
private void UpdateQuestDescription(string description)
|
||
{
|
||
if (taskDescriptionText != null)
|
||
{
|
||
taskDescriptionText.text = description;
|
||
}
|
||
|
||
// Обновляем прогресс, если квест активен
|
||
if (QuestManager.Instance.currentQuest != null && !QuestManager.Instance.currentQuest.IsCompleted)
|
||
{
|
||
if (taskProgressText != null)
|
||
{
|
||
taskProgressText.text = $"{QuestManager.Instance.currentQuest.CurrentStep} / {QuestManager.Instance.currentQuest.TotalSteps}";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (taskProgressText != null)
|
||
{
|
||
taskProgressText.text = ""; // Очищаем прогресс, если квеста нет
|
||
}
|
||
}
|
||
}
|
||
} |