From b230acf506bd2c58a6c7f4b0484d6b949f53893f Mon Sep 17 00:00:00 2001 From: Christopher Date: Thu, 24 Nov 2016 16:08:25 +0000 Subject: [PATCH] Updated slot picking --- .../Fungus/Scripts/Components/SaveManager.cs | 46 +++++++++++-------- .../Scripts/SavePoints/SavePointData.cs | 26 ++++++++--- Assets/FungusExamples/SaveGame/SavePicker.cs | 12 ++++- ProjectSettings/ProjectVersion.txt | 2 +- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/Assets/Fungus/Scripts/Components/SaveManager.cs b/Assets/Fungus/Scripts/Components/SaveManager.cs index 5acd00e7..0b13c414 100644 --- a/Assets/Fungus/Scripts/Components/SaveManager.cs +++ b/Assets/Fungus/Scripts/Components/SaveManager.cs @@ -16,22 +16,6 @@ namespace Fungus return string.Format(SlotKeyFormat, slot); } - protected virtual bool LoadNewGame(string key) - { - if (PlayerPrefs.HasKey(key) && - PlayerPrefs.GetString(key) != "") - { - return false; - } - - // Create a new save entry - PlayerPrefs.SetString(key, ""); - - SavePointLoaded.NotifyEventHandlers("new_game"); - - return true; - } - protected virtual void StoreJSONData(string key, string jsonData) { if (key.Length > 0) @@ -79,20 +63,42 @@ namespace Fungus saveBuffer = ""; } + public virtual bool SlotExists(int slot) + { + var key = FormatSaveKey(slot); + + if (PlayerPrefs.HasKey(key) && + PlayerPrefs.GetString(key) != "") + { + return false; + } + + return true; + } + public virtual void Load(int slot) { ActiveSlot = slot; var key = FormatSaveKey(slot); - if (LoadNewGame(key)) + var saveDataJSON = LoadJSONData(key); + if (saveDataJSON != "") { - return; + SavePointData.Decode(saveDataJSON); } + } - var saveDataJSON = LoadJSONData(key); + public virtual void LoadNewGame(int slot, string saveDescription) + { + var key = FormatSaveKey(slot); - SavePointData.Decode(saveDataJSON); + var saveDataJSON = SavePointData.EncodeNewGame(saveDescription, SceneManager.GetActiveScene().name); + + // Create a new save entry + PlayerPrefs.SetString(key, saveDataJSON); + + SavePointLoaded.NotifyEventHandlers("new_game"); } public virtual void Delete(int slot) diff --git a/Assets/Fungus/Scripts/SavePoints/SavePointData.cs b/Assets/Fungus/Scripts/SavePoints/SavePointData.cs index 595593e3..5ad74b63 100644 --- a/Assets/Fungus/Scripts/SavePoints/SavePointData.cs +++ b/Assets/Fungus/Scripts/SavePoints/SavePointData.cs @@ -58,6 +58,18 @@ namespace Fungus } } + protected static SavePointData Create(string _saveKey, string _description, string _sceneName) + { + var savePointData = new SavePointData(); + + savePointData.version = 1; + savePointData.saveKey = _saveKey; + savePointData.description = _description; + savePointData.sceneName = _sceneName; + + return savePointData; + } + #region Public methods public string SaveKey { get { return saveKey; } set { saveKey = value; } } @@ -65,14 +77,16 @@ namespace Fungus public string SceneName { get { return sceneName; } set { sceneName = value; } } public List FlowchartDatas { get { return flowchartDatas; } set { flowchartDatas = value; } } - public static string Encode(string _saveKey, string _description, string _sceneName) + public static string EncodeNewGame(string _description, string _sceneName) { - var savePointData = new SavePointData(); + var savePointData = Create("new_game", _description, _sceneName); - savePointData.version = 1; - savePointData.saveKey = _saveKey; - savePointData.description = _description; - savePointData.sceneName = _sceneName; + return JsonUtility.ToJson(savePointData, true); + } + + public static string Encode(string _saveKey, string _description, string _sceneName) + { + var savePointData = Create(_saveKey, _description, _sceneName); var gameSaver = GameObject.FindObjectOfType(); if (gameSaver == null) diff --git a/Assets/FungusExamples/SaveGame/SavePicker.cs b/Assets/FungusExamples/SaveGame/SavePicker.cs index de5494aa..a0204b25 100644 --- a/Assets/FungusExamples/SaveGame/SavePicker.cs +++ b/Assets/FungusExamples/SaveGame/SavePicker.cs @@ -5,12 +5,22 @@ namespace Fungus { public class SavePicker : MonoBehaviour { + [SerializeField] protected string newGameDescription = "Playing"; + #region Public methods public virtual void Select(int slot) { var saveManager = FungusManager.Instance.SaveManager; - saveManager.Load(slot); + + if (saveManager.SlotExists(slot)) + { + saveManager.Load(slot); + } + else + { + saveManager.LoadNewGame(slot, newGameDescription); + } } public virtual void Delete(int slot) diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index ae41cb69..027070f2 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 5.4.2f2 +m_EditorVersion: 5.4.3f1 m_StandardAssetsVersion: 0