using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using BITKit; using BITKit.SubSystems.Quest; using UnityEngine.UIElements; using System.Threading.Tasks; namespace BITKit.UX { public class UXQuest : MonoBehaviour { [Header(Constant.Header.Settings)] public bool clearAfterCompleted; [Header(Constant.Header.Components)] [SerializeField] private UIDocument document; [Header(Constant.Header.Reference)] [SerializeReference, SubclassSelector] public References completeClassName; [Header(Constant.Header.Prefabs)] public VisualTreeAsset visualTreeAsset; [Header(Constant.Header.InternalVariables)] private readonly Dictionary dictionary = new(); [UXBindPath("quest-container")] private VisualElement _container; private void Start() { UXUtils.Inject(this); QuestSystem.OnQuestCreated += OnQuestCreated; QuestSystem.OnQuestCompleted += OnQuestCompleted; QuestSystem.OnQuestCanceled += OnQuestCancened; _container.Clear(); } private void OnDestroy() { QuestSystem.OnQuestCreated -= OnQuestCreated; QuestSystem.OnQuestCompleted -= OnQuestCompleted; QuestSystem.OnQuestCanceled -= OnQuestCancened; } private void OnQuestCreated(QuestSystem.Info quest) { BIT4Log.Log($"已创建任务:{quest.name}"); if (dictionary.TryGetValue(quest.name, out var container)) { container.visualElement.RemoveFromClassList(completeClassName); } else { container = new(visualTreeAsset.CloneTree()); dictionary.Add(quest.name, container); } container.titleLabel.text = quest.name; container.descriptionLabel.text = quest.description; _container.Add(container); } void OnQuestCancened(QuestSystem.Info quest) { BIT4Log.Log($"已取消任务:{quest.name}"); if (dictionary.TryGetValue(quest.name, out var container)) { _container.Remove(container); dictionary.Remove(quest.name); } } async void OnQuestCompleted(QuestSystem.Info quest) { BIT4Log.Log($"已完成任务:{quest.name}"); if (dictionary.TryGetValue(quest.name, out var container)) { container.visualElement.AddToClassList(completeClassName); container.titleLabel.text = $"已完成:{quest.name}"; container.visualElement.tabIndex = 256; if (clearAfterCompleted) { dictionary.Remove(quest.name); try { await Task.Delay(8096, destroyCancellationToken); } catch (System.OperationCanceledException) { } catch (System.Exception) { _container.Remove(container); throw; } } } } } }