diff --git a/Assets/Fungus/Scripts/Components/SaveManager.cs b/Assets/Fungus/Scripts/Components/SaveManager.cs index 088ecbe9..eec1513a 100644 --- a/Assets/Fungus/Scripts/Components/SaveManager.cs +++ b/Assets/Fungus/Scripts/Components/SaveManager.cs @@ -60,99 +60,27 @@ namespace Fungus var saveData = new SavePointData(); // Store the scene, flowchart and block to execute on resume - saveData.sceneName = SceneManager.GetActiveScene().name; - saveData.flowchartName = flowchart.name; - saveData.saveKey = saveKey; + saveData.SceneName = SceneManager.GetActiveScene().name; + saveData.SaveKey = saveKey; - for (int i = 0; i < flowchart.Variables.Count; i++) - { - var v = flowchart.Variables[i]; - - // Save string - var stringVariable = v as StringVariable; - if (stringVariable != null) - { - var d = new StringVar(); - d.key = stringVariable.Key; - d.value = stringVariable.Value; - saveData.stringVars.Add(d); - } - - // Save int - var intVariable = v as IntegerVariable; - if (intVariable != null) - { - var d = new IntVar(); - d.key = intVariable.Key; - d.value = intVariable.Value; - saveData.intVars.Add(d); - } - - // Save float - var floatVariable = v as FloatVariable; - if (floatVariable != null) - { - var d = new FloatVar(); - d.key = floatVariable.Key; - d.value = floatVariable.Value; - saveData.floatVars.Add(d); - } - - // Save bool - var boolVariable = v as BooleanVariable; - if (boolVariable != null) - { - var d = new BoolVar(); - d.key = boolVariable.Key; - d.value = boolVariable.Value; - saveData.boolVars.Add(d); - } - } + var flowchartData = FlowchartData.Encode(flowchart); + saveData.FlowchartData.Add(flowchartData); return JsonUtility.ToJson(saveData, true); } protected virtual void RestoreSavedGame(SavePointData saveData) { - var go = GameObject.Find(saveData.flowchartName); - if (go == null) - { - return; - } + var flowchartData = saveData.FlowchartData[0]; - var flowchart = go.GetComponent(); - if (flowchart == null) - { - return; - } - - for (int i = 0; i < saveData.boolVars.Count; i++) - { - var boolVar = saveData.boolVars[i]; - flowchart.SetBooleanVariable(boolVar.key, boolVar.value); - } - for (int i = 0; i < saveData.intVars.Count; i++) - { - var intVar = saveData.intVars[i]; - flowchart.SetIntegerVariable(intVar.key, intVar.value); - } - for (int i = 0; i < saveData.floatVars.Count; i++) - { - var floatVar = saveData.floatVars[i]; - flowchart.SetFloatVariable(floatVar.key, floatVar.value); - } - for (int i = 0; i < saveData.stringVars.Count; i++) - { - var stringVar = saveData.stringVars[i]; - flowchart.SetStringVariable(stringVar.key, stringVar.value); - } + FlowchartData.Decode(flowchartData); // Fire any matching SavePointLoaded event handler with matching save key. var eventHandlers = Object.FindObjectsOfType(); for (int i = 0; i < eventHandlers.Length; i++) { var eventHandler = eventHandlers[i]; - eventHandler.OnSavePointLoaded(saveData.saveKey); + eventHandler.OnSavePointLoaded(saveData.SaveKey); } // Execute any block with a Label matching the save key @@ -160,14 +88,14 @@ namespace Fungus for (int i = 0; i < labels.Length; i++) { var label = labels[i]; - if (string.Compare(label.Key, saveData.saveKey) == 0) + 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) @@ -190,7 +118,7 @@ namespace Fungus protected virtual void OnSceneLoaded(Scene scene, LoadSceneMode mode) { - if (scene.name == tempSaveData.sceneName) + if (scene.name == tempSaveData.SceneName) { RestoreSavedGame(tempSaveData); } @@ -244,7 +172,7 @@ namespace Fungus tempSaveData = JsonUtility.FromJson(jsonData); SceneManager.sceneLoaded += OnSceneLoaded; - SceneManager.LoadScene(tempSaveData.sceneName); + SceneManager.LoadScene(tempSaveData.SceneName); } public virtual void Delete(int slot) diff --git a/Assets/Fungus/Scripts/SavePoints.meta b/Assets/Fungus/Scripts/SavePoints.meta new file mode 100644 index 00000000..9683f914 --- /dev/null +++ b/Assets/Fungus/Scripts/SavePoints.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 6bd8827dc9c254f3daa0a9b2672a7764 +folderAsset: yes +timeCreated: 1479213577 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs b/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs new file mode 100644 index 00000000..53d24c8f --- /dev/null +++ b/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs @@ -0,0 +1,174 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Fungus +{ + [System.Serializable] + public class StringVar + { + [SerializeField] protected string key; + [SerializeField] protected string value; + + #region Public methods + + public string Key { get { return key; } set { key = value; } } + public string Value { get { return value; } set { this.value = value; } } + + #endregion + } + + [System.Serializable] + public class IntVar + { + [SerializeField] protected string key; + [SerializeField] protected int value; + + #region Public methods + + public string Key { get { return key; } set { key = value; } } + public int Value { get { return value; } set { this.value = value; } } + + #endregion + } + + [System.Serializable] + public class FloatVar + { + [SerializeField] protected string key; + [SerializeField] protected float value; + + #region Public methods + + public string Key { get { return key; } set { key = value; } } + public float Value { get { return value; } set { this.value = value; } } + + #endregion + } + + [System.Serializable] + public class BoolVar + { + [SerializeField] protected string key; + [SerializeField] protected bool value; + + #region Public methods + + public string Key { get { return key; } set { key = value; } } + public bool Value { get { return value; } set { this.value = value; } } + + #endregion + } + + [System.Serializable] + public class FlowchartData + { + [SerializeField] protected string flowchartName; + [SerializeField] protected List stringVars = new List(); + [SerializeField] protected List intVars = new List(); + [SerializeField] protected List floatVars = new List(); + [SerializeField] protected List boolVars = new List(); + + #region Public methods + + public string FlowchartName { get { return flowchartName; } set { flowchartName = value; } } + public List StringVars { get { return stringVars; } set { stringVars = value; } } + public List IntVars { get { return intVars; } set { intVars = value; } } + public List FloatVars { get { return floatVars; } set { floatVars = value; } } + public List BoolVars { get { return boolVars; } set { boolVars = value; } } + + public static FlowchartData Encode(Flowchart flowchart) + { + var flowchartData = new FlowchartData(); + + flowchartData.FlowchartName = flowchart.name; + + for (int i = 0; i < flowchart.Variables.Count; i++) + { + var v = flowchart.Variables[i]; + + // Save string + var stringVariable = v as StringVariable; + if (stringVariable != null) + { + var d = new StringVar(); + d.Key = stringVariable.Key; + d.Value = stringVariable.Value; + flowchartData.StringVars.Add(d); + } + + // Save int + var intVariable = v as IntegerVariable; + if (intVariable != null) + { + var d = new IntVar(); + d.Key = intVariable.Key; + d.Value = intVariable.Value; + flowchartData.IntVars.Add(d); + } + + // Save float + var floatVariable = v as FloatVariable; + if (floatVariable != null) + { + var d = new FloatVar(); + d.Key = floatVariable.Key; + d.Value = floatVariable.Value; + flowchartData.FloatVars.Add(d); + } + + // Save bool + var boolVariable = v as BooleanVariable; + if (boolVariable != null) + { + var d = new BoolVar(); + d.Key = boolVariable.Key; + d.Value = boolVariable.Value; + flowchartData.BoolVars.Add(d); + } + } + + return flowchartData; + } + + public static void Decode(FlowchartData flowchartData) + { + var go = GameObject.Find(flowchartData.FlowchartName); + if (go == null) + { + Debug.LogError("Failed to find flowchart object specified in save data"); + return; + } + + var flowchart = go.GetComponent(); + if (flowchart == null) + { + Debug.LogError("Failed to find flowchart object specified in save data"); + return; + } + + for (int i = 0; i < flowchartData.BoolVars.Count; i++) + { + var boolVar = flowchartData.BoolVars[i]; + flowchart.SetBooleanVariable(boolVar.Key, boolVar.Value); + } + for (int i = 0; i < flowchartData.IntVars.Count; i++) + { + var intVar = flowchartData.IntVars[i]; + flowchart.SetIntegerVariable(intVar.Key, intVar.Value); + } + for (int i = 0; i < flowchartData.FloatVars.Count; i++) + { + var floatVar = flowchartData.FloatVars[i]; + flowchart.SetFloatVariable(floatVar.Key, floatVar.Value); + } + for (int i = 0; i < flowchartData.StringVars.Count; i++) + { + var stringVar = flowchartData.StringVars[i]; + flowchart.SetStringVariable(stringVar.Key, stringVar.Value); + } + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs.meta b/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs.meta new file mode 100644 index 00000000..597ec7f8 --- /dev/null +++ b/Assets/Fungus/Scripts/SavePoints/FlowchartData.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 07443f90b887a47fb846500fe5ce3840 +timeCreated: 1478698399 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/SavePoints/SavePointData.cs b/Assets/Fungus/Scripts/SavePoints/SavePointData.cs new file mode 100644 index 00000000..184da366 --- /dev/null +++ b/Assets/Fungus/Scripts/SavePoints/SavePointData.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Fungus +{ + [System.Serializable] + public class SavePointData + { + [SerializeField] protected string saveKey; + [SerializeField] protected string sceneName; + [SerializeField] protected List flowchartData = new List(); + + #region Public methods + + public string SaveKey { get { return saveKey; } set { saveKey = value; } } + public string SceneName { get { return sceneName; } set { sceneName = value; } } + public List FlowchartData { get { return flowchartData; } set { flowchartData = value; } } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Utils/SavePointData.cs.meta b/Assets/Fungus/Scripts/SavePoints/SavePointData.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/Utils/SavePointData.cs.meta rename to Assets/Fungus/Scripts/SavePoints/SavePointData.cs.meta diff --git a/Assets/Fungus/Scripts/Utils/SavePointData.cs b/Assets/Fungus/Scripts/Utils/SavePointData.cs deleted file mode 100644 index d57f9955..00000000 --- a/Assets/Fungus/Scripts/Utils/SavePointData.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.SceneManagement; - -namespace Fungus -{ - [System.Serializable] - public class StringVar - { - public string key; - public string value; - } - - [System.Serializable] - public class IntVar - { - public string key; - public int value; - } - - [System.Serializable] - public class FloatVar - { - public string key; - public float value; - } - - [System.Serializable] - public class BoolVar - { - public string key; - public bool value; - } - - [System.Serializable] - public class SavePointData - { - public string sceneName; - public string flowchartName; - public string saveKey; - - public List stringVars = new List(); - public List intVars = new List(); - public List floatVars = new List(); - public List boolVars = new List(); - } -} \ No newline at end of file