using SimpleFirebaseUnity; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class QuestContainerBehaviour : MonoBehaviour { [Header("User data")] [SerializeField] private DataTools.DataUser dataUser; [Header("Task item")] [SerializeField] private GameObject templateItem; [Header("Task container")] [SerializeField] private GameObject container; [Header("Tasks list")] [SerializeField] private List tasks = new List(); [Header("Button block")] [SerializeField] private GameObject buttonPosShown; [SerializeField] private GameObject buttonPosHidden; [SerializeField] private Button showHideButton; [Header("Header")] [SerializeField] private GameObject header; [SerializeField] private Text questName; [Header("Additional settings")] [SerializeField] private int maxNumOfVisibleTasks = 3; private DataTools.Quest currentQuest; private bool isHidden = false; private float headerWidth = 0; private bool questComplete = true; private int updateFrequency = 28; private int frameCounter = 0; private void Start() { headerWidth = header.GetComponent().GetWidth(); } public int LastActive { get; set; } public void SetQuest(DataTools.Quest quest) { RemoveQuest(); currentQuest = quest; questComplete = false; Setup(); } private void Clear() { foreach(Transform tr in container.transform.GetChilds()) { tr.SetParent(null); Destroy(tr.gameObject); } tasks.Clear(); } private void Setup() { questName.text = currentQuest.Description; if(dataUser.UserData.ProgressData.QuestId != currentQuest.Id) { dataUser.UserData.ProgressData.QuestId = currentQuest.Id; dataUser.UpdateQuestStartTime(); dataUser.UpdateUserProgress(); } DateTime time = FirebaseDatabase.TimeStampToDateTime(dataUser.UserData.TimeData.QuestStartTime); foreach(DataTools.Task task in currentQuest.Tasks) { GameObject item = (GameObject)Instantiate(templateItem); TaskUINew taskItemInstance = item.GetComponent(); taskItemInstance.SetDescriptionText(task.Description); taskItemInstance.SetTaskId(task.Action.PropertyId); taskItemInstance.durationInHours = task.Duration; taskItemInstance.questStartTime = time; taskItemInstance.hiddenTask = task.IsHidden; tasks.Add(taskItemInstance); item.SetActive(false); item.transform.SetParent(container.transform); item.transform.localScale = Vector3.one * item.transform.lossyScale.x; item.transform.localPosition = Vector3.zero; } } public void SetTaskActive(int taskIndex) { if(taskIndex > -1 && taskIndex < tasks.Count) { tasks[taskIndex].gameObject.SetActive(true); tasks[taskIndex].questStartTime = FirebaseDatabase.TimeStampToDateTime(dataUser.UserData.TimeData.QuestStartTime); LastActive = taskIndex; if (tasks[taskIndex].hiddenTask && tasks[taskIndex].durationInHours == 0) { SetTaskActive(taskIndex + 1); } } } public void SetTaskComplete(int taskIndex) { if(taskIndex > -1 && taskIndex < tasks.Count) { tasks[taskIndex].SetCheckActive(true); questComplete = taskIndex == tasks.Count - 1; } } public void DisableTimerOnTask(int taskIndex = -1) { if (taskIndex == -1) { taskIndex = tasks.FindLastIndex(t => t.gameObject.activeSelf); } tasks[taskIndex].durationInHours = 0; } public void SetTaskActive(string actionId) { int index = tasks.FindIndex(t => t.GetTaskId() == actionId); SetTaskActive(index); } public void SetTaskComplete(string actionId) { int index = tasks.FindIndex(t => t.GetTaskId() == actionId); SetTaskComplete(index); } private void Update() { if(frameCounter == updateFrequency) { foreach (TaskUINew task in tasks) { task.CallUpdate(); } frameCounter = 0; } else { frameCounter++; } } public void Hide() { if(!isHidden) { isHidden = true; container.SetActive(false); questName.gameObject.SetActive(false); showHideButton.GetComponent().anchoredPosition = buttonPosHidden.GetComponent().anchoredPosition; showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, 90); header.GetComponent().SetWidth(headerWidth / 2); } } public void Show() { if(isHidden) { isHidden = false; container.SetActive(true); questName.gameObject.SetActive(true); showHideButton.GetComponent().anchoredPosition = buttonPosShown.GetComponent().anchoredPosition; showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, -90); header.GetComponent().SetWidth(headerWidth); } } public bool IsQuestComplete { get { return questComplete; } } public void SwitchVisibity() { if (isHidden) { Show(); } else { Hide(); } } public void RemoveQuest() { Clear(); currentQuest = null; } public void SetFakeTask(string task, string title = "", bool hidden = true) { tasks.Clear(); GameObject item = (GameObject)Instantiate(templateItem); TaskUINew taskItemInstance = item.GetComponent(); taskItemInstance.SetDescriptionText(task); taskItemInstance.SetTaskId("FakeTask"); taskItemInstance.durationInHours = 0; tasks.Add(taskItemInstance); item.SetActive(true); questName.text = title; item.transform.SetParent(container.transform); item.transform.localScale = Vector3.one * item.transform.lossyScale.x; item.transform.localPosition = Vector3.zero; } public void CompleteFakeTask() { SetTaskComplete("FakeTask"); } public void UpdateTasksCheckForLinearQuests(int lastActive) { int count = tasks != null ? tasks.Count : -1; int active = 1; int start = lastActive > 0 ? (lastActive <= count - 2 ? lastActive - 1 : count - 2) : 0; if(count > 1) { for(int i = start; i >= 0; i--) { bool nextActive = tasks[i + 1].gameObject.activeSelf; bool enoughTasks = active == maxNumOfVisibleTasks; if (nextActive && !enoughTasks) { tasks[i].gameObject.SetActive(true); tasks[i].SetCheckActive(true); active++; } else { tasks[i].gameObject.SetActive(false); } } } if (start == 0) { tasks[0].gameObject.SetActive(true); } } public void UpdateTasksForLinearQuestsIncludingHiddenFlag(int lastActive) { int count = tasks != null ? tasks.Count : -1; if (lastActive > count) { return; } if(count > 1) { int active = 0; int maxIndex = lastActive >= count - 1 ? lastActive : lastActive + 1; if (lastActive == 0 ) { if (maxIndex + 1 < count) { maxIndex++; } } for (int i = maxIndex; i >= 0; i--) { tasks[i].durationInHours = 0; if (!tasks[i].hiddenTask && i >= lastActive) { tasks[i].gameObject.SetActive(true); active++; } else if (!tasks[i].hiddenTask && (active < maxNumOfVisibleTasks)) { tasks[i].gameObject.SetActive(true); tasks[i].SetCheckActive(true); active++; } else { tasks[i].gameObject.SetActive(false); } } } } public int NumOfVisibleTasks { get { int visible = 0; foreach(TaskUINew task in tasks) { visible += task.gameObject.activeSelf && !(task.hiddenTask && task.TaskWaitingRemaining() <= 0.0) ? 1 : 0; } return visible; } } public void ShowNearestNonhiddenTask(int currentQuestIndex) { currentQuestIndex %= currentQuest.Tasks.Count; while (currentQuest.Tasks[currentQuestIndex].IsHidden && currentQuestIndex < currentQuest.Tasks.Count - 1) { currentQuestIndex++; } for (int i = 0; i <= currentQuestIndex; i++) { tasks[i].gameObject.SetActive(!currentQuest.Tasks[currentQuestIndex].IsHidden); } } public bool ContainsFakeTask { get { return (tasks.FindIndex(t => t.GetTaskId() == "FakeTask") != -1); } } public bool IsAnyToShow { get { return ((tasks != null) && (tasks.Count > 0)); } } }