123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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<TaskUINew> tasksUI = new List<TaskUINew>();
- 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);
- }
- }
- /// <summary>
- /// Update quest's UI with the it's data.
- /// </summary>
- 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<TaskUINew>();
- 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<UIElement>().MovementSection.startVectorValue = GetComponent<RectTransform>().position;
- }
- public void CheckTask(int index)
- {
- if (index == tasksUI.Count - 1)
- {
- Invoke("Hide", 1);
- }
- }
- public void Hide()
- {
- Close();
- GetComponent<UIElement>().ChangeVisibility(false);
- Invoke("Remove", 0.5f);
- }
- private void Remove()
- {
- InGameNotificationManager.Instance.PopNotification("Quest Completed!", QuestDescription);
- Destroy(gameObject);
- }
- }
|