using System; using System.Collections.Generic; using System.Linq; using Terraria.ModLoader; using Terraria.ModLoader.IO; namespace CalamityMod.Systems.Quests { /// /// An that implements the foundation to make quests in Terraria
/// and making their data be saved and loaded accordingly. ///
/// A which contains all the progression points of the quest public abstract class Quest : ModSystem where TEnum : Enum { #region Data Saving & Loading public override void Unload() => QuestProgression = null; public override void ClearWorld() { // Resets all the properties when loading a world without a TagCompound. QuestStarted = false; QuestFinished = false; QuestProgression = ResetQuestProgression(); } public override void LoadWorldData(TagCompound tag) { QuestStarted = tag.ContainsKey("questStarted"); QuestFinished = tag.ContainsKey("questFinished"); QuestProgression = ResetQuestProgression(); // Although this method may seem weird, it's endorsed by tML on their guides for save & loading dictionaries. if (tag.TryGet>("questProgression", out var value)) QuestProgression = QuestProgression.Keys.Zip(value, (k, v) => new { Key = k, Value = v }).ToDictionary(x => x.Key, x => x.Value); } public override void SaveWorldData(TagCompound tag) { // If any of them have modified values from the default value, we save them on the TagCompound. if (QuestStarted) tag.Add("questStarted", QuestStarted); if (QuestFinished) tag.Add("questFinished", QuestFinished); if (QuestProgression.ContainsValue(true)) tag.Add("questProgression", QuestProgression.Values.ToList()); } #endregion /// /// Whether or not this quest has started. /// public static bool QuestStarted { get; private set; } /// /// Whether or not this quest has finished. /// public static bool QuestFinished { get; private set; } /// /// A dictionary that saves a boolean for each progression point, to state which one was completed and which one was not. /// public static Dictionary QuestProgression { get; private set; } = ResetQuestProgression(); /// /// An event that is triggered when this quest starts. /// public static event Action OnQuestStarted; /// /// An event that is triggered when this quest finishes. /// public static event Action OnQuestFinish; /// /// An event that is triggered when this quest completes a new progression point. /// public static event Action OnQuestProgression; /// /// A method that makes this quest start and trigger its corresponding event.
/// Does nothing if the quest has already started. ///
public static void StartQuest() { if (!QuestStarted) { QuestStarted = true; OnQuestStarted?.Invoke(); } } /// /// A method that makes this quest finishes and trigger its corresponding event.
/// Does nothing if the quest has already finished. ///
public static void FinishQuest() { if (!QuestFinished) { QuestFinished = true; OnQuestFinish?.Invoke(); } } /// /// A method that makes a progression point be counted as completed.
/// Does nothing if the progression point has already been completed. ///
/// The progression point that has been completed. public static void ProgressQuest(TEnum progressionPoint) { if (!QuestProgression[progressionPoint]) { QuestProgression[progressionPoint] = true; OnQuestProgression?.Invoke(progressionPoint); } } /// /// A quick method to resets the progression of Amidias' Quest.
/// Used for when changing worlds or initializing . ///
private static Dictionary ResetQuestProgression() { Dictionary questProgression = new(); var progressionPoints = Enum.GetValues(typeof(TEnum)); foreach (TEnum progressionPoint in progressionPoints) questProgression.Add(progressionPoint, false); return questProgression; } } }