|
|
|
@ -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 |
|
|
|
|