// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using System; using System.Collections.Generic; using UnityEngine; namespace Fungus { /// /// A single line of dialog /// [Serializable] public class Line { [SerializeField] public string name; [SerializeField] public string text; } /// /// Serializable object to store Narrative Lines /// [Serializable] public class NarrativeData { [SerializeField] public List lines; public NarrativeData() { lines = new List(); } } /// /// Controls dialog history /// public class NarrativeLog : MonoBehaviour { NarrativeData history; protected virtual void Awake() { history = new NarrativeData(); } protected virtual void OnEnable() { SaveManagerSignals.OnSaveReset += OnSaveReset; } protected virtual void OnDisable() { SaveManagerSignals.OnSaveReset -= OnSaveReset; } protected virtual void OnSaveReset() { Clear(); } #region Public Methods /// /// Add a line of dialog to the Narrative Log /// /// Character Name /// Narrative Text public void AddLine(string name, string text) { Line line = new Line(); line.name = name; line.text = text; history.lines.Add(line); } /// /// Clear all lines of the narrative log /// Usually used on restart /// public void Clear() { history.lines.Clear(); } /// /// Convert history into Json for saving in SaveData /// /// public string GetJsonHistory() { string jsonText = JsonUtility.ToJson(history, true); return jsonText; } /// /// Show previous lines for display purposes /// /// public string GetPrettyHistory() { string output = ""; for (int i = 0; i < history.lines.Count; i++) { output += "" + history.lines[i].name + "\n"; output += history.lines[i].text + "\n\n"; } return output; } /// /// Load History from Json /// /// public void LoadHistory(string narrativeData) { if (narrativeData == null) { Debug.LogError("Failed to decode History save data item"); return; } history = JsonUtility.FromJson(narrativeData); } #endregion } }