using System.Collections; using System.Collections.Generic; using UnityEngine; using BITKit; using BITKit.SubSystems.Quest; using UnityEngine.UIElements; using System.Threading.Tasks; namespace BITKit.UX { public class UXQuest : UXElement { [Header(Constant.Header.Settings)] public bool clearAfterCompleted; [Header(Constant.Header.Reference)] [SerializeReference, SubclassSelector] public References completeClassName; [Header(Constant.Header.Prefabs)] public VisualTreeAsset visualTreeAsset; [Header(Constant.Header.InternalVariables)] public Dictionary dictionary = new(); public override void OnStart() { base.OnStart(); QuestSystem.OnQuestCreated += OnQuestCreated; QuestSystem.OnQuestCompleted += OnQuestCompleted; QuestSystem.OnQuestCanceled += OnQuestCancened; visualElement.Clear(); } void OnDestroy() { QuestSystem.OnQuestCreated -= OnQuestCreated; QuestSystem.OnQuestCompleted -= OnQuestCompleted; QuestSystem.OnQuestCanceled -= OnQuestCancened; } 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; visualElement.Add(container); } void OnQuestCancened(QuestSystem.Info quest) { BIT4Log.Log($"已取消任务:{quest.name}"); if (dictionary.TryGetValue(quest.name, out var container)) { visualElement.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, cancellationToken); } catch (System.OperationCanceledException) { } catch (System.Exception) { visualElement.Remove(container); throw; } } } } } }