Browse Source

Refactored SavePointData to support multiple flowcharts

master
Christopher 8 years ago
parent
commit
cb95ccc760
  1. 2
      Assets/Fungus/Scripts/Commands/SavePoint.cs
  2. 9
      Assets/Fungus/Scripts/Components/SaveHelper.cs
  3. 89
      Assets/Fungus/Scripts/Components/SaveManager.cs
  4. 89
      Assets/Fungus/Scripts/SavePoints/SavePointData.cs

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

@ -24,7 +24,7 @@ namespace Fungus
{
var saveManager = FungusManager.Instance.SaveManager;
saveManager.PopulateSaveBuffer(GetFlowchart(), saveKey, description);
saveManager.PopulateSaveBuffer(saveKey, description);
if (saveNow)
{

9
Assets/Fungus/Scripts/Components/SaveHelper.cs

@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace Fungus
@ -8,6 +9,12 @@ namespace Fungus
{
[SerializeField] protected string startScene = "";
[SerializeField] protected List<Flowchart> flowcharts = new List<Flowchart>();
#region Public methods
public List<Flowchart> Flowcharts { get { return flowcharts; } }
public virtual void Load(int slot)
{
var saveManager = FungusManager.Instance.SaveManager;
@ -30,5 +37,7 @@ namespace Fungus
{
SceneManager.LoadScene(sceneName);
}
#endregion
}
}

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

@ -9,30 +9,8 @@ namespace Fungus
const string SlotKeyFormat = "slot{0}";
// Make serialize data extensible (subclassing?)
// Save key, use save profile and variable substitution
// Store scene name, flowchart name and block name to execute after load
// Show link to Block to be executed
// Handle New Save Slot case: Scene to load?
// Save command stores data in SaveManager for writing later
// If SaveImmediately is selected then save it straight away (SaveGame command)
// If not selected, then Save when a Save button is pressed
// Select / Load button - set active slot (in playerprefs) and load the state
// Get list of saved games
// Delete Save Game
// Rename SaveFlowchart to SaveGame
protected string saveBuffer = "";
protected static SaveManager instance;
protected SavePointData tempSaveData;
protected virtual void Awake()
{
instance = this;
}
protected virtual string FormatSaveKey(int slot)
{
return string.Format(SlotKeyFormat, slot);
@ -55,50 +33,6 @@ namespace Fungus
return true;
}
protected virtual string CreateSaveData(Flowchart flowchart, string saveKey, string description)
{
var saveData = new SavePointData();
// Store the scene, flowchart and block to execute on resume
saveData.SceneName = SceneManager.GetActiveScene().name;
saveData.SaveKey = saveKey;
saveData.Description = description;
var flowchartData = FlowchartData.Encode(flowchart);
saveData.FlowchartData.Add(flowchartData);
return JsonUtility.ToJson(saveData, true);
}
protected virtual void RestoreSavedGame(SavePointData saveData)
{
var flowchartData = saveData.FlowchartData[0];
FlowchartData.Decode(flowchartData);
// 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(saveData.SaveKey);
}
// Execute any block with a Label matching the save key
var labels = Object.FindObjectsOfType<Label>();
for (int i = 0; i < labels.Length; i++)
{
var label = labels[i];
if (string.Compare(label.Key, saveData.SaveKey, true) == 0)
{
int index = label.CommandIndex;
var block = label.ParentBlock;
var fc = label.GetFlowchart();
fc.ExecuteBlock(block, index + 1);
}
}
}
protected virtual void StoreJSONData(string key, string jsonData)
{
if (key.Length > 0)
@ -117,20 +51,8 @@ namespace Fungus
return "";
}
protected virtual void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == tempSaveData.SceneName)
{
RestoreSavedGame(tempSaveData);
}
SceneManager.sceneLoaded -= OnSceneLoaded;
}
#region Public members
public static SaveManager Instance { get { return instance; } }
public virtual int ActiveSlot
{
get
@ -169,11 +91,9 @@ namespace Fungus
return;
}
var jsonData = LoadJSONData(key);
tempSaveData = JsonUtility.FromJson<SavePointData>(jsonData);
var saveDataJSON = LoadJSONData(key);
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(tempSaveData.SceneName);
SavePointData.Decode(saveDataJSON);
}
public virtual void Delete(int slot)
@ -182,9 +102,10 @@ namespace Fungus
PlayerPrefs.DeleteKey(key);
}
public virtual void PopulateSaveBuffer(Flowchart flowchart, string saveKey, string description)
public virtual void PopulateSaveBuffer(string saveKey, string description)
{
saveBuffer = CreateSaveData(flowchart, saveKey, description);
saveBuffer = SavePointData.Encode(saveKey, description, SceneManager.GetActiveScene().name);
Debug.Log(saveBuffer);
}
#endregion

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

@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Fungus
{
@ -9,14 +10,98 @@ namespace Fungus
[SerializeField] protected string saveKey;
[SerializeField] protected string description;
[SerializeField] protected string sceneName;
[SerializeField] protected List<FlowchartData> flowchartData = new List<FlowchartData>();
[SerializeField] protected List<FlowchartData> flowchartDatas = new List<FlowchartData>();
protected static SavePointData tempSavePointData;
protected static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name != tempSavePointData.SceneName)
{
return;
}
SceneManager.sceneLoaded -= OnSceneLoaded;
var savePointData = tempSavePointData;
for (int i = 0; i < savePointData.FlowchartDatas.Count; i++)
{
var flowchartData = savePointData.FlowchartDatas[i];
FlowchartData.Decode(flowchartData);
}
NotifyEventHandlers(savePointData);
ExecuteBlocks(savePointData);
}
protected static void NotifyEventHandlers(SavePointData savePointData)
{
// 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(savePointData.SaveKey);
}
}
protected static void ExecuteBlocks(SavePointData savePointData)
{
// Execute any block containing a Label matching the save key
var labels = Object.FindObjectsOfType<Label>();
for (int i = 0; i < labels.Length; i++)
{
var label = labels[i];
if (string.Compare(label.Key, savePointData.SaveKey, true) == 0)
{
int index = label.CommandIndex;
var block = label.ParentBlock;
var flowchart = label.GetFlowchart();
flowchart.ExecuteBlock(block, index + 1);
}
}
}
#region Public methods
public string SaveKey { get { return saveKey; } set { saveKey = value; } }
public string Description { get { return description; } set { description = value; } }
public string SceneName { get { return sceneName; } set { sceneName = value; } }
public List<FlowchartData> FlowchartData { get { return flowchartData; } set { flowchartData = value; } }
public List<FlowchartData> FlowchartDatas { get { return flowchartDatas; } set { flowchartDatas = value; } }
public static string Encode(string _saveKey, string _description, string _sceneName)
{
var savePointData = new SavePointData();
savePointData.saveKey = _saveKey;
savePointData.description = _description;
savePointData.sceneName = _sceneName;
var saveHelper = GameObject.FindObjectOfType<SaveHelper>();
if (saveHelper == null)
{
Debug.LogError("Failed to find SaveHelper object in scene");
return null;
}
for (int i = 0; i < saveHelper.Flowcharts.Count; i++)
{
var flowchart = saveHelper.Flowcharts[i];
var flowchartData = FlowchartData.Encode(flowchart);
savePointData.FlowchartDatas.Add(flowchartData);
}
return JsonUtility.ToJson(savePointData, true);
}
public static void Decode(string saveDataJSON)
{
tempSavePointData = JsonUtility.FromJson<SavePointData>(saveDataJSON);
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(tempSavePointData.SceneName);
}
#endregion
}

Loading…
Cancel
Save