Advertisement
evelynshilosky

QuestManager - Part 31

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