Advertisement
evelynshilosky

QuestManager

Aug 29th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class QuestManager : MonoBehaviour
  5. {
  6.     public static QuestManager Instance;
  7.     public List<Quest> allQuests = new List<Quest>(); // All available quests in the game
  8.     public List<Quest> activeQuests = new List<Quest>();
  9.     public List<Quest> completedQuests = new List<Quest>();
  10.  
  11.     private void Awake()
  12.     {
  13.         if (Instance == null)
  14.         {
  15.             Instance = this;
  16.             DontDestroyOnLoad(gameObject);
  17.         }
  18.         else
  19.         {
  20.             Destroy(gameObject);
  21.         }
  22.     }
  23.  
  24.     public void AddActiveQuest(Quest quest)
  25.     {
  26.         activeQuests.Add(quest);
  27.     }
  28.  
  29.     public void MarkQuestCompleted(Quest quest)
  30.     {
  31.         activeQuests.Remove(quest);
  32.         completedQuests.Add(quest);
  33.     }
  34.  
  35.     public QuestSaveData GetSaveData()
  36.     {
  37.         QuestSaveData data = new QuestSaveData();
  38.         data.activeQuestIds = activeQuests.ConvertAll(q => q.questId);
  39.         data.completedQuestIds = completedQuests.ConvertAll(q => q.questId);
  40.         return data;
  41.     }
  42.  
  43.     public void LoadSaveData(QuestSaveData data)
  44.     {
  45.         activeQuests.Clear();
  46.         completedQuests.Clear();
  47.  
  48.         foreach (string questId in data.activeQuestIds)
  49.         {
  50.             Quest quest = allQuests.Find(q => q.questId == questId);
  51.             if (quest != null)
  52.             {
  53.                 activeQuests.Add(quest);
  54.             }
  55.         }
  56.  
  57.         foreach (string questId in data.completedQuestIds)
  58.         {
  59.             Quest quest = allQuests.Find(q => q.questId == questId);
  60.             if (quest != null)
  61.             {
  62.                 completedQuests.Add(quest);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. [System.Serializable]
  69. public class QuestSaveData
  70. {
  71.     public List<string> activeQuestIds;
  72.     public List<string> completedQuestIds;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement