| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Huds.QuestsTracker |
| | 9 | | { |
| | 10 | | public interface IQuestsTrackerHUDView |
| | 11 | | { |
| | 12 | | void UpdateQuest(string questId, bool hasProgressed); |
| | 13 | | void RemoveEntry(string questId); |
| | 14 | | void PinQuest(string questId); |
| | 15 | | void UnpinQuest(string questId); |
| | 16 | | void ClearEntries(); |
| | 17 | | void SetVisibility(bool visibility); |
| | 18 | | void AddReward(string questId, QuestReward reward); |
| | 19 | | void Dispose(); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public class QuestsTrackerHUDView : MonoBehaviour, IQuestsTrackerHUDView |
| | 23 | | { |
| 1 | 24 | | internal static int ENTRIES_PER_FRAME { get; set; } = 5; |
| 10 | 25 | | private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests; |
| 2 | 26 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| | 27 | |
|
| | 28 | | [SerializeField] internal RectTransform questsContainer; |
| | 29 | | [SerializeField] internal GameObject questPrefab; |
| | 30 | | [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity; |
| | 31 | | [SerializeField] internal QuestsNotificationsController notificationsController; |
| | 32 | |
|
| 15 | 33 | | internal readonly Dictionary<string, QuestsTrackerEntry> currentEntries = new Dictionary<string, QuestsTrackerEn |
| | 34 | | private bool layoutRebuildRequested; |
| | 35 | |
|
| | 36 | | private bool isDestroyed = false; |
| | 37 | |
|
| | 38 | | public static QuestsTrackerHUDView Create() |
| | 39 | | { |
| 1 | 40 | | QuestsTrackerHUDView view = Instantiate(Resources.Load<GameObject>("QuestsTrackerHUD")).GetComponent<QuestsT |
| | 41 | |
|
| | 42 | | #if UNITY_EDITOR |
| 1 | 43 | | view.gameObject.name = "_QuestsTrackerHUDView"; |
| | 44 | | #endif |
| 1 | 45 | | return view; |
| | 46 | | } |
| | 47 | |
|
| 28 | 48 | | private void Awake() { StartCoroutine(RemoveEntriesRoutine()); } |
| | 49 | |
|
| | 50 | | public void UpdateQuest(string questId, bool hasProgressed) |
| | 51 | | { |
| 1 | 52 | | if (hasProgressed || currentEntries.ContainsKey(questId)) |
| 1 | 53 | | AddOrUpdateQuest(questId, pinnedQuests.Contains(questId)); |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | internal void AddOrUpdateQuest(string questId, bool isPinned) |
| | 57 | | { |
| 10 | 58 | | if (!quests.TryGetValue(questId, out QuestModel quest)) |
| 0 | 59 | | return; |
| | 60 | |
|
| 10 | 61 | | if (quest.isCompleted && !quest.justProgressed) |
| | 62 | | { |
| 0 | 63 | | RemoveEntry(questId); |
| 0 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| 10 | 67 | | if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry questEntry)) |
| | 68 | | { |
| 10 | 69 | | questEntry = Instantiate(questPrefab, questsContainer).GetComponent<QuestsTrackerEntry>(); |
| 56 | 70 | | questEntry.OnLayoutRebuildRequested += () => layoutRebuildRequested = true; |
| 10 | 71 | | questEntry.OnQuestCompleted += (x => notificationsController.ShowQuestCompleted(x)); |
| 10 | 72 | | questEntry.OnRewardObtained += (x => notificationsController.ShowRewardObtained(x)); |
| 10 | 73 | | currentEntries.Add(quest.id, questEntry); |
| | 74 | | } |
| | 75 | |
|
| 10 | 76 | | questEntry.transform.SetSiblingIndex(0); |
| | 77 | |
|
| 10 | 78 | | questEntry.Populate(quest); |
| 10 | 79 | | questEntry.SetPinStatus(isPinned); |
| 10 | 80 | | layoutRebuildRequested = true; |
| 10 | 81 | | } |
| | 82 | |
|
| | 83 | | public void RemoveEntry(string questId) |
| | 84 | | { |
| 1 | 85 | | if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry)) |
| 0 | 86 | | return; |
| | 87 | |
|
| 1 | 88 | | currentEntries.Remove(questId); |
| 1 | 89 | | entry.StartDestroy(); |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | public void PinQuest(string questId) |
| | 93 | | { |
| 2 | 94 | | if (currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry)) |
| | 95 | | { |
| 1 | 96 | | entry.SetPinStatus(true); |
| 1 | 97 | | } |
| | 98 | | else |
| | 99 | | { |
| 1 | 100 | | AddOrUpdateQuest(questId, pinnedQuests.Contains(questId)); |
| | 101 | | } |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | public void UnpinQuest(string questId) |
| | 105 | | { |
| 1 | 106 | | if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry)) |
| 0 | 107 | | return; |
| | 108 | |
|
| 1 | 109 | | entry.SetPinStatus(false); |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | private void Update() |
| | 113 | | { |
| 137 | 114 | | if (layoutRebuildRequested) |
| | 115 | | { |
| 4 | 116 | | layoutRebuildRequested = false; |
| 4 | 117 | | questsContainer.ForceUpdateLayout( false); |
| 4 | 118 | | dynamicScrollSensitivity?.RecalculateSensitivity(); |
| | 119 | | } |
| 137 | 120 | | } |
| | 121 | |
|
| | 122 | | public void ClearEntries() |
| | 123 | | { |
| 0 | 124 | | foreach ((string key, QuestsTrackerEntry value) in currentEntries) |
| | 125 | | { |
| 0 | 126 | | Destroy(value.gameObject); |
| | 127 | | } |
| 0 | 128 | | currentEntries.Clear(); |
| 0 | 129 | | } |
| | 130 | |
|
| 2 | 131 | | public void SetVisibility(bool visibility) { gameObject.SetActive(visibility); } |
| | 132 | | public void AddReward(string questId, QuestReward reward) |
| | 133 | | { |
| 0 | 134 | | if (!currentEntries.TryGetValue(questId, out QuestsTrackerEntry entry)) |
| 0 | 135 | | return; |
| 0 | 136 | | entry.AddRewardToGive(reward); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | public void Dispose() |
| | 140 | | { |
| 1 | 141 | | if (!isDestroyed) |
| 1 | 142 | | Destroy(gameObject); |
| 1 | 143 | | } |
| | 144 | |
|
| 28 | 145 | | private void OnDestroy() { isDestroyed = true; } |
| | 146 | |
|
| | 147 | | private IEnumerator RemoveEntriesRoutine() |
| | 148 | | { |
| 10 | 149 | | while (true) |
| | 150 | | { |
| 24 | 151 | | var entriesToRemove = currentEntries.Where(x => x.Value.isReadyForDisposal).Select(x => x.Key).ToArray() |
| 48 | 152 | | foreach (string questId in entriesToRemove) |
| | 153 | | { |
| 0 | 154 | | RemoveEntry(questId); |
| | 155 | | } |
| 24 | 156 | | yield return WaitForSecondsCache.Get(0.25f); |
| | 157 | | } |
| | 158 | | } |
| | 159 | | } |
| | 160 | | } |