QuestContainerBehaviour.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. using SimpleFirebaseUnity;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class QuestContainerBehaviour : MonoBehaviour
  7. {
  8. [Header("User data")]
  9. [SerializeField]
  10. private DataTools.DataUser dataUser;
  11. [Header("Task item")]
  12. [SerializeField]
  13. private GameObject templateItem;
  14. [Header("Task container")]
  15. [SerializeField]
  16. private GameObject container;
  17. [Header("Tasks list")]
  18. [SerializeField]
  19. private List<TaskUINew> tasks = new List<TaskUINew>();
  20. [Header("Button block")]
  21. [SerializeField]
  22. private GameObject buttonPosShown;
  23. [SerializeField]
  24. private GameObject buttonPosHidden;
  25. [SerializeField]
  26. private Button showHideButton;
  27. [Header("Header")]
  28. [SerializeField]
  29. private GameObject header;
  30. [SerializeField]
  31. private Text questName;
  32. [Header("Additional settings")]
  33. [SerializeField]
  34. private int maxNumOfVisibleTasks = 3;
  35. private DataTools.Quest currentQuest;
  36. private bool isHidden = false;
  37. private float headerWidth = 0;
  38. private bool questComplete = true;
  39. private int updateFrequency = 28;
  40. private int frameCounter = 0;
  41. private void Start()
  42. {
  43. headerWidth = header.GetComponent<RectTransform>().GetWidth();
  44. }
  45. public int LastActive
  46. {
  47. get;
  48. set;
  49. }
  50. public void SetQuest(DataTools.Quest quest)
  51. {
  52. RemoveQuest();
  53. currentQuest = quest;
  54. questComplete = false;
  55. Setup();
  56. }
  57. private void Clear()
  58. {
  59. foreach(Transform tr in container.transform.GetChilds())
  60. {
  61. tr.SetParent(null);
  62. Destroy(tr.gameObject);
  63. }
  64. tasks.Clear();
  65. }
  66. private void Setup()
  67. {
  68. questName.text = currentQuest.Description;
  69. if(dataUser.UserData.ProgressData.QuestId != currentQuest.Id)
  70. {
  71. dataUser.UserData.ProgressData.QuestId = currentQuest.Id;
  72. dataUser.UpdateQuestStartTime();
  73. dataUser.UpdateUserProgress();
  74. }
  75. DateTime time = FirebaseDatabase.TimeStampToDateTime(dataUser.UserData.TimeData.QuestStartTime);
  76. foreach(DataTools.Task task in currentQuest.Tasks)
  77. {
  78. GameObject item = (GameObject)Instantiate(templateItem);
  79. TaskUINew taskItemInstance = item.GetComponent<TaskUINew>();
  80. taskItemInstance.SetDescriptionText(task.Description);
  81. taskItemInstance.SetTaskId(task.Action.PropertyId);
  82. taskItemInstance.durationInHours = task.Duration;
  83. taskItemInstance.questStartTime = time;
  84. taskItemInstance.hiddenTask = task.IsHidden;
  85. tasks.Add(taskItemInstance);
  86. item.SetActive(false);
  87. item.transform.SetParent(container.transform);
  88. item.transform.localScale = Vector3.one * item.transform.lossyScale.x;
  89. item.transform.localPosition = Vector3.zero;
  90. }
  91. }
  92. public void SetTaskActive(int taskIndex)
  93. {
  94. if(taskIndex > -1 && taskIndex < tasks.Count)
  95. {
  96. tasks[taskIndex].gameObject.SetActive(true);
  97. tasks[taskIndex].questStartTime = FirebaseDatabase.TimeStampToDateTime(dataUser.UserData.TimeData.QuestStartTime);
  98. LastActive = taskIndex;
  99. if (tasks[taskIndex].hiddenTask && tasks[taskIndex].durationInHours == 0)
  100. {
  101. SetTaskActive(taskIndex + 1);
  102. }
  103. }
  104. }
  105. public void SetTaskComplete(int taskIndex)
  106. {
  107. if(taskIndex > -1 && taskIndex < tasks.Count)
  108. {
  109. tasks[taskIndex].SetCheckActive(true);
  110. questComplete = taskIndex == tasks.Count - 1;
  111. }
  112. }
  113. public void DisableTimerOnTask(int taskIndex = -1)
  114. {
  115. if (taskIndex == -1)
  116. {
  117. taskIndex = tasks.FindLastIndex(t => t.gameObject.activeSelf);
  118. }
  119. tasks[taskIndex].durationInHours = 0;
  120. }
  121. public void SetTaskActive(string actionId)
  122. {
  123. int index = tasks.FindIndex(t => t.GetTaskId() == actionId);
  124. SetTaskActive(index);
  125. }
  126. public void SetTaskComplete(string actionId)
  127. {
  128. int index = tasks.FindIndex(t => t.GetTaskId() == actionId);
  129. SetTaskComplete(index);
  130. }
  131. private void Update()
  132. {
  133. if(frameCounter == updateFrequency)
  134. {
  135. foreach (TaskUINew task in tasks)
  136. {
  137. task.CallUpdate();
  138. }
  139. frameCounter = 0;
  140. }
  141. else
  142. {
  143. frameCounter++;
  144. }
  145. }
  146. public void Hide()
  147. {
  148. if(!isHidden)
  149. {
  150. isHidden = true;
  151. container.SetActive(false);
  152. questName.gameObject.SetActive(false);
  153. showHideButton.GetComponent<RectTransform>().anchoredPosition = buttonPosHidden.GetComponent<RectTransform>().anchoredPosition;
  154. showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, 90);
  155. header.GetComponent<RectTransform>().SetWidth(headerWidth / 2);
  156. }
  157. }
  158. public void Show()
  159. {
  160. if(isHidden)
  161. {
  162. isHidden = false;
  163. container.SetActive(true);
  164. questName.gameObject.SetActive(true);
  165. showHideButton.GetComponent<RectTransform>().anchoredPosition = buttonPosShown.GetComponent<RectTransform>().anchoredPosition;
  166. showHideButton.transform.GetChild(0).transform.Rotate(Vector3.forward, -90);
  167. header.GetComponent<RectTransform>().SetWidth(headerWidth);
  168. }
  169. }
  170. public bool IsQuestComplete
  171. {
  172. get
  173. {
  174. return questComplete;
  175. }
  176. }
  177. public void SwitchVisibity()
  178. {
  179. if (isHidden)
  180. {
  181. Show();
  182. }
  183. else
  184. {
  185. Hide();
  186. }
  187. }
  188. public void RemoveQuest()
  189. {
  190. Clear();
  191. currentQuest = null;
  192. }
  193. public void SetFakeTask(string task, string title = "", bool hidden = true)
  194. {
  195. tasks.Clear();
  196. GameObject item = (GameObject)Instantiate(templateItem);
  197. TaskUINew taskItemInstance = item.GetComponent<TaskUINew>();
  198. taskItemInstance.SetDescriptionText(task);
  199. taskItemInstance.SetTaskId("FakeTask");
  200. taskItemInstance.durationInHours = 0;
  201. tasks.Add(taskItemInstance);
  202. item.SetActive(true);
  203. questName.text = title;
  204. item.transform.SetParent(container.transform);
  205. item.transform.localScale = Vector3.one * item.transform.lossyScale.x;
  206. item.transform.localPosition = Vector3.zero;
  207. }
  208. public void CompleteFakeTask()
  209. {
  210. SetTaskComplete("FakeTask");
  211. }
  212. public void UpdateTasksCheckForLinearQuests(int lastActive)
  213. {
  214. int count = tasks != null ? tasks.Count : -1;
  215. int active = 1;
  216. int start = lastActive > 0 ? (lastActive <= count - 2 ? lastActive - 1 : count - 2) : 0;
  217. if(count > 1)
  218. {
  219. for(int i = start; i >= 0; i--)
  220. {
  221. bool nextActive = tasks[i + 1].gameObject.activeSelf;
  222. bool enoughTasks = active == maxNumOfVisibleTasks;
  223. if (nextActive && !enoughTasks)
  224. {
  225. tasks[i].gameObject.SetActive(true);
  226. tasks[i].SetCheckActive(true);
  227. active++;
  228. }
  229. else
  230. {
  231. tasks[i].gameObject.SetActive(false);
  232. }
  233. }
  234. }
  235. if (start == 0)
  236. {
  237. tasks[0].gameObject.SetActive(true);
  238. }
  239. }
  240. public void UpdateTasksForLinearQuestsIncludingHiddenFlag(int lastActive)
  241. {
  242. int count = tasks != null ? tasks.Count : -1;
  243. if (lastActive > count)
  244. {
  245. return;
  246. }
  247. if(count > 1)
  248. {
  249. int active = 0;
  250. int maxIndex = lastActive >= count - 1 ? lastActive : lastActive + 1;
  251. if (lastActive == 0 )
  252. {
  253. if (maxIndex + 1 < count)
  254. {
  255. maxIndex++;
  256. }
  257. }
  258. for (int i = maxIndex; i >= 0; i--)
  259. {
  260. tasks[i].durationInHours = 0;
  261. if (!tasks[i].hiddenTask && i >= lastActive)
  262. {
  263. tasks[i].gameObject.SetActive(true);
  264. active++;
  265. }
  266. else if (!tasks[i].hiddenTask && (active < maxNumOfVisibleTasks))
  267. {
  268. tasks[i].gameObject.SetActive(true);
  269. tasks[i].SetCheckActive(true);
  270. active++;
  271. }
  272. else
  273. {
  274. tasks[i].gameObject.SetActive(false);
  275. }
  276. }
  277. }
  278. }
  279. public int NumOfVisibleTasks
  280. {
  281. get
  282. {
  283. int visible = 0;
  284. foreach(TaskUINew task in tasks)
  285. {
  286. visible += task.gameObject.activeSelf && !(task.hiddenTask && task.TaskWaitingRemaining() <= 0.0) ? 1 : 0;
  287. }
  288. return visible;
  289. }
  290. }
  291. public void ShowNearestNonhiddenTask(int currentQuestIndex)
  292. {
  293. currentQuestIndex %= currentQuest.Tasks.Count;
  294. while (currentQuest.Tasks[currentQuestIndex].IsHidden && currentQuestIndex < currentQuest.Tasks.Count - 1)
  295. {
  296. currentQuestIndex++;
  297. }
  298. for (int i = 0; i <= currentQuestIndex; i++)
  299. {
  300. tasks[i].gameObject.SetActive(!currentQuest.Tasks[currentQuestIndex].IsHidden);
  301. }
  302. }
  303. public bool ContainsFakeTask
  304. {
  305. get
  306. {
  307. return (tasks.FindIndex(t => t.GetTaskId() == "FakeTask") != -1);
  308. }
  309. }
  310. public bool IsAnyToShow
  311. {
  312. get
  313. {
  314. return ((tasks != null) && (tasks.Count > 0));
  315. }
  316. }
  317. }