2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-03-31 23:31:00 +08:00
|
|
|
using System.Runtime.InteropServices;
|
2023-06-05 19:57:17 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using BITKit;
|
|
|
|
using BITKit.SubSystems.Quest;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BITKit.UX
|
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
public class UXQuest : MonoBehaviour
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
[Header(Constant.Header.Settings)]
|
|
|
|
public bool clearAfterCompleted;
|
2024-03-31 23:31:00 +08:00
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.Reference)]
|
2024-03-31 23:31:00 +08:00
|
|
|
[SerializeReference, SubclassSelector] public IReference completeClassName;
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.Prefabs)]
|
2024-03-31 23:31:00 +08:00
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
public VisualTreeAsset visualTreeAsset;
|
2024-03-31 23:31:00 +08:00
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.InternalVariables)]
|
2024-03-31 23:31:00 +08:00
|
|
|
private readonly Dictionary<string, UXContainer> dictionary = new();
|
|
|
|
|
|
|
|
[UXBindPath("quest-container")]
|
|
|
|
private VisualElement _container;
|
|
|
|
private void Start()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
UXUtils.Inject(this);
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
QuestSystem.OnQuestCreated += OnQuestCreated;
|
|
|
|
QuestSystem.OnQuestCompleted += OnQuestCompleted;
|
2024-03-31 23:31:00 +08:00
|
|
|
QuestSystem.OnQuestCanceled += OnQuestCanceled;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
_container.Clear();
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
private void OnDestroy()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
QuestSystem.OnQuestCreated -= OnQuestCreated;
|
|
|
|
QuestSystem.OnQuestCompleted -= OnQuestCompleted;
|
2024-03-31 23:31:00 +08:00
|
|
|
QuestSystem.OnQuestCanceled -= OnQuestCanceled;
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
private void OnQuestCreated(QuestSystem.Info quest)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
BIT4Log.Log<UXQuest>($"已创建任务:{quest.Name}");
|
2023-06-05 19:57:17 +08:00
|
|
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
if (dictionary.TryGetValue(quest.Name, out var container))
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
container.visualElement.RemoveFromClassList(completeClassName.Value);
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
container = new(visualTreeAsset.CloneTree());
|
2024-03-31 23:31:00 +08:00
|
|
|
dictionary.Add(quest.Name, container);
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
container.titleLabel.text = quest.Name;
|
|
|
|
container.descriptionLabel.text = quest.Description;
|
2023-06-05 19:57:17 +08:00
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
_container.Add(container);
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
private void OnQuestCanceled(QuestSystem.Info quest)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
BIT4Log.Log<UXQuest>($"已取消任务:{quest.Name}");
|
|
|
|
if (!dictionary.TryGetValue(quest.Name, out var container)) return;
|
|
|
|
_container.Remove(container);
|
|
|
|
dictionary.Remove(quest.Name);
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
2024-03-31 23:31:00 +08:00
|
|
|
private async void OnQuestCompleted(QuestSystem.Info quest)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
BIT4Log.Log<UXQuest>($"已完成任务:{quest.Name}");
|
2023-06-05 19:57:17 +08:00
|
|
|
|
2024-03-31 23:31:00 +08:00
|
|
|
if (!dictionary.TryGetValue(quest.Name, out var container)) return;
|
|
|
|
container.visualElement.AddToClassList(completeClassName.Value);
|
|
|
|
container.titleLabel.text = $"已完成:{quest.Name}";
|
|
|
|
container.visualElement.tabIndex = 256;
|
|
|
|
if (!clearAfterCompleted) return;
|
|
|
|
dictionary.Remove(quest.Name);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await Task.Delay(8096, destroyCancellationToken);
|
|
|
|
}
|
|
|
|
catch (System.OperationCanceledException) { }
|
|
|
|
catch (System.Exception)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2024-03-31 23:31:00 +08:00
|
|
|
_container.Remove(container);
|
|
|
|
throw;
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|