1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.SubSystems.Quest;
|
||||
@@ -7,84 +8,85 @@ using UnityEngine.UIElements;
|
||||
using System.Threading.Tasks;
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class UXQuest : UXElement<VisualElement>
|
||||
public class UXQuest : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public bool clearAfterCompleted;
|
||||
|
||||
[Header(Constant.Header.Reference)]
|
||||
[SerializeReference, SubclassSelector] public References completeClassName;
|
||||
[SerializeReference, SubclassSelector] public IReference completeClassName;
|
||||
[Header(Constant.Header.Prefabs)]
|
||||
|
||||
public VisualTreeAsset visualTreeAsset;
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
public Dictionary<string, UXContainer> dictionary = new();
|
||||
public override void OnStart()
|
||||
private readonly Dictionary<string, UXContainer> dictionary = new();
|
||||
|
||||
[UXBindPath("quest-container")]
|
||||
private VisualElement _container;
|
||||
private void Start()
|
||||
{
|
||||
base.OnStart();
|
||||
UXUtils.Inject(this);
|
||||
|
||||
QuestSystem.OnQuestCreated += OnQuestCreated;
|
||||
QuestSystem.OnQuestCompleted += OnQuestCompleted;
|
||||
QuestSystem.OnQuestCanceled += OnQuestCancened;
|
||||
QuestSystem.OnQuestCanceled += OnQuestCanceled;
|
||||
|
||||
visualElement.Clear();
|
||||
_container.Clear();
|
||||
}
|
||||
void OnDestroy()
|
||||
private void OnDestroy()
|
||||
{
|
||||
QuestSystem.OnQuestCreated -= OnQuestCreated;
|
||||
QuestSystem.OnQuestCompleted -= OnQuestCompleted;
|
||||
QuestSystem.OnQuestCanceled -= OnQuestCancened;
|
||||
QuestSystem.OnQuestCanceled -= OnQuestCanceled;
|
||||
}
|
||||
void OnQuestCreated(QuestSystem.Info quest)
|
||||
private void OnQuestCreated(QuestSystem.Info quest)
|
||||
{
|
||||
BIT4Log.Log<UXQuest>($"已创建任务:{quest.name}");
|
||||
BIT4Log.Log<UXQuest>($"已创建任务:{quest.Name}");
|
||||
|
||||
|
||||
if (dictionary.TryGetValue(quest.name, out var container))
|
||||
if (dictionary.TryGetValue(quest.Name, out var container))
|
||||
{
|
||||
container.visualElement.RemoveFromClassList(completeClassName);
|
||||
container.visualElement.RemoveFromClassList(completeClassName.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
container = new(visualTreeAsset.CloneTree());
|
||||
dictionary.Add(quest.name, container);
|
||||
dictionary.Add(quest.Name, container);
|
||||
}
|
||||
|
||||
|
||||
container.titleLabel.text = quest.name;
|
||||
container.descriptionLabel.text = quest.description;
|
||||
container.titleLabel.text = quest.Name;
|
||||
container.descriptionLabel.text = quest.Description;
|
||||
|
||||
visualElement.Add(container);
|
||||
_container.Add(container);
|
||||
}
|
||||
void OnQuestCancened(QuestSystem.Info quest)
|
||||
private void OnQuestCanceled(QuestSystem.Info quest)
|
||||
{
|
||||
BIT4Log.Log<UXQuest>($"已取消任务:{quest.name}");
|
||||
if (dictionary.TryGetValue(quest.name, out var container))
|
||||
BIT4Log.Log<UXQuest>($"已取消任务:{quest.Name}");
|
||||
if (!dictionary.TryGetValue(quest.Name, out var container)) return;
|
||||
_container.Remove(container);
|
||||
dictionary.Remove(quest.Name);
|
||||
}
|
||||
private async void OnQuestCompleted(QuestSystem.Info quest)
|
||||
{
|
||||
BIT4Log.Log<UXQuest>($"已完成任务:{quest.Name}");
|
||||
|
||||
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
|
||||
{
|
||||
visualElement.Remove(container);
|
||||
dictionary.Remove(quest.name);
|
||||
await Task.Delay(8096, destroyCancellationToken);
|
||||
}
|
||||
}
|
||||
async void OnQuestCompleted(QuestSystem.Info quest)
|
||||
{
|
||||
BIT4Log.Log<UXQuest>($"已完成任务:{quest.name}");
|
||||
|
||||
if (dictionary.TryGetValue(quest.name, out var container))
|
||||
catch (System.OperationCanceledException) { }
|
||||
catch (System.Exception)
|
||||
{
|
||||
container.visualElement.AddToClassList(completeClassName);
|
||||
container.titleLabel.text = $"已完成:{quest.name}";
|
||||
container.visualElement.tabIndex = 256;
|
||||
if (clearAfterCompleted)
|
||||
{
|
||||
dictionary.Remove(quest.name);
|
||||
try
|
||||
{
|
||||
await Task.Delay(8096, cancellationToken);
|
||||
}
|
||||
catch (System.OperationCanceledException) { }
|
||||
catch (System.Exception)
|
||||
{
|
||||
visualElement.Remove(container);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
_container.Remove(container);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user