Browse Source

Refactored save key and save description variable names. Added check for duplicate save keys.

master
Christopher 8 years ago
parent
commit
e0dca84770
  1. 21
      Assets/Fungus/Scripts/Commands/SavePoint.cs
  2. 44
      Assets/Fungus/Scripts/Components/SaveGameHelper.cs
  3. 10
      Assets/Fungus/Scripts/Components/SaveManager.cs
  4. 10
      Assets/Fungus/Scripts/EventHandlers/SavePointLoaded.cs
  5. 4
      Assets/Fungus/Scripts/SavePoints/SaveHistory.cs
  6. 28
      Assets/Fungus/Scripts/SavePoints/SavePointData.cs
  7. 21
      Assets/FungusExamples/SaveGame/SaveExample.unity
  8. 8
      Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab
  9. 0
      Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab.meta

21
Assets/Fungus/Scripts/Commands/SavePoint.cs

@ -12,30 +12,41 @@ namespace Fungus
"Creates a save point which can be saved to persistant storage and loaded again later.")]
public class SavePoint : Command
{
[SerializeField] protected string saveKey;
[SerializeField] protected string savePointKey;
[SerializeField] protected string saveDescription;
[SerializeField] protected string savePointDescription;
[SerializeField] protected bool resumeFromHere = true;
#region Public members
public string SaveKey { get { return saveKey; } }
public string SavePointKey { get { return savePointKey; } }
public bool ResumeFromHere { get { return resumeFromHere; } }
public override void OnEnter()
{
if (string.IsNullOrEmpty(savePointKey))
{
Continue();
return;
}
var saveManager = FungusManager.Instance.SaveManager;
saveManager.AddSavePoint(saveKey, saveDescription);
saveManager.AddSavePoint(savePointKey, savePointDescription);
Continue();
}
public override string GetSummary()
{
return saveKey + " : " + saveDescription;
if (string.IsNullOrEmpty(savePointKey))
{
return "Error: Save Point Key not specified";
}
return savePointKey + " : " + savePointDescription;
}
public override Color GetButtonColor()

44
Assets/Fungus/Scripts/Components/SaveGameHelper.cs

@ -8,7 +8,7 @@ namespace Fungus
{
public class SaveGameHelper : MonoBehaviour
{
const string NewGameSaveKey = "new_game";
const string NewGameSavePointKey = "new_game";
[SerializeField] protected string startScene = "";
@ -18,16 +18,6 @@ namespace Fungus
[SerializeField] protected SaveGameObjects saveGameObjects = new SaveGameObjects();
protected virtual void OnEnable()
{
SaveSignals.OnGameSave += OnGameSave;
}
protected virtual void OnDisable()
{
SaveSignals.OnGameSave -= OnGameSave;
}
protected virtual void Start()
{
var saveManager = FungusManager.Instance.SaveManager;
@ -35,12 +25,35 @@ namespace Fungus
if (autoStartGame &&
saveManager.NumSavePoints == 0)
{
SavePointLoaded.NotifyEventHandlers(NewGameSaveKey);
SavePointLoaded.NotifyEventHandlers(NewGameSavePointKey);
}
CheckSavePointKeys();
}
protected void CheckSavePointKeys()
{
List<string> keys = new List<string>();
var savePoints = GameObject.FindObjectsOfType<SavePoint>();
foreach (var savePoint in savePoints)
{
if (string.IsNullOrEmpty(savePoint.SavePointKey))
{
continue;
}
protected virtual void OnGameSave(string saveKey, string saveDescription)
if (keys.Contains(savePoint.SavePointKey))
{
Debug.LogError("Save Point Key " + savePoint.SavePointKey + " is defined multiple times.");
}
else
{
keys.Add(savePoint.SavePointKey);
}
}
}
#region Public methods
@ -78,6 +91,11 @@ namespace Fungus
SceneManager.LoadScene(startScene);
}
public virtual void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
#endregion
}
}

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

@ -45,6 +45,8 @@ namespace Fungus
public virtual void Save(string saveDataKey = DefaultSaveDataKey)
{
WriteSaveHistory(saveDataKey);
SaveManagerSignals.DoGameSaved(saveDataKey);
}
public void Load(string saveDataKey = DefaultSaveDataKey)
@ -52,6 +54,8 @@ namespace Fungus
if (ReadSaveHistory(saveDataKey))
{
saveHistory.LoadLatestSavePoint();
SaveManagerSignals.DoGameLoaded(saveDataKey);
}
}
@ -61,9 +65,11 @@ namespace Fungus
PlayerPrefs.Save();
}
public virtual void AddSavePoint(string saveKey, string saveDescription)
public virtual void AddSavePoint(string savePointKey, string savePointDescription)
{
saveHistory.AddSavePoint(saveKey, saveDescription);
saveHistory.AddSavePoint(savePointKey, savePointDescription);
SaveManagerSignals.DoSavePointAdded(savePointKey, savePointDescription);
}
public virtual void Rewind()

10
Assets/Fungus/Scripts/EventHandlers/SavePointLoaded.cs

@ -12,11 +12,11 @@ namespace Fungus
public class SavePointLoaded : EventHandler
{
[Tooltip("Block will execute if the Save Key of the loaded save point matches this save key.")]
[SerializeField] protected string saveKey = "";
[SerializeField] protected string savePointKey = "";
protected void OnSavePointLoaded(string _saveKey)
protected void OnSavePointLoaded(string _savePointKey)
{
if (string.Compare(saveKey, _saveKey, true) == 0)
if (string.Compare(savePointKey, _savePointKey, true) == 0)
{
ExecuteBlock();
}
@ -24,14 +24,14 @@ namespace Fungus
#region Public methods
public static void NotifyEventHandlers(string _saveKey)
public static void NotifyEventHandlers(string _savePointKey)
{
// Fire any matching SavePointLoaded event handler with matching save key.
var eventHandlers = Object.FindObjectsOfType<SavePointLoaded>();
for (int i = 0; i < eventHandlers.Length; i++)
{
var eventHandler = eventHandlers[i];
eventHandler.OnSavePointLoaded(_saveKey);
eventHandler.OnSavePointLoaded(_savePointKey);
}
}

4
Assets/Fungus/Scripts/SavePoints/SaveHistory.cs

@ -21,10 +21,10 @@ namespace Fungus
public int NumSavePoints { get { return savePoints.Count; } }
public void AddSavePoint(string saveKey, string saveDescription)
public void AddSavePoint(string savePointKey, string savePointDescription)
{
string sceneName = SceneManager.GetActiveScene().name;
var savePointData = SavePointData.Encode(saveKey, saveDescription, sceneName);
var savePointData = SavePointData.Encode(savePointKey, savePointDescription, sceneName);
savePoints.Add(savePointData);
}

28
Assets/Fungus/Scripts/SavePoints/SavePointData.cs

@ -7,8 +7,8 @@ namespace Fungus
[System.Serializable]
public class SavePointData
{
[SerializeField] protected string saveKey;
[SerializeField] protected string description;
[SerializeField] protected string savePointKey;
[SerializeField] protected string savePointDescription;
[SerializeField] protected string sceneName;
[SerializeField] protected List<FlowchartData> flowchartDatas = new List<FlowchartData>();
@ -30,12 +30,12 @@ namespace Fungus
FlowchartData.Decode(flowchartData);
}
ExecuteBlocks(savePointData.saveKey);
ExecuteBlocks(savePointData.savePointKey);
}
protected static void ExecuteBlocks(string saveKey)
protected static void ExecuteBlocks(string savePointKey)
{
SavePointLoaded.NotifyEventHandlers(saveKey);
SavePointLoaded.NotifyEventHandlers(savePointKey);
// Execute any block containing a SavePoint command matching the save key, with Resume From Here enabled
var savePoints = Object.FindObjectsOfType<SavePoint>();
@ -43,7 +43,7 @@ namespace Fungus
{
var savePoint = savePoints[i];
if (savePoint.ResumeFromHere &&
string.Compare(savePoint.SaveKey, saveKey, true) == 0)
string.Compare(savePoint.SavePointKey, savePointKey, true) == 0)
{
int index = savePoint.CommandIndex;
var block = savePoint.ParentBlock;
@ -60,7 +60,7 @@ namespace Fungus
for (int i = 0; i < labels.Length; i++)
{
var label = labels[i];
if (string.Compare(label.Key, saveKey, true) == 0)
if (string.Compare(label.Key, savePointKey, true) == 0)
{
int index = label.CommandIndex;
var block = label.ParentBlock;
@ -70,12 +70,12 @@ namespace Fungus
}
}
protected static SavePointData Create(string _saveKey, string _description, string _sceneName)
protected static SavePointData Create(string _savePointKey, string _savePointDescription, string _sceneName)
{
var savePointData = new SavePointData();
savePointData.saveKey = _saveKey;
savePointData.description = _description;
savePointData.savePointKey = _savePointKey;
savePointData.savePointDescription = _savePointDescription;
savePointData.sceneName = _sceneName;
return savePointData;
@ -83,8 +83,8 @@ namespace Fungus
#region Public methods
public string SaveKey { get { return saveKey; } set { saveKey = value; } }
public string Description { get { return description; } set { description = value; } }
public string SavePointKey { get { return savePointKey; } set { savePointKey = value; } }
public string SavePointDescription { get { return savePointDescription; } set { savePointDescription = value; } }
public string SceneName { get { return sceneName; } set { sceneName = value; } }
public List<FlowchartData> FlowchartDatas { get { return flowchartDatas; } set { flowchartDatas = value; } }
@ -95,9 +95,9 @@ namespace Fungus
return JsonUtility.ToJson(savePointData, true);
}
public static string Encode(string _saveKey, string _description, string _sceneName)
public static string Encode(string _savePointKey, string _savePointDescription, string _sceneName)
{
var savePointData = Create(_saveKey, _description, _sceneName);
var savePointData = Create(_savePointKey, _savePointDescription, _sceneName);
var saveGameHelper = GameObject.FindObjectOfType<SaveGameHelper>();
if (saveGameHelper == null)

21
Assets/FungusExamples/SaveGame/SaveExample.unity

@ -1727,8 +1727,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
itemId: 14
indentLevel: 0
saveKey: step_4
saveDescription:
savePointKey: step 4
savePointDescription:
resumeFromHere: 1
--- !u!114 &1889213472
MonoBehaviour:
@ -1768,8 +1768,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
itemId: 12
indentLevel: 0
saveKey: step_3
saveDescription:
savePointKey: step 3
savePointDescription:
resumeFromHere: 1
--- !u!114 &1889213474
MonoBehaviour:
@ -1783,7 +1783,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
parentBlock: {fileID: 1889213475}
saveKey: new_game
savePointKey: new_game
--- !u!114 &1889213475
MonoBehaviour:
m_ObjectHideFlags: 2
@ -1843,7 +1843,8 @@ MonoBehaviour:
height: 922
selectedBlocks:
- {fileID: 1889213475}
selectedCommands: []
selectedCommands:
- {fileID: 1889213471}
variables: []
description:
stepPause: 0
@ -1906,8 +1907,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
itemId: 10
indentLevel: 0
saveKey: step_2
saveDescription:
savePointKey: step 2
savePointDescription:
resumeFromHere: 1
--- !u!114 &1889213480
MonoBehaviour:
@ -1947,8 +1948,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
itemId: 8
indentLevel: 0
saveKey: step_1
saveDescription:
savePointKey: step 1
savePointDescription:
resumeFromHere: 1
--- !u!1 &1922034243
GameObject:

8
Assets/FungusExamples/SaveGame/GameSaver.prefab → Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab

@ -21,7 +21,7 @@ GameObject:
- 4: {fileID: 4000012594529446}
- 114: {fileID: 114000011912982844}
m_Layer: 0
m_Name: GameSaver
m_Name: SaveGameCanvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@ -52,5 +52,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
startScene:
flowcharts:
- {fileID: 0}
autoStartGame: 1
restartDeletesSave: 0
saveGameObjects:
flowcharts: []

0
Assets/FungusExamples/SaveGame/GameSaver.prefab.meta → Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab.meta

Loading…
Cancel
Save