Advertisement
evelynshilosky

QuestManager - Part 29

Jan 5th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class QuestManager : MonoBehaviour
  7. {
  8.     public static QuestManager Instance { get; set; }
  9.     private void Awake()
  10.     {
  11.         if (Instance != null && Instance != this)
  12.         {
  13.             Destroy(gameObject);
  14.         }
  15.         else
  16.         {
  17.             Instance = this;
  18.         }
  19.  
  20.     }
  21.  
  22.     public List<Quest> allActiveQuests;
  23.     public List<Quest> allCompletedQuests;
  24.  
  25.     [Header("QuestMenu")]
  26.     public GameObject questMenu;
  27.     public bool isQuestMenuOpen;
  28.  
  29.     public GameObject activeQuestPrefab;
  30.     public GameObject completedQuestPrefab;
  31.  
  32.     public GameObject questMenuContent;
  33.  
  34.     [Header("QuestTracker")]
  35.     public GameObject questTrackerContent;
  36.     public GameObject trackerRowPrefab;
  37.  
  38.     public List<Quest> allTrackedQuests;
  39.  
  40.     public void TrackQuest(Quest quest)
  41.     {
  42.         allTrackedQuests.Add(quest);
  43.         RefreshTrackerList();
  44.     }
  45.  
  46.     public void UnTrackerQuest(Quest quest)
  47.     {
  48.         allTrackedQuests.Remove(quest);
  49.         RefreshTrackerList();
  50.     }
  51.  
  52.     public void RefreshTrackerList()
  53.     {
  54.         // Destroying the previous list
  55.         foreach (Transform child in questTrackerContent.transform)
  56.         {
  57.             Destroy(child.gameObject);
  58.         }
  59.  
  60.         foreach (Quest trackedQuest in allTrackedQuests)
  61.         {
  62.             GameObject trackerPrefab = Instantiate(trackerRowPrefab, Vector3.zero, Quaternion.identity);
  63.             trackerPrefab.transform.SetParent(questTrackerContent.transform, false);
  64.  
  65.             TrackerRow tRow = trackerPrefab.GetComponent<TrackerRow>();
  66.  
  67.             tRow.questName.text = trackedQuest.questName;
  68.             tRow.description.text = trackedQuest.questDescription;
  69.  
  70.             var req1 = trackedQuest.info.firstRequirementItem;
  71.             var req1Amount = trackedQuest.info.firstRequirementAmount;
  72.             var req2 = trackedQuest.info.secondRequirementItem;
  73.             var req2Amount = trackedQuest.info.secondRequirementAmount;
  74.  
  75.  
  76.             if (req2 != "") // if we have 2 requirements
  77.             {
  78.                 tRow.requirements.text = $"{req1} " + InventorySystem.Instance.CheckItemAmount(req1) + "/" + $"{req1Amount}\n" +
  79.                $"{req2} " + InventorySystem.Instance.CheckItemAmount(req2) + "/" + $"{req2Amount}\n";
  80.             }
  81.             else // if we have only one
  82.             {
  83.                 tRow.requirements.text = $"{req1} " + InventorySystem.Instance.CheckItemAmount(req1) + "/" + $"{req1Amount}\n";
  84.  
  85.             }
  86.  
  87.  
  88.         }
  89.  
  90.     }
  91.  
  92.  
  93.  
  94.     private void Update()
  95.     {
  96.         if (Input.GetKeyDown(KeyCode.Q) && !isQuestMenuOpen && !ConstructionManager.Instance.inConstructionMode && !InventorySystem.Instance.isOpen && !CraftingSystem.Instance.isOpen)
  97.         {
  98.  
  99.             questMenu.SetActive(true);
  100.  
  101.             questMenu.GetComponentInChildren<Canvas>().sortingOrder = MenuManager.Instance.SetAsFront();
  102.  
  103.             Cursor.lockState = CursorLockMode.None;
  104.             Cursor.visible = true;
  105.  
  106.             SelectionManager.Instance.DisableSelection();
  107.             SelectionManager.Instance.GetComponent<SelectionManager>().enabled = false;
  108.  
  109.             isQuestMenuOpen = true;
  110.  
  111.         }
  112.         else if (Input.GetKeyDown(KeyCode.Q) && isQuestMenuOpen)
  113.         {
  114.  
  115.             questMenu.SetActive(false);
  116.  
  117.             if(!CraftingSystem.Instance.isOpen || !InventorySystem.Instance.isOpen)
  118.             {
  119.                 Cursor.lockState = CursorLockMode.Locked;
  120.                 Cursor.visible = false;
  121.  
  122.                 SelectionManager.Instance.EnableSelection();
  123.                 SelectionManager.Instance.GetComponent<SelectionManager>().enabled = true;
  124.             }
  125.  
  126.             isQuestMenuOpen = false;
  127.         }
  128.     }
  129.  
  130.  
  131.     public void AddActiveQuest(Quest quest)
  132.     {
  133.         allActiveQuests.Add(quest);
  134.         TrackQuest(quest);
  135.         RefreshQuestList();
  136.     }
  137.  
  138.     public void MarkQuestCompleted(Quest quest)
  139.     {
  140.         // Remove quest from active list
  141.         allActiveQuests.Remove(quest);
  142.         // Add it into the completed list
  143.         allCompletedQuests.Add(quest);
  144.         UnTrackerQuest(quest);
  145.         RefreshQuestList();
  146.     }
  147.  
  148.  
  149.  
  150.     public void RefreshQuestList()
  151.     {
  152.         foreach (Transform child in questMenuContent.transform)
  153.         {
  154.             Destroy(child.gameObject);
  155.         }
  156.  
  157.  
  158.         foreach (Quest activeQuest in allActiveQuests)
  159.         {
  160.  
  161.             GameObject questPrefab = Instantiate(activeQuestPrefab, Vector3.zero, Quaternion.identity);
  162.             questPrefab.transform.SetParent(questMenuContent.transform, false);
  163.  
  164.             QuestRow qRow = questPrefab.GetComponent<QuestRow>();
  165.  
  166.             qRow.thisQuest = activeQuest;
  167.  
  168.             qRow.questName.text = activeQuest.questName;
  169.             qRow.questGiver.text = activeQuest.questGiver;
  170.  
  171.             qRow.isActive = true;
  172.             qRow.isTracking = true;
  173.  
  174.             qRow.coinAmount.text = $"{activeQuest.info.coinReward}";
  175.  
  176.             //qRow.firstReward.sprite = "";
  177.             qRow.firstRewardAmount.text = "";
  178.  
  179.             //qRow.secondReward.sprite = "";
  180.             qRow.secondRewardAmount.text = "";
  181.  
  182.         }
  183.  
  184.         foreach (Quest completedQuest in allCompletedQuests)
  185.         {
  186.             GameObject questPrefab = Instantiate(completedQuestPrefab, Vector3.zero, Quaternion.identity);
  187.             questPrefab.transform.SetParent(questMenuContent.transform, false);
  188.  
  189.             QuestRow qRow = questPrefab.GetComponent<QuestRow>();
  190.  
  191.             qRow.questName.text = completedQuest.questName;
  192.             qRow.questGiver.text = completedQuest.questGiver;
  193.  
  194.             qRow.isActive = false;
  195.             qRow.isTracking = false;
  196.  
  197.             qRow.coinAmount.text = $"{completedQuest.info.coinReward}";
  198.  
  199.             //qRow.firstReward.sprite = "";
  200.             qRow.firstRewardAmount.text = "";
  201.  
  202.             //qRow.secondReward.sprite = "";
  203.             qRow.secondRewardAmount.text = "";
  204.  
  205.         }
  206.     }
  207.  
  208.  
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement