| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL.Huds.QuestsTracker |
| | 11 | | { |
| | 12 | | public class QuestsTrackerEntry : MonoBehaviour |
| | 13 | | { |
| | 14 | | internal const float OUT_ANIM_DELAY = 0.5f; |
| | 15 | | private const float DELAY_TO_DESTROY = 0.5f; |
| 1 | 16 | | private static readonly int OUT_ANIM_TRIGGER = Animator.StringToHash("Out"); |
| | 17 | | public event Action OnLayoutRebuildRequested; |
| | 18 | | public event Action<QuestModel> OnQuestCompleted; |
| | 19 | | public event Action<QuestReward> OnRewardObtained; |
| | 20 | |
|
| | 21 | | [SerializeField] internal TextMeshProUGUI questTitle; |
| | 22 | | [SerializeField] internal TextMeshProUGUI questProgressText; |
| | 23 | | [SerializeField] internal Image progress; |
| | 24 | | [SerializeField] internal RectTransform sectionContainer; |
| | 25 | | [SerializeField] internal GameObject sectionPrefab; |
| | 26 | | [SerializeField] internal Button expandCollapseButton; |
| | 27 | | [SerializeField] internal GameObject expandIcon; |
| | 28 | | [SerializeField] internal GameObject collapseIcon; |
| | 29 | | [SerializeField] internal Toggle pinQuestToggle; |
| | 30 | | [SerializeField] internal RawImage iconImage; |
| | 31 | | [SerializeField] internal Animator containerAnimator; |
| | 32 | |
|
| 0 | 33 | | public bool isReadyForDisposal { get; private set; } = false; |
| 0 | 34 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| 0 | 35 | | private bool isProgressAnimationDone => Math.Abs(progress.fillAmount - progressTarget) < Mathf.Epsilon; |
| | 36 | |
|
| | 37 | | private float progressTarget = 0; |
| | 38 | | private AssetPromise_Texture iconPromise; |
| | 39 | |
|
| | 40 | | internal QuestModel quest; |
| | 41 | | internal bool isExpanded; |
| | 42 | | internal bool isPinned; |
| | 43 | | internal bool outAnimDone = false; |
| | 44 | | internal bool hasProgressedThisUpdate = false; |
| | 45 | |
|
| 19 | 46 | | internal readonly Dictionary<string, QuestsTrackerSection> sectionEntries = new Dictionary<string, QuestsTracker |
| 19 | 47 | | private readonly List<QuestReward> rewardsToNotify = new List<QuestReward>(); |
| 19 | 48 | | private readonly List<Coroutine> sectionRoutines = new List<Coroutine>(); |
| | 49 | | private Coroutine sequenceRoutine; |
| | 50 | | private Coroutine progressRoutine; |
| | 51 | |
|
| | 52 | | public void Awake() |
| | 53 | | { |
| 18 | 54 | | pinQuestToggle.onValueChanged.AddListener(OnPinToggleValueChanged); |
| | 55 | |
|
| 18 | 56 | | expandCollapseButton.gameObject.SetActive(false); |
| 18 | 57 | | SetExpandCollapseState(true); |
| 18 | 58 | | expandCollapseButton.onClick.AddListener(() => SetExpandCollapseState(!isExpanded)); |
| 18 | 59 | | StartCoroutine(OutDelayRoutine()); |
| | 60 | |
|
| 18 | 61 | | AudioScriptableObjects.fadeIn.Play(); |
| 18 | 62 | | } |
| | 63 | |
|
| | 64 | | private IEnumerator OutDelayRoutine() |
| | 65 | | { |
| 18 | 66 | | yield return new WaitForSeconds(OUT_ANIM_DELAY); |
| 0 | 67 | | outAnimDone = true; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public void Populate(QuestModel newQuest) |
| | 71 | | { |
| 16 | 72 | | StopSequence(); |
| | 73 | |
|
| 16 | 74 | | quest = newQuest; |
| 16 | 75 | | SetIcon(quest.thumbnail_entry); |
| 35 | 76 | | QuestTask[] allTasks = quest.sections.SelectMany(x => x.tasks).ToArray(); |
| | 77 | |
|
| 35 | 78 | | int completedTasksAmount = allTasks.Count(x => x.progress >= 1); |
| 16 | 79 | | questTitle.text = $"{quest.name}"; |
| 16 | 80 | | questProgressText.text = $"{completedTasksAmount}/{allTasks.Length}"; |
| 16 | 81 | | progress.fillAmount = quest.oldProgress; |
| 16 | 82 | | progressTarget = quest.progress; |
| | 83 | |
|
| 54 | 84 | | hasProgressedThisUpdate = newQuest.sections.Any(x => x.tasks.Any(y => y.justProgressed)); |
| | 85 | |
|
| 16 | 86 | | List<string> entriesToRemove = sectionEntries.Keys.ToList(); |
| 16 | 87 | | List<QuestsTrackerSection> visibleSectionEntries = new List<QuestsTrackerSection>(); |
| 16 | 88 | | List<QuestsTrackerSection> newSectionEntries = new List<QuestsTrackerSection>(); |
| 70 | 89 | | for (var i = 0; i < quest.sections.Length; i++) |
| | 90 | | { |
| 19 | 91 | | QuestSection section = quest.sections[i]; |
| | 92 | |
|
| 38 | 93 | | bool hasTasks = section.tasks.Any(x => x.status != QuestsLiterals.Status.BLOCKED && (x.progress < 1 || x |
| 19 | 94 | | if (!hasTasks) |
| | 95 | | continue; |
| | 96 | |
|
| 36 | 97 | | bool isVisible = section.tasks.Any(x => x.status != QuestsLiterals.Status.BLOCKED && ((x.progress < 1 && |
| | 98 | |
|
| 18 | 99 | | entriesToRemove.Remove(section.id); |
| 18 | 100 | | if (!sectionEntries.TryGetValue(section.id, out QuestsTrackerSection sectionEntry)) |
| | 101 | | { |
| 18 | 102 | | sectionEntry = CreateSection(); |
| | 103 | | //New tasks are invisible |
| 18 | 104 | | sectionEntries.Add(section.id, sectionEntry); |
| | 105 | | } |
| | 106 | |
|
| 18 | 107 | | sectionEntry.gameObject.SetActive(isVisible); |
| 18 | 108 | | sectionEntry.Populate(section); |
| 18 | 109 | | sectionEntry.transform.SetAsLastSibling(); |
| | 110 | |
|
| 18 | 111 | | if (sectionEntry.gameObject.activeSelf) |
| 17 | 112 | | visibleSectionEntries.Add(sectionEntry); |
| | 113 | | else |
| 1 | 114 | | newSectionEntries.Add(sectionEntry); |
| | 115 | | } |
| | 116 | |
|
| 32 | 117 | | for (int index = 0; index < entriesToRemove.Count; index++) |
| | 118 | | { |
| 0 | 119 | | DestroySectionEntry(entriesToRemove[index]); |
| | 120 | | } |
| | 121 | |
|
| 16 | 122 | | expandCollapseButton.gameObject.SetActive(sectionEntries.Count > 0); |
| 16 | 123 | | SetExpandCollapseState(true); |
| 16 | 124 | | OnLayoutRebuildRequested?.Invoke(); |
| | 125 | |
|
| 16 | 126 | | sequenceRoutine = StartCoroutine(Sequence(visibleSectionEntries, newSectionEntries)); |
| 16 | 127 | | } |
| | 128 | |
|
| | 129 | | private QuestsTrackerSection CreateSection() |
| | 130 | | { |
| 18 | 131 | | var sectionEntry = Instantiate(sectionPrefab, sectionContainer).GetComponent<QuestsTrackerSection>(); |
| 54 | 132 | | sectionEntry.OnLayoutRebuildRequested += () => OnLayoutRebuildRequested?.Invoke(); |
| 18 | 133 | | sectionEntry.OnDestroyed += (sectionId) => sectionEntries.Remove(sectionId); |
| 18 | 134 | | return sectionEntry; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | private void DestroySectionEntry(string taskId) |
| | 138 | | { |
| 0 | 139 | | if (!sectionEntries.TryGetValue(taskId, out QuestsTrackerSection sectionEntry)) |
| 0 | 140 | | return; |
| 0 | 141 | | Destroy(sectionEntry.gameObject); |
| 0 | 142 | | sectionEntries.Remove(taskId); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | internal void SetIcon(string iconURL) |
| | 146 | | { |
| 16 | 147 | | if (iconPromise != null) |
| | 148 | | { |
| 0 | 149 | | iconPromise.ClearEvents(); |
| 0 | 150 | | AssetPromiseKeeper_Texture.i.Forget(iconPromise); |
| | 151 | | } |
| | 152 | |
|
| 16 | 153 | | if (string.IsNullOrEmpty(iconURL)) |
| | 154 | | { |
| 16 | 155 | | iconImage.gameObject.SetActive(false); |
| 16 | 156 | | return; |
| | 157 | | } |
| | 158 | |
|
| 0 | 159 | | iconPromise = new AssetPromise_Texture(iconURL); |
| 0 | 160 | | iconPromise.OnSuccessEvent += assetTexture => |
| | 161 | | { |
| 0 | 162 | | iconImage.gameObject.SetActive(true); |
| 0 | 163 | | iconImage.texture = assetTexture.texture; |
| 0 | 164 | | }; |
| 0 | 165 | | iconPromise.OnFailEvent += (assetTexture, error) => |
| | 166 | | { |
| 0 | 167 | | iconImage.gameObject.SetActive(false); |
| 0 | 168 | | Debug.Log($"Error downloading quest tracker entry icon: {iconURL}, Exception: {error}"); |
| 0 | 169 | | }; |
| | 170 | |
|
| 0 | 171 | | AssetPromiseKeeper_Texture.i.Keep(iconPromise); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | private IEnumerator Sequence(List<QuestsTrackerSection> visibleSections, List<QuestsTrackerSection> newSections) |
| | 175 | | { |
| 36 | 176 | | yield return new WaitUntil(() => outAnimDone); |
| | 177 | |
|
| 0 | 178 | | ClearSectionRoutines(); |
| | 179 | |
|
| 0 | 180 | | if (progressRoutine != null) |
| 0 | 181 | | StopCoroutine(progressRoutine); |
| 0 | 182 | | progressRoutine = StartCoroutine(ProgressSequence()); |
| | 183 | |
|
| | 184 | | //Progress of currently visible sections |
| 0 | 185 | | for (int i = 0; i < visibleSections.Count; i++) |
| | 186 | | { |
| 0 | 187 | | sectionRoutines.Add(StartCoroutine(visibleSections[i].Sequence())); |
| | 188 | | } |
| | 189 | |
|
| 0 | 190 | | yield return WaitForTaskRoutines(); |
| | 191 | |
|
| | 192 | | //Show and progress of new tasks |
| 0 | 193 | | for (int i = 0; i < newSections.Count; i++) |
| | 194 | | { |
| 0 | 195 | | newSections[i].gameObject.SetActive(true); |
| 0 | 196 | | sectionRoutines.Add(StartCoroutine(newSections[i].Sequence())); |
| | 197 | | } |
| | 198 | |
|
| 0 | 199 | | OnLayoutRebuildRequested?.Invoke(); |
| 0 | 200 | | yield return WaitForTaskRoutines(); |
| | 201 | |
|
| 0 | 202 | | OnLayoutRebuildRequested?.Invoke(); |
| | 203 | | //The entry should exit automatically if questCompleted or no progress, therefore the use of MinValue |
| 0 | 204 | | DateTime tasksIdleTime = (quest.isCompleted || !hasProgressedThisUpdate) ? DateTime.MinValue : DateTime.Now; |
| 0 | 205 | | yield return new WaitUntil(() => isProgressAnimationDone && !isPinned && (DateTime.Now - tasksIdleTime) > Ti |
| | 206 | |
|
| 0 | 207 | | if (quest.isCompleted) |
| 0 | 208 | | OnQuestCompleted?.Invoke(quest); |
| | 209 | |
|
| 0 | 210 | | for (int i = 0; i < rewardsToNotify.Count; i++) |
| | 211 | | { |
| 0 | 212 | | OnRewardObtained?.Invoke(rewardsToNotify[i]); |
| | 213 | | } |
| | 214 | |
|
| 0 | 215 | | rewardsToNotify.Clear(); |
| | 216 | |
|
| 0 | 217 | | isReadyForDisposal = true; |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | private void StopSequence() |
| | 221 | | { |
| 16 | 222 | | if (sequenceRoutine != null) |
| 0 | 223 | | StopCoroutine(sequenceRoutine); |
| 16 | 224 | | ClearSectionRoutines(); |
| 16 | 225 | | } |
| | 226 | |
|
| | 227 | | private void ClearSectionRoutines() |
| | 228 | | { |
| 16 | 229 | | if (sectionRoutines.Count > 0) |
| | 230 | | { |
| 0 | 231 | | for (int i = 0; i < sectionRoutines.Count; i++) |
| | 232 | | { |
| 0 | 233 | | if (sectionRoutines[i] != null) |
| 0 | 234 | | StopCoroutine(sectionRoutines[i]); |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | sectionRoutines.Clear(); |
| | 238 | | } |
| | 239 | |
|
| 32 | 240 | | foreach (var section in sectionEntries) |
| | 241 | | { |
| 0 | 242 | | section.Value.ClearTaskRoutines(); |
| | 243 | | } |
| 16 | 244 | | } |
| | 245 | |
|
| | 246 | | internal void SetExpandCollapseState(bool newIsExpanded) |
| | 247 | | { |
| 34 | 248 | | isExpanded = newIsExpanded; |
| 34 | 249 | | expandIcon.SetActive(!isExpanded); |
| 34 | 250 | | collapseIcon.SetActive(isExpanded); |
| 34 | 251 | | sectionContainer.gameObject.SetActive(isExpanded); |
| | 252 | |
|
| 104 | 253 | | foreach (QuestsTrackerSection section in sectionEntries.Values) |
| | 254 | | { |
| 18 | 255 | | section.SetExpandCollapseState(newIsExpanded); |
| | 256 | | } |
| | 257 | |
|
| 34 | 258 | | OnLayoutRebuildRequested?.Invoke(); |
| 10 | 259 | | } |
| | 260 | |
|
| | 261 | | private void OnPinToggleValueChanged(bool isOn) |
| | 262 | | { |
| 0 | 263 | | if (quest == null) |
| 0 | 264 | | return; |
| | 265 | |
|
| 0 | 266 | | if (!quest.canBePinned) |
| | 267 | | { |
| 0 | 268 | | pinnedQuests.Remove(quest.id); |
| 0 | 269 | | SetPinStatus(false); |
| 0 | 270 | | return; |
| | 271 | | } |
| | 272 | |
|
| 0 | 273 | | if (isOn) |
| | 274 | | { |
| 0 | 275 | | if (!pinnedQuests.Contains(quest.id)) |
| 0 | 276 | | pinnedQuests.Add(quest.id); |
| 0 | 277 | | } |
| | 278 | | else |
| | 279 | | { |
| 0 | 280 | | pinnedQuests.Remove(quest.id); |
| | 281 | | } |
| | 282 | |
|
| 0 | 283 | | QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestsTracker); |
| 0 | 284 | | } |
| | 285 | |
|
| | 286 | | public void SetPinStatus(bool newIsPinned) |
| | 287 | | { |
| 14 | 288 | | isPinned = newIsPinned; |
| 14 | 289 | | pinQuestToggle.SetIsOnWithoutNotify(newIsPinned); |
| 14 | 290 | | } |
| | 291 | |
|
| 0 | 292 | | public void AddRewardToGive(QuestReward reward) { rewardsToNotify.Add(reward); } |
| | 293 | |
|
| 2 | 294 | | public void StartDestroy() { StartCoroutine(DestroySequence()); } |
| | 295 | |
|
| | 296 | | private IEnumerator DestroySequence() |
| | 297 | | { |
| 1 | 298 | | AudioScriptableObjects.fadeOut.Play(); |
| 1 | 299 | | containerAnimator.SetTrigger(OUT_ANIM_TRIGGER); |
| 1 | 300 | | yield return WaitForSecondsCache.Get(DELAY_TO_DESTROY); |
| | 301 | |
|
| 0 | 302 | | OnLayoutRebuildRequested?.Invoke(); |
| 0 | 303 | | Destroy(gameObject); |
| 0 | 304 | | } |
| | 305 | |
|
| | 306 | | private IEnumerator ProgressSequence() |
| | 307 | | { |
| 0 | 308 | | while (Math.Abs(progress.fillAmount - progressTarget) > Mathf.Epsilon) |
| | 309 | | { |
| 0 | 310 | | progress.fillAmount = Mathf.MoveTowards(progress.fillAmount, progressTarget, Time.deltaTime); |
| 0 | 311 | | yield return null; |
| | 312 | | } |
| | 313 | |
|
| 0 | 314 | | progressRoutine = null; |
| 0 | 315 | | } |
| | 316 | |
|
| | 317 | | private IEnumerator WaitForTaskRoutines() |
| | 318 | | { |
| 0 | 319 | | for (int i = 0; i < sectionRoutines.Count; i++) |
| | 320 | | { |
| | 321 | | //yielding Coroutines (not IEnumerators) allows us to wait for them in parallel |
| 0 | 322 | | yield return sectionRoutines[i]; |
| | 323 | | } |
| 0 | 324 | | } |
| | 325 | |
|
| | 326 | | private void OnDestroy() |
| | 327 | | { |
| 18 | 328 | | if (iconPromise != null) |
| | 329 | | { |
| 0 | 330 | | iconPromise.ClearEvents(); |
| 0 | 331 | | AssetPromiseKeeper_Texture.i.Forget(iconPromise); |
| | 332 | | } |
| 18 | 333 | | } |
| | 334 | | } |
| | 335 | | } |