using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class QuestUINew : MonoBehaviour { public bool Opened = true; [Tooltip("The Text component holding the quest's name.")] public Text Name; [Tooltip("The window that contains all the tasks and their holder.")] public RectTransform TasksWindow; [Tooltip("The parent of the tasks, should have ContentSizeFitter to fit all the tasks inside it.")] public RectTransform TasksHolder; [Tooltip("The prefab of the task template to be instatiated and edited for each task inside this quest.")] public GameObject TaskTemplate; [Tooltip("The name of the quest.")] public string QuestName; private string QuestDescription; [Header("Settings")] [Tooltip("The time in seconds the window will take to open/close.")] public float SwitchingDuration = 0.5f; private IEnumerator switchingEnum; public List tasksUI = new List(); void Start() { if (Opened) { //Make sure the UI is set to opened state TasksWindow.offsetMin = TasksHolder.offsetMin; TasksWindow.offsetMax = TasksHolder.offsetMax; } else { //Make sure the UI is set to closed state TasksWindow.offsetMin = new Vector2(0, 0); TasksWindow.offsetMax = new Vector2(0, 0); } } /// /// Update quest's UI with the it's data. /// public void Init(Quest quest,int index) { StartCoroutine(Initializing(quest,index)); } IEnumerator Initializing(Quest quest,int index) { QuestName = quest.id; QuestDescription = quest.Description; Name.text = QuestDescription; for (int i = 0; i < quest.tasks.Length; i++) { GameObject t = Instantiate(TaskTemplate, TasksHolder); TaskUINew tUI = t.GetComponent(); tasksUI.Add(tUI); } //Wait for half a second before changing size to TasksHolder (because it takes few frames for the two ContentSizeFitters used to adapt to the missions size). yield return new WaitForSeconds(0.25f); Open(); yield break; } public void SwitchState() { if (Opened) { Close(); } else { Open(); } } public void Open() { if (switchingEnum != null) { StopCoroutine(switchingEnum); } switchingEnum = SwitchAnimation(true); StartCoroutine(switchingEnum); Opened = true; } public void Close() { if (switchingEnum != null) { StopCoroutine(switchingEnum); } switchingEnum = SwitchAnimation(false); StartCoroutine(switchingEnum); Opened = false; } IEnumerator SwitchAnimation(bool open) { float startTime = Time.time; Vector2 startOffsetMin = TasksWindow.offsetMin; Vector2 startOffsetMax = TasksWindow.offsetMax; Vector2 wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ; Vector2 wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ; while (Time.time < startTime + SwitchingDuration + 1) { float t = (Time.time - startTime) / SwitchingDuration; wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ; wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ; TasksWindow.offsetMin = Vector2.Lerp(startOffsetMin, wantedOffsetMin, t); TasksWindow.offsetMax = Vector2.Lerp(startOffsetMax, wantedOffsetMax, t); yield return null; } wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ; wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ; TasksWindow.offsetMin = wantedOffsetMin; TasksWindow.offsetMax = wantedOffsetMax; GetComponent().MovementSection.startVectorValue = GetComponent().position; } public void CheckTask(int index) { if (index == tasksUI.Count - 1) { Invoke("Hide", 1); } } public void Hide() { Close(); GetComponent().ChangeVisibility(false); Invoke("Remove", 0.5f); } private void Remove() { InGameNotificationManager.Instance.PopNotification("Quest Completed!", QuestDescription); Destroy(gameObject); } }