| | 1 | | using DCL.Interface; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Huds.QuestsTracker |
| | 10 | | { |
| | 11 | | public class QuestsTrackerTask : MonoBehaviour |
| | 12 | | { |
| | 13 | | private const string TASK_COMPLETED_ANIMATION_NAME = "QuestTrackerTaskCompleted"; |
| 1 | 14 | | private static readonly int EXPAND_ANIMATOR_TRIGGER = Animator.StringToHash("Expand"); |
| 1 | 15 | | private static readonly int COLLAPSE_ANIMATOR_TRIGGER = Animator.StringToHash("Collapse"); |
| 1 | 16 | | private static readonly int COMPLETED_ANIMATOR_TRIGGER = Animator.StringToHash("Completed"); |
| 1 | 17 | | private static readonly int NEW_ANIMATOR_TRIGGER = Animator.StringToHash("New"); |
| | 18 | |
|
| | 19 | | public event Action<string> OnDestroyed; |
| | 20 | |
|
| | 21 | | [SerializeField] internal TextMeshProUGUI taskTitle; |
| | 22 | | [SerializeField] internal Image progress; |
| | 23 | | [SerializeField] internal TextMeshProUGUI progressText; |
| | 24 | | [SerializeField] internal Button jumpInButton; |
| | 25 | | [SerializeField] internal Animator animator; |
| | 26 | |
|
| | 27 | | [Header("Audio Events")] |
| | 28 | | [SerializeField] internal AudioEvent progressBarIncrementAudioEvent; |
| | 29 | | [SerializeField] internal AudioEvent taskCompleteAudioEvent; |
| | 30 | |
|
| | 31 | | private QuestTask task = null; |
| | 32 | | private float progressTarget = 0; |
| | 33 | | private Action jumpInDelegate; |
| | 34 | |
|
| 42 | 35 | | public void Awake() { jumpInButton.onClick.AddListener(() => { jumpInDelegate?.Invoke(); }); } |
| | 36 | |
|
| 0 | 37 | | public void SetIsNew(bool isNew) { animator.SetBool(NEW_ANIMATOR_TRIGGER, isNew); } |
| | 38 | |
|
| | 39 | | public void Populate(QuestTask newTask) |
| | 40 | | { |
| 22 | 41 | | StopAllCoroutines(); |
| 22 | 42 | | task = newTask; |
| 22 | 43 | | taskTitle.text = task.name; |
| 22 | 44 | | jumpInDelegate = () => WebInterface.SendChatMessage(new ChatMessage |
| | 45 | | { |
| | 46 | | messageType = ChatMessage.Type.NONE, |
| | 47 | | recipient = string.Empty, |
| | 48 | | body = $"/goto {task.coordinates}", |
| | 49 | | }); |
| 22 | 50 | | Vector3 scale = progress.transform.localScale; |
| 22 | 51 | | scale.x = newTask.oldProgress; |
| 22 | 52 | | progress.transform.localScale = scale; |
| 22 | 53 | | jumpInButton.gameObject.SetActive(task.progress < 1 && !string.IsNullOrEmpty(task.coordinates)); |
| 22 | 54 | | progressTarget = task.progress; |
| 22 | 55 | | switch (task.type) |
| | 56 | | { |
| | 57 | | case "single": |
| 12 | 58 | | SetProgressText(task.progress, 1); |
| 12 | 59 | | break; |
| | 60 | | case "numeric": |
| | 61 | | case "step-based": |
| 10 | 62 | | var payload = JsonUtility.FromJson<TaskPayload_Numeric>(task.payload); |
| 10 | 63 | | SetProgressText(payload.current, payload.end); |
| | 64 | | break; |
| | 65 | | } |
| 10 | 66 | | } |
| | 67 | |
|
| | 68 | | public IEnumerator ProgressAndCompleteSequence() |
| | 69 | | { |
| 0 | 70 | | Vector3 scale = progress.transform.localScale; |
| 0 | 71 | | if (Math.Abs(scale.x - progressTarget) > Mathf.Epsilon) |
| 0 | 72 | | progressBarIncrementAudioEvent.Play(); |
| 0 | 73 | | while (Math.Abs(scale.x - progressTarget) > Mathf.Epsilon) |
| | 74 | | { |
| 0 | 75 | | scale.x = Mathf.MoveTowards(scale.x, progressTarget, Time.deltaTime); |
| 0 | 76 | | progress.transform.localScale = scale; |
| 0 | 77 | | yield return null; |
| | 78 | | } |
| 0 | 79 | | if (progressTarget < 1) |
| 0 | 80 | | yield break; |
| | 81 | |
|
| | 82 | | //Dont play completed animation if is already playing |
| 0 | 83 | | if (animator.GetCurrentAnimatorClipInfo(0).All(x => x.clip.name != TASK_COMPLETED_ANIMATION_NAME)) |
| | 84 | | { |
| 0 | 85 | | yield return WaitForSecondsCache.Get(0.5f); |
| 0 | 86 | | animator.SetTrigger(COMPLETED_ANIMATOR_TRIGGER); |
| 0 | 87 | | taskCompleteAudioEvent.Play(); |
| 0 | 88 | | yield return null; // Wait for the animator to update its clipInfo |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | yield return new WaitUntil(() => animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.95f); |
| | 92 | |
|
| 0 | 93 | | OnDestroyed?.Invoke(task.id); |
| 0 | 94 | | Destroy(gameObject); |
| 0 | 95 | | } |
| | 96 | |
|
| 44 | 97 | | internal void SetProgressText(float current, float end) { progressText.text = $"{current}/{end}"; } |
| | 98 | |
|
| | 99 | | public void SetExpandedStatus(bool active) |
| | 100 | | { |
| 18 | 101 | | if (active) |
| 18 | 102 | | animator.SetTrigger(EXPAND_ANIMATOR_TRIGGER); |
| | 103 | | else |
| 0 | 104 | | animator.SetTrigger(COLLAPSE_ANIMATOR_TRIGGER); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | } |
| | 108 | | } |