| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Huds.QuestsTracker |
| | 9 | | { |
| | 10 | | public class QuestsTrackerSection : MonoBehaviour |
| | 11 | | { |
| 1 | 12 | | private static readonly int ANIMATION_TRIGGER_OUT = Animator.StringToHash("Out"); |
| | 13 | |
|
| | 14 | | public enum SequenceState |
| | 15 | | { |
| | 16 | | NotStarted, |
| | 17 | | ProgressingOngoingTasks, |
| | 18 | | ShowingNewTasks, |
| | 19 | | Finished |
| | 20 | | } |
| | 21 | |
|
| | 22 | | [SerializeField] internal GameObject titleContainer; |
| | 23 | | [SerializeField] internal TextMeshProUGUI sectionTitle; |
| | 24 | | [SerializeField] internal RectTransform taskContainer; |
| | 25 | | [SerializeField] internal GameObject taskPrefab; |
| | 26 | | [SerializeField] internal Animator animator; |
| | 27 | | [SerializeField] internal AudioEvent newTaskAudioEvent; |
| | 28 | |
|
| | 29 | | public event Action OnLayoutRebuildRequested; |
| | 30 | | public event Action<string> OnDestroyed; |
| | 31 | |
|
| 0 | 32 | | public SequenceState sequenceState { get; private set; } = SequenceState.NotStarted; |
| | 33 | |
|
| | 34 | | private QuestSection section; |
| 26 | 35 | | internal readonly Dictionary<string, QuestsTrackerTask> taskEntries = new Dictionary<string, QuestsTrackerTask>( |
| 26 | 36 | | private readonly List<Coroutine> tasksRoutines = new List<Coroutine>(); |
| | 37 | |
|
| | 38 | | public void Populate(QuestSection newSection) |
| | 39 | | { |
| 25 | 40 | | sequenceState = SequenceState.NotStarted; |
| 25 | 41 | | ClearTaskRoutines(); |
| | 42 | |
|
| 25 | 43 | | section = newSection; |
| 25 | 44 | | QuestTask[] allTasks = section.tasks; |
| 25 | 45 | | titleContainer.SetActive(!string.IsNullOrEmpty(section.name)); |
| 25 | 46 | | sectionTitle.text = section.name; |
| | 47 | |
|
| | 48 | | bool hasCompletedTasksToShow = false; |
| 25 | 49 | | List<string> entriesToRemove = taskEntries.Keys.ToList(); |
| | 50 | |
|
| 96 | 51 | | for (int index = 0; index < allTasks.Length; index++) |
| | 52 | | { |
| 23 | 53 | | QuestTask task = allTasks[index]; |
| | 54 | | //We only show completed quests that has been just completed to show the progress |
| 23 | 55 | | if (!taskEntries.ContainsKey(task.id) && (task.status == QuestsLiterals.Status.BLOCKED || (task.progress |
| | 56 | | continue; |
| | 57 | |
|
| 22 | 58 | | entriesToRemove.Remove(task.id); |
| 22 | 59 | | if (!taskEntries.TryGetValue(task.id, out QuestsTrackerTask taskEntry)) |
| | 60 | | { |
| 22 | 61 | | taskEntry = CreateTask(); |
| | 62 | | //New tasks are invisible |
| 22 | 63 | | taskEntry.gameObject.SetActive(!task.justUnlocked); |
| 22 | 64 | | taskEntries.Add(task.id, taskEntry); |
| | 65 | | } |
| | 66 | |
|
| 22 | 67 | | taskEntry.Populate(task); |
| 22 | 68 | | taskEntry.transform.SetSiblingIndex(index); |
| | 69 | | } |
| | 70 | |
|
| 50 | 71 | | for (int index = 0; index < entriesToRemove.Count; index++) |
| | 72 | | { |
| 0 | 73 | | DestroyTaskEntry(entriesToRemove[index]); |
| | 74 | | } |
| 25 | 75 | | OnLayoutRebuildRequested?.Invoke(); |
| | 76 | |
|
| 18 | 77 | | } |
| | 78 | |
|
| | 79 | | internal QuestsTrackerTask CreateTask() |
| | 80 | | { |
| 22 | 81 | | var taskEntry = Instantiate(taskPrefab, taskContainer).GetComponent<QuestsTrackerTask>(); |
| 22 | 82 | | taskEntry.OnDestroyed += (taskId) => |
| | 83 | | { |
| 0 | 84 | | taskEntries.Remove(taskId); |
| 0 | 85 | | OnLayoutRebuildRequested?.Invoke(); |
| 0 | 86 | | }; |
| 22 | 87 | | return taskEntry; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private void DestroyTaskEntry(string taskId) |
| | 91 | | { |
| 0 | 92 | | if (!taskEntries.TryGetValue(taskId, out QuestsTrackerTask taskEntry)) |
| 0 | 93 | | return; |
| 0 | 94 | | Destroy(taskEntry.gameObject); |
| 0 | 95 | | taskEntries.Remove(taskId); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public IEnumerator Sequence() |
| | 99 | | { |
| 0 | 100 | | sequenceState = SequenceState.ProgressingOngoingTasks; |
| | 101 | |
|
| 0 | 102 | | ClearTaskRoutines(); |
| | 103 | |
|
| 0 | 104 | | List<QuestsTrackerTask> visibleTasks = new List<QuestsTrackerTask>(); |
| 0 | 105 | | List<QuestsTrackerTask> newTasks = new List<QuestsTrackerTask>(); |
| 0 | 106 | | foreach (QuestsTrackerTask task in taskEntries.Values) |
| | 107 | | { |
| 0 | 108 | | if (task.gameObject.activeSelf) |
| 0 | 109 | | visibleTasks.Add(task); |
| | 110 | | else |
| 0 | 111 | | newTasks.Add(task); |
| | 112 | | } |
| | 113 | |
|
| | 114 | | //Progress of currently visible tasks |
| 0 | 115 | | for (int i = 0; i < visibleTasks.Count; i++) |
| | 116 | | { |
| 0 | 117 | | tasksRoutines.Add(StartCoroutine(visibleTasks[i].ProgressAndCompleteSequence())); |
| | 118 | | } |
| 0 | 119 | | yield return WaitForTaskRoutines(); |
| 0 | 120 | | OnLayoutRebuildRequested?.Invoke(); |
| | 121 | |
|
| 0 | 122 | | sequenceState = SequenceState.ShowingNewTasks; |
| | 123 | |
|
| | 124 | | //Play "new task" sound |
| 0 | 125 | | if (newTasks.Count > 0) |
| 0 | 126 | | newTaskAudioEvent.Play(); |
| | 127 | |
|
| | 128 | | //Show and progress of new tasks |
| 0 | 129 | | for (int i = 0; i < newTasks.Count; i++) |
| | 130 | | { |
| 0 | 131 | | newTasks[i].gameObject.SetActive(true); |
| 0 | 132 | | newTasks[i].SetIsNew(true); |
| 0 | 133 | | tasksRoutines.Add(StartCoroutine(newTasks[i].ProgressAndCompleteSequence())); |
| | 134 | | } |
| 0 | 135 | | OnLayoutRebuildRequested?.Invoke(); |
| | 136 | |
|
| 0 | 137 | | yield return WaitForTaskRoutines(); |
| | 138 | |
|
| 0 | 139 | | if (taskEntries.Count == 0) |
| | 140 | | { |
| 0 | 141 | | animator.SetTrigger(ANIMATION_TRIGGER_OUT); |
| 0 | 142 | | yield return WaitForSecondsCache.Get(0.5f); |
| 0 | 143 | | Destroy(gameObject); |
| 0 | 144 | | OnDestroyed?.Invoke(section.id); |
| | 145 | | } |
| 0 | 146 | | sequenceState = SequenceState.Finished; |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | public void SetExpandCollapseState(bool newIsExpanded) |
| | 150 | | { |
| 72 | 151 | | foreach (QuestsTrackerTask taskEntry in taskEntries.Values) |
| | 152 | | { |
| 18 | 153 | | taskEntry.SetExpandedStatus(newIsExpanded); |
| | 154 | | } |
| | 155 | |
|
| 18 | 156 | | OnLayoutRebuildRequested?.Invoke(); |
| 18 | 157 | | } |
| | 158 | |
|
| | 159 | | public void ClearTaskRoutines() |
| | 160 | | { |
| 25 | 161 | | if (tasksRoutines.Count > 0) |
| | 162 | | { |
| 0 | 163 | | for (int i = 0; i < tasksRoutines.Count; i++) |
| | 164 | | { |
| 0 | 165 | | if (tasksRoutines[i] != null) |
| 0 | 166 | | StopCoroutine(tasksRoutines[i]); |
| | 167 | | } |
| 0 | 168 | | tasksRoutines.Clear(); |
| | 169 | | } |
| 25 | 170 | | } |
| | 171 | |
|
| | 172 | | private IEnumerator WaitForTaskRoutines() |
| | 173 | | { |
| 0 | 174 | | for (int i = 0; i < tasksRoutines.Count; i++) |
| | 175 | | { |
| | 176 | | //yielding Coroutines (not IEnumerators) allows us to wait for them in parallel |
| 0 | 177 | | yield return tasksRoutines[i]; |
| | 178 | | } |
| 0 | 179 | | } |
| | 180 | | } |
| | 181 | | } |