Browse Source

Make NarrativeLog saving work

- Changed the name of the HistoryManager to NarrativeLog
 - Created the SaveReset SaveManagerSignal but it needs to be tied in
 - Made the PrettyPrint only show previous logs, not the current dialog.
master
lealeelu 8 years ago
parent
commit
d28cd2ea88
  1. 6
      Assets/Fungus/Scripts/Components/FungusManager.cs
  2. 71
      Assets/Fungus/Scripts/Components/HistoryManager.cs
  3. 109
      Assets/Fungus/Scripts/Components/NarrativeLog.cs
  4. 0
      Assets/Fungus/Scripts/Components/NarrativeLog.cs.meta
  5. 11
      Assets/Fungus/Scripts/Components/SaveData.cs
  6. 10
      Assets/Fungus/Scripts/Components/SaveMenu.cs
  7. 2
      Assets/Fungus/Scripts/Components/SayDialog.cs
  8. 9
      Assets/Fungus/Scripts/Signals/SaveManagerSignals.cs
  9. 1097
      Assets/FungusExamples/Conversation/Say History.unity

6
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -14,7 +14,7 @@ namespace Fungus
[RequireComponent(typeof(EventDispatcher))]
#if UNITY_5_3_OR_NEWER
[RequireComponent(typeof(SaveManager))]
[RequireComponent(typeof(HistoryManager))]
[RequireComponent(typeof(NarrativeLog))]
#endif
public sealed class FungusManager : MonoBehaviour
{
@ -29,7 +29,7 @@ namespace Fungus
EventDispatcher = GetComponent<EventDispatcher>();
#if UNITY_5_3_OR_NEWER
SaveManager = GetComponent<SaveManager>();
HistoryManager = GetComponent<HistoryManager>();
NarrativeLog = GetComponent<NarrativeLog>();
#endif
}
@ -72,7 +72,7 @@ namespace Fungus
/// <summary>
/// Gets the history manager singleton instance.
/// </summary>
public HistoryManager HistoryManager { get; private set; }
public NarrativeLog NarrativeLog { get; private set; }
#endif

71
Assets/Fungus/Scripts/Components/HistoryManager.cs

@ -1,71 +0,0 @@
// 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
{
/// <summary>
/// A single line of dialog
/// </summary>
[Serializable]
public class Line
{
public string name;
public string text;
}
[Serializable]
public class HistoryData
{
public List<Line> lines;
public HistoryData() {
lines = new List<Line>();
}
}
/// <summary>
/// Controls dialog history
/// </summary>
public class HistoryManager : MonoBehaviour
{
HistoryData history;
protected virtual void Awake()
{
history = new HistoryData();
}
public void AddLine(string name, string text)
{
Line line = new Line();
line.name = name;
line.text = text;
history.lines.Add(line);
}
public string GetHistory()
{
string jsonText = JsonUtility.ToJson(history, true);
return jsonText;
}
public string GetPrettyHistory()
{
string output = "";
for (int i = 0; i < history.lines.Count; i++)
{
output += "<b>" + history.lines[i].name + "</b>\n";
output += history.lines[i].text + "\n\n";
}
return output;
}
}
}

109
Assets/Fungus/Scripts/Components/NarrativeLog.cs

@ -0,0 +1,109 @@
// 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
{
/// <summary>
/// A single line of dialog
/// </summary>
[Serializable]
public class Line
{
[SerializeField] public string name;
[SerializeField] public string text;
}
/// <summary>
/// Serializable object to store Narrative Lines
/// </summary>
[Serializable]
public class NarrativeData
{
[SerializeField] public List<Line> lines;
public NarrativeData() {
lines = new List<Line>();
}
}
/// <summary>
/// Controls dialog history
/// </summary>
public class NarrativeLog : MonoBehaviour
{
NarrativeData history;
protected virtual void Awake()
{
history = new NarrativeData();
}
/// <summary>
/// Add a line of dialog to the Narrative Log
/// </summary>
/// <param name="name"></param>
/// <param name="text"></param>
public void AddLine(string name, string text)
{
Line line = new Line();
line.name = name;
line.text = text;
history.lines.Add(line);
}
/// <summary>
/// Clear line history after reset
/// </summary>
public void Clear()
{
history.lines.Clear();
}
/// <summary>
/// Convert history into Json for saving in SaveData
/// </summary>
/// <returns></returns>
public string GetJsonHistory()
{
string jsonText = JsonUtility.ToJson(history, true);
return jsonText;
}
/// <summary>
/// Show previous lines for display purposes
/// </summary>
/// <returns></returns>
public string GetPrettyHistory()
{
string output = "";
for (int i = 0; i < history.lines.Count-1; i++)
{
output += "<b>" + history.lines[i].name + "</b>\n";
output += history.lines[i].text + "\n\n";
}
return output;
}
/// <summary>
/// Load History from Json
/// </summary>
/// <param name="narrativeData"></param>
public void LoadHistory(string narrativeData)
{
if (narrativeData == null)
{
Debug.LogError("Failed to decode History save data item");
return;
}
history = JsonUtility.FromJson<NarrativeData>(narrativeData);
}
}
}

0
Assets/Fungus/Scripts/Components/HistoryManager.cs.meta → Assets/Fungus/Scripts/Components/NarrativeLog.cs.meta

11
Assets/Fungus/Scripts/Components/SaveData.cs

@ -17,6 +17,8 @@ namespace Fungus
{
protected const string FlowchartDataKey = "FlowchartData";
protected const string NarrativeLogKey = "HistoryData";
[Tooltip("A list of Flowchart objects whose variables will be encoded in the save data. Boolean, Integer, Float and String variables are supported.")]
[SerializeField] protected List<Flowchart> flowcharts = new List<Flowchart>();
@ -33,8 +35,10 @@ namespace Fungus
var flowchartData = FlowchartData.Encode(flowchart);
var saveDataItem = SaveDataItem.Create(FlowchartDataKey, JsonUtility.ToJson(flowchartData));
saveDataItems.Add(saveDataItem);
var historyDataItem = SaveDataItem.Create(NarrativeLogKey, FungusManager.Instance.NarrativeLog.GetJsonHistory());
saveDataItems.Add(historyDataItem);
}
}
@ -62,6 +66,11 @@ namespace Fungus
FlowchartData.Decode(flowchartData);
}
if (saveDataItem.DataType == NarrativeLogKey)
{
FungusManager.Instance.NarrativeLog.LoadHistory(saveDataItem.Data);
}
}
}

10
Assets/Fungus/Scripts/Components/SaveMenu.cs

@ -164,7 +164,7 @@ namespace Fungus
var historyText = historyView.GetComponentInChildren<Text>();
if (historyText != null)
{
historyText.text = FungusManager.Instance.HistoryManager.GetPrettyHistory();
historyText.text = FungusManager.Instance.NarrativeLog.GetPrettyHistory();
}
}
}
@ -190,6 +190,11 @@ namespace Fungus
}
}
protected virtual void OnSaveReset()
{
var saveManager = FungusManager.Instance.SaveManager;
}
protected void PlayClickSound()
{
if (clickAudioSource != null)
@ -301,7 +306,7 @@ namespace Fungus
public virtual void Restart()
{
var saveManager = FungusManager.Instance.SaveManager;
var narrativeLog = FungusManager.Instance.NarrativeLog;
if (string.IsNullOrEmpty(saveManager.StartScene))
{
Debug.LogError("No start scene specified");
@ -312,6 +317,7 @@ namespace Fungus
// Reset the Save History for a new game
saveManager.ClearHistory();
narrativeLog.Clear();
if (restartDeletesSave)
{
saveManager.Delete(saveDataKey);

2
Assets/Fungus/Scripts/Components/SayDialog.cs

@ -435,7 +435,7 @@ namespace Fungus
/// <param name="onComplete">Callback to execute when writing and player input have finished.</param>
public virtual void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete)
{
FungusManager.Instance.HistoryManager.AddLine(nameText.text, text);
FungusManager.Instance.NarrativeLog.AddLine(nameText.text, text);
StartCoroutine(DoSay(text, clearPrevious, waitForInput, fadeWhenDone, stopVoiceover, voiceOverClip, onComplete));
}

9
Assets/Fungus/Scripts/Signals/SaveManagerSignals.cs

@ -4,7 +4,7 @@
namespace Fungus
{
/// <summary>
/// Save manager signalling system.
/// Save manager signaling system.
/// You can use this to be notified about various events in the save game system.
/// </summary>
public static class SaveManagerSignals
@ -25,6 +25,13 @@ namespace Fungus
public delegate void SavePointAddedHandler(string savePointKey, string savePointDescription);
public static void DoSavePointAdded(string savePointKey, string savePointDescription) { if (OnSavePointAdded != null) OnSavePointAdded(savePointKey, savePointDescription); }
/// <summary>
/// SaveReset signal. Sent when the save history is reset.
/// </summary>
public static event SaveResetHandler OnSaveReset;
public delegate void SaveResetHandler();
public static void DoSaveReset() { if (OnSaveReset != null) OnSaveReset(); }
#endregion
}
}

1097
Assets/FungusExamples/Conversation/Say History.unity

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save