// 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)
#if UNITY_5_3_OR_NEWER
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace Fungus
{
///
/// The Save History is a list of previously created Save Points, sorted chronologically.
///
[System.Serializable]
public class SaveHistory
{
///
/// Version number of current save data format.
///
protected const int SaveDataVersion = 1;
[SerializeField] protected int version = SaveDataVersion;
[SerializeField] protected List savePoints = new List();
[SerializeField] protected List rewoundSavePoints = new List();
#region Public methods
///
/// Returns the number of Save Points in the Save History.
///
public int NumSavePoints { get { return savePoints.Count; } }
///
/// Returns the current number of rewound Save Points in the Save History.
///
public int NumRewoundSavePoints { get { return rewoundSavePoints.Count; } }
///
/// Creates a new Save Point using a key and description, and adds it to the Save History.
///
public void AddSavePoint(string savePointKey, string savePointDescription)
{
// Creating a new Save Point invalidates all rewound Save Points, so delete them.
ClearRewoundSavePoints();
string sceneName = SceneManager.GetActiveScene().name;
var savePointData = SavePointData.Encode(savePointKey, savePointDescription, sceneName);
savePoints.Add(savePointData);
}
///
/// Rewinds to the previous Save Point in the Save History.
/// The latest Save Point is moved to a seperate list of rewound save points.
///
public void Rewind()
{
if (savePoints.Count > 0)
{
rewoundSavePoints.Add(savePoints[savePoints.Count - 1]);
savePoints.RemoveAt(savePoints.Count - 1);
}
}
///
/// Fast forwards to the next Save Point in the Save History.
/// The most recently rewound Save Point is moved back to the main list of save points.
///
public void FastForward()
{
if (rewoundSavePoints.Count > 0)
{
savePoints.Add(rewoundSavePoints[rewoundSavePoints.Count - 1]);
rewoundSavePoints.RemoveAt(rewoundSavePoints.Count - 1);
}
}
///
/// Loads the latest Save Point.
///
public void LoadLatestSavePoint()
{
if (savePoints.Count > 0)
{
var savePointData = savePoints[savePoints.Count - 1];
SavePointData.Decode(savePointData);
}
}
///
/// Clears all Save Points.
///
public void Clear()
{
savePoints.Clear();
rewoundSavePoints.Clear();
}
///
/// Clears rewound Save Points only. The main Save Point list is not changed.
///
public void ClearRewoundSavePoints()
{
rewoundSavePoints.Clear();
}
public virtual string GetDebugInfo()
{
string debugInfo = "Save points:\n";
foreach (var savePoint in savePoints)
{
debugInfo += savePoint.Substring(0, savePoint.IndexOf(',')).Replace("\n", "").Replace("{", "").Replace("}","") + "\n";
}
debugInfo += "Rewound points:\n";
foreach (var savePoint in rewoundSavePoints)
{
debugInfo += savePoint.Substring(0, savePoint.IndexOf(',')).Replace("\n", "").Replace("{", "").Replace("}","") + "\n";
}
return debugInfo;
}
#endregion
}
}
#endif