173 lines
5.6 KiB
C#
173 lines
5.6 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
namespace OTGIntegrated.Quests
|
||
|
|
{
|
||
|
|
public sealed class QuestLogUI : MonoBehaviour
|
||
|
|
{
|
||
|
|
public GameObject panelRoot;
|
||
|
|
public Transform activeListRoot;
|
||
|
|
public Transform completedListRoot;
|
||
|
|
public Text titleText;
|
||
|
|
public Text detailText;
|
||
|
|
public Button closeButton;
|
||
|
|
public Font uiFont;
|
||
|
|
|
||
|
|
private QuestManager questManager;
|
||
|
|
private QuestState selectedState;
|
||
|
|
private readonly List<GameObject> spawnedButtons = new List<GameObject>();
|
||
|
|
|
||
|
|
public void Initialize(QuestManager manager)
|
||
|
|
{
|
||
|
|
questManager = manager;
|
||
|
|
if (questManager != null)
|
||
|
|
{
|
||
|
|
questManager.OnQuestsChanged -= Refresh;
|
||
|
|
questManager.OnQuestsChanged += Refresh;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (closeButton != null)
|
||
|
|
{
|
||
|
|
closeButton.onClick.RemoveAllListeners();
|
||
|
|
closeButton.onClick.AddListener(() => FindObjectOfType<OTGIntegrated.UI.UIManager>()?.CloseCurrentWindow());
|
||
|
|
}
|
||
|
|
|
||
|
|
Refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable()
|
||
|
|
{
|
||
|
|
Refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnDestroy()
|
||
|
|
{
|
||
|
|
if (questManager != null)
|
||
|
|
{
|
||
|
|
questManager.OnQuestsChanged -= Refresh;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Refresh()
|
||
|
|
{
|
||
|
|
ClearButtons();
|
||
|
|
|
||
|
|
if (titleText != null)
|
||
|
|
{
|
||
|
|
titleText.text = "Журнал квестов";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (questManager == null)
|
||
|
|
{
|
||
|
|
SetDetails(null);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (selectedState == null && questManager.ActiveQuests.Count > 0)
|
||
|
|
{
|
||
|
|
selectedState = questManager.ActiveQuests[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
CreateButtons(activeListRoot, questManager.ActiveQuests);
|
||
|
|
CreateButtons(completedListRoot, questManager.CompletedQuests);
|
||
|
|
SetDetails(selectedState);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void CreateButtons(Transform parent, IReadOnlyList<QuestState> states)
|
||
|
|
{
|
||
|
|
if (parent == null || states == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < states.Count; i++)
|
||
|
|
{
|
||
|
|
QuestState state = states[i];
|
||
|
|
if (state == null || state.questData == null)
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
GameObject root = new GameObject(state.questData.displayName, typeof(RectTransform), typeof(Image), typeof(Button));
|
||
|
|
root.transform.SetParent(parent, false);
|
||
|
|
spawnedButtons.Add(root);
|
||
|
|
root.GetComponent<RectTransform>().sizeDelta = new Vector2(320f, 44f);
|
||
|
|
|
||
|
|
Image image = root.GetComponent<Image>();
|
||
|
|
image.color = GetStatusColor(state.status);
|
||
|
|
|
||
|
|
Text label = CreateText(root.transform, "Text", state.questData.displayName, 16, TextAnchor.MiddleLeft);
|
||
|
|
label.rectTransform.anchorMin = Vector2.zero;
|
||
|
|
label.rectTransform.anchorMax = Vector2.one;
|
||
|
|
label.rectTransform.offsetMin = new Vector2(12f, 0f);
|
||
|
|
label.rectTransform.offsetMax = new Vector2(-8f, 0f);
|
||
|
|
|
||
|
|
Button button = root.GetComponent<Button>();
|
||
|
|
button.onClick.AddListener(() =>
|
||
|
|
{
|
||
|
|
selectedState = state;
|
||
|
|
SetDetails(selectedState);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SetDetails(QuestState state)
|
||
|
|
{
|
||
|
|
if (detailText == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (state == null || state.questData == null)
|
||
|
|
{
|
||
|
|
detailText.text = "Нет выбранного квеста.";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
QuestData quest = state.questData;
|
||
|
|
detailText.text =
|
||
|
|
$"{quest.displayName}\n\n{quest.description}\n\nСтатус: {state.status}\n\n{questManager.GetStateProgressText(state)}\n\nНаграды:\nОпыт: {quest.rewardExperience}\nОчки талантов: {quest.rewardTalentPoints}";
|
||
|
|
}
|
||
|
|
|
||
|
|
private Color GetStatusColor(QuestStatus status)
|
||
|
|
{
|
||
|
|
switch (status)
|
||
|
|
{
|
||
|
|
case QuestStatus.ReadyToComplete:
|
||
|
|
return new Color(0.16f, 0.42f, 0.22f, 0.95f);
|
||
|
|
case QuestStatus.Completed:
|
||
|
|
return new Color(0.16f, 0.2f, 0.34f, 0.82f);
|
||
|
|
default:
|
||
|
|
return new Color(0.46f, 0.34f, 0.12f, 0.92f);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private Text CreateText(Transform parent, string name, string value, int fontSize, TextAnchor anchor)
|
||
|
|
{
|
||
|
|
GameObject textObject = new GameObject(name, typeof(RectTransform), typeof(Text));
|
||
|
|
textObject.transform.SetParent(parent, false);
|
||
|
|
Text text = textObject.GetComponent<Text>();
|
||
|
|
text.font = uiFont != null ? uiFont : Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||
|
|
text.fontSize = fontSize;
|
||
|
|
text.alignment = anchor;
|
||
|
|
text.color = new Color(0.94f, 0.93f, 0.88f, 1f);
|
||
|
|
text.text = value;
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ClearButtons()
|
||
|
|
{
|
||
|
|
for (int i = 0; i < spawnedButtons.Count; i++)
|
||
|
|
{
|
||
|
|
if (spawnedButtons[i] != null)
|
||
|
|
{
|
||
|
|
Destroy(spawnedButtons[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
spawnedButtons.Clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|