// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System.Collections.Generic; namespace PixelCrushers.QuestMachine { /// /// Abstract base class for quest subassets (ScriptableObjects) such as QuestCondition, /// QuestAction, and QuestContent. Adds references to a Quest and QuestNode, and /// handles serialization and instance management. /// public abstract class QuestSubasset : ScriptableObject, IProxySerializationCallbackReceiver { /// /// (Runtime) The quest that this condition belongs to. /// public Quest quest { get; protected set; } /// /// (Runtime) The quest node that this condition belongs to. /// public QuestNode questNode { get; protected set; } /// /// (Runtime) The quest's tag dictionary. /// protected TagDictionary tagDictionary { get { return (quest != null) ? quest.tagDictionary : null; } } /// /// Returns the name to show in the editor for this subasset. /// public virtual string GetEditorName() { return GetType().Name; } /// /// Sets quest references, as some subassets might need to refer to their containing /// quest and/or quest node. /// public virtual void SetRuntimeReferences(Quest quest, QuestNode questNode) { this.quest = quest; this.questNode = questNode; AddTagsToDictionary(); } /// /// Records the static tags used in this asset's text content into the quest's /// staticTags property. /// public virtual void AddTagsToDictionary() { } /// /// Adds any tags in the StringField to the tags dictionary. /// /// protected virtual void AddTagsToDictionary(StringField stringField) { AddTagsToDictionary(StringField.GetStringValue(stringField)); } /// /// Adds any tags in the string to the tags dictionary. /// /// protected virtual void AddTagsToDictionary(string s) { QuestMachineTags.AddTagsToDictionary(tagDictionary, s); } /// /// Override to return any images that this content references. /// /// Array of images referenced by this content. public virtual Sprite[] GetImages() { return null; } /// /// Overide to return any audio clips that this content references. /// /// Array of audio clips referenced by this content. public virtual AudioClip[] GetAudioClips() { return null; } /// /// Allows a subasset to save information in a serializable format prior to being /// serialized to a proxy object for saving. The base method doesn't do anything, /// but subclasses may need to. /// public virtual void OnBeforeProxySerialization() { } /// /// Applies saved information from a proxy object. The base method doesn't do anything, /// but subclasses may need to. /// public virtual void OnAfterProxyDeserialization() { } /// /// Allows subclasses to deep copy their own subassets by instantiating copies. /// /// The copy to instantiate subasset copies into. Assumes the /// copy has already been instantiated and contains an accurate copy of everything /// except subassets. public virtual void CloneSubassetsInto(QuestSubasset copy) { } /// /// Allows subclasses to destroy their subassets, usually when a runtime instance /// of a quest is being destroyed. /// public virtual void DestroySubassets() { } /// /// Returns a deep copy of a QuestSubasset list. /// public static List CloneList(List original) where T : QuestSubasset { if (original == null) return null; var copy = ScriptableObjectUtility.CloneList(original); for (int i = 0; i < original.Count; i++) { if (original[i] != null) { original[i].CloneSubassetsInto(copy[i]); } else { //--- No need to worry anyone. This is harmless. //if (Debug.isDebugBuild) Debug.LogWarning("Quest Machine: QuestSubasset.CloneList<" + typeof(T).Name + ">: Element " + i + " is null."); } } return copy; } public static void DestroyList(List list) where T : QuestSubasset { if (list == null) return; for (int i = 0; i < list.Count; i++) { var subasset = list[i]; if (subasset == null) continue; subasset.DestroySubassets(); Destroy(subasset); } } } }