123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- 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<TaskUINew> tasks = new List<TaskUINew>();
- [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<RectTransform>().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<TaskUINew>();
- 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<RectTransform>().anchoredPosition = buttonPosHidden.GetComponent<RectTransform>().anchoredPosition;
- showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, 90);
- header.GetComponent<RectTransform>().SetWidth(headerWidth / 2);
- }
- }
- public void Show()
- {
- if(isHidden)
- {
- isHidden = false;
- container.SetActive(true);
- questName.gameObject.SetActive(true);
- showHideButton.GetComponent<RectTransform>().anchoredPosition = buttonPosShown.GetComponent<RectTransform>().anchoredPosition;
- showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, -90);
- header.GetComponent<RectTransform>().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<TaskUINew>();
- 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));
- }
- }
- }
|