| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Huds.QuestsTracker; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Huds.QuestsTracker |
| | 7 | | { |
| | 8 | | public class QuestsNotificationsController : MonoBehaviour |
| | 9 | | { |
| 1 | 10 | | internal static float NOTIFICATIONS_SEPARATION { get; set; } = 0.5f; |
| 1 | 11 | | public static float DEFAULT_NOTIFICATION_DURATION { get; set; } = 2.5f; |
| | 12 | |
|
| 15 | 13 | | internal readonly Queue<IQuestNotification> notificationsQueue = new Queue<IQuestNotification>(); |
| | 14 | | private IQuestNotification currentNotification; |
| | 15 | |
|
| | 16 | | [SerializeField] private GameObject questCompletedPrefab; |
| | 17 | | [SerializeField] private GameObject rewardObtainedPrefab; |
| | 18 | | private bool isDestroyed = false; |
| | 19 | |
|
| | 20 | | internal static QuestsNotificationsController Create() |
| | 21 | | { |
| 0 | 22 | | QuestsNotificationsController view = Instantiate(Resources.Load<GameObject>("QuestsNotificationsHUD")).GetCo |
| | 23 | | #if UNITY_EDITOR |
| 0 | 24 | | view.gameObject.name = "_QuestsNotificationsHUDView"; |
| | 25 | | #endif |
| 0 | 26 | | return view; |
| | 27 | | } |
| | 28 | |
|
| 28 | 29 | | private void Awake() { StartCoroutine(ProcessSectionsNotificationQueue()); } |
| | 30 | |
|
| | 31 | | public void ShowQuestCompleted(QuestModel quest) |
| | 32 | | { |
| 3 | 33 | | var questNotification = Instantiate(questCompletedPrefab, transform).GetComponent<QuestNotification_QuestCom |
| 3 | 34 | | questNotification.Populate(quest); |
| 3 | 35 | | questNotification.gameObject.SetActive(false); |
| 3 | 36 | | notificationsQueue.Enqueue(questNotification); |
| 3 | 37 | | } |
| | 38 | |
|
| | 39 | | public void ShowRewardObtained(QuestReward reward) |
| | 40 | | { |
| 0 | 41 | | var questNotification = Instantiate(rewardObtainedPrefab, transform).GetComponent<QuestNotification_RewardOb |
| 0 | 42 | | questNotification.Populate(reward); |
| 0 | 43 | | questNotification.gameObject.SetActive(false); |
| 0 | 44 | | notificationsQueue.Enqueue(questNotification); |
| 0 | 45 | | } |
| | 46 | |
|
| 0 | 47 | | public void SetVisibility(bool visible) { gameObject.SetActive(visible); } |
| | 48 | |
|
| | 49 | | public void Dispose() |
| | 50 | | { |
| 0 | 51 | | if (isDestroyed) |
| 0 | 52 | | return; |
| | 53 | |
|
| 0 | 54 | | Destroy(gameObject); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void ClearAllNotifications() |
| | 58 | | { |
| 30 | 59 | | foreach ( var noti in notificationsQueue ) |
| | 60 | | { |
| 1 | 61 | | noti.Dispose(); |
| | 62 | | } |
| | 63 | |
|
| 14 | 64 | | if ( currentNotification != null ) |
| 0 | 65 | | currentNotification.Dispose(); |
| 14 | 66 | | } |
| | 67 | |
|
| | 68 | | private void OnDestroy() |
| | 69 | | { |
| 14 | 70 | | ClearAllNotifications(); |
| 14 | 71 | | isDestroyed = true; |
| 14 | 72 | | } |
| | 73 | |
|
| | 74 | | private IEnumerator ProcessSectionsNotificationQueue() |
| | 75 | | { |
| 16 | 76 | | while (true) |
| | 77 | | { |
| 30 | 78 | | if (notificationsQueue.Count > 0) |
| | 79 | | { |
| 2 | 80 | | IQuestNotification notification = notificationsQueue.Dequeue(); |
| 2 | 81 | | currentNotification = notification; |
| 2 | 82 | | notification.Show(); |
| 2 | 83 | | yield return notification.Waiter(); |
| 2 | 84 | | notification.Dispose(); |
| 2 | 85 | | currentNotification = null; |
| 2 | 86 | | } |
| | 87 | |
|
| 30 | 88 | | yield return WaitForSecondsCache.Get(NOTIFICATIONS_SEPARATION); |
| | 89 | | } |
| | 90 | | } |
| | 91 | | } |
| | 92 | | } |