QuestUINew.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class QuestUINew : MonoBehaviour
  6. {
  7. public bool Opened = true;
  8. [Tooltip("The Text component holding the quest's name.")]
  9. public Text Name;
  10. [Tooltip("The window that contains all the tasks and their holder.")]
  11. public RectTransform TasksWindow;
  12. [Tooltip("The parent of the tasks, should have ContentSizeFitter to fit all the tasks inside it.")]
  13. public RectTransform TasksHolder;
  14. [Tooltip("The prefab of the task template to be instatiated and edited for each task inside this quest.")]
  15. public GameObject TaskTemplate;
  16. [Tooltip("The name of the quest.")]
  17. public string QuestName;
  18. private string QuestDescription;
  19. [Header("Settings")]
  20. [Tooltip("The time in seconds the window will take to open/close.")]
  21. public float SwitchingDuration = 0.5f;
  22. private IEnumerator switchingEnum;
  23. public List<TaskUINew> tasksUI = new List<TaskUINew>();
  24. void Start()
  25. {
  26. if (Opened)
  27. {
  28. //Make sure the UI is set to opened state
  29. TasksWindow.offsetMin = TasksHolder.offsetMin;
  30. TasksWindow.offsetMax = TasksHolder.offsetMax;
  31. }
  32. else
  33. {
  34. //Make sure the UI is set to closed state
  35. TasksWindow.offsetMin = new Vector2(0, 0);
  36. TasksWindow.offsetMax = new Vector2(0, 0);
  37. }
  38. }
  39. /// <summary>
  40. /// Update quest's UI with the it's data.
  41. /// </summary>
  42. public void Init(Quest quest,int index)
  43. {
  44. StartCoroutine(Initializing(quest,index));
  45. }
  46. IEnumerator Initializing(Quest quest,int index)
  47. {
  48. QuestName = quest.id;
  49. QuestDescription = quest.Description;
  50. Name.text = QuestDescription;
  51. for (int i = 0; i < quest.tasks.Length; i++)
  52. {
  53. GameObject t = Instantiate(TaskTemplate, TasksHolder);
  54. TaskUINew tUI = t.GetComponent<TaskUINew>();
  55. tasksUI.Add(tUI);
  56. }
  57. //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).
  58. yield return new WaitForSeconds(0.25f);
  59. Open();
  60. yield break;
  61. }
  62. public void SwitchState()
  63. {
  64. if (Opened)
  65. {
  66. Close();
  67. }
  68. else
  69. {
  70. Open();
  71. }
  72. }
  73. public void Open()
  74. {
  75. if (switchingEnum != null)
  76. {
  77. StopCoroutine(switchingEnum);
  78. }
  79. switchingEnum = SwitchAnimation(true);
  80. StartCoroutine(switchingEnum);
  81. Opened = true;
  82. }
  83. public void Close()
  84. {
  85. if (switchingEnum != null)
  86. {
  87. StopCoroutine(switchingEnum);
  88. }
  89. switchingEnum = SwitchAnimation(false);
  90. StartCoroutine(switchingEnum);
  91. Opened = false;
  92. }
  93. IEnumerator SwitchAnimation(bool open)
  94. {
  95. float startTime = Time.time;
  96. Vector2 startOffsetMin = TasksWindow.offsetMin;
  97. Vector2 startOffsetMax = TasksWindow.offsetMax;
  98. Vector2 wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ;
  99. Vector2 wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ;
  100. while (Time.time < startTime + SwitchingDuration + 1)
  101. {
  102. float t = (Time.time - startTime) / SwitchingDuration;
  103. wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ;
  104. wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ;
  105. TasksWindow.offsetMin = Vector2.Lerp(startOffsetMin, wantedOffsetMin, t);
  106. TasksWindow.offsetMax = Vector2.Lerp(startOffsetMax, wantedOffsetMax, t);
  107. yield return null;
  108. }
  109. wantedOffsetMin = open ? TasksHolder.offsetMin : new Vector2(0, 0); ;
  110. wantedOffsetMax = open ? TasksHolder.offsetMax : new Vector2(0, 0); ;
  111. TasksWindow.offsetMin = wantedOffsetMin;
  112. TasksWindow.offsetMax = wantedOffsetMax;
  113. GetComponent<UIElement>().MovementSection.startVectorValue = GetComponent<RectTransform>().position;
  114. }
  115. public void CheckTask(int index)
  116. {
  117. if (index == tasksUI.Count - 1)
  118. {
  119. Invoke("Hide", 1);
  120. }
  121. }
  122. public void Hide()
  123. {
  124. Close();
  125. GetComponent<UIElement>().ChangeVisibility(false);
  126. Invoke("Remove", 0.5f);
  127. }
  128. private void Remove()
  129. {
  130. InGameNotificationManager.Instance.PopNotification("Quest Completed!", QuestDescription);
  131. Destroy(gameObject);
  132. }
  133. }