Browse Source

SaveMenu is now a singleton game object. SaveGameObjects manages mapping to saveable game objects.

master
Christopher 8 years ago
parent
commit
0c27baca7b
  1. 25
      Assets/Fungus/Scripts/Components/SaveGameObjects.cs
  2. 0
      Assets/Fungus/Scripts/Components/SaveGameObjects.cs.meta
  3. 1
      Assets/Fungus/Scripts/Components/SaveManager.cs
  4. 58
      Assets/Fungus/Scripts/Components/SaveMenu.cs
  5. 0
      Assets/Fungus/Scripts/Components/SaveMenu.cs.meta
  6. 17
      Assets/Fungus/Scripts/SavePoints/SaveGameObjects.cs
  7. 6
      Assets/Fungus/Scripts/SavePoints/SaveHistory.cs
  8. 21
      Assets/Fungus/Scripts/SavePoints/SavePointData.cs
  9. 2208
      Assets/FungusExamples/SaveGame/SaveExample.unity
  10. 981
      Assets/FungusExamples/SaveGame/SaveMenu.prefab
  11. 4
      Assets/FungusExamples/SaveGame/SaveMenu.prefab.meta

25
Assets/Fungus/Scripts/Components/SaveGameObjects.cs

@ -0,0 +1,25 @@
using UnityEngine;
using System.Collections.Generic;
namespace Fungus
{
[System.Serializable]
public class SaveGameObjects : MonoBehaviour
{
[SerializeField] protected List<Flowchart> flowcharts = new List<Flowchart>();
#region Public methods
public void Encode(SavePointData savePointData)
{
for (int i = 0; i < flowcharts.Count; i++)
{
var flowchart = flowcharts[i];
var flowchartData = FlowchartData.Encode(flowchart);
savePointData.FlowchartDatas.Add(flowchartData);
}
}
#endregion
}
}

0
Assets/Fungus/Scripts/SavePoints/SaveGameObjects.cs.meta → Assets/Fungus/Scripts/Components/SaveGameObjects.cs.meta

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

@ -53,6 +53,7 @@ namespace Fungus
{ {
if (ReadSaveHistory(saveDataKey)) if (ReadSaveHistory(saveDataKey))
{ {
saveHistory.ClearRewoundSavePoints();
saveHistory.LoadLatestSavePoint(); saveHistory.LoadLatestSavePoint();
SaveManagerSignals.DoGameLoaded(saveDataKey); SaveManagerSignals.DoGameLoaded(saveDataKey);

58
Assets/Fungus/Scripts/Components/SaveGameHelper.cs → Assets/Fungus/Scripts/Components/SaveMenu.cs

@ -6,18 +6,14 @@ using UnityEngine.SceneManagement;
namespace Fungus namespace Fungus
{ {
public class SaveGameHelper : MonoBehaviour public class SaveMenu : MonoBehaviour
{ {
const string SaveDataKey = "save_data"; const string SaveDataKey = "save_data";
const string NewGameSavePointKey = "new_game"; const string NewGameSavePointKey = "new_game";
[SerializeField] protected string startScene = "";
[SerializeField] protected bool autoStartGame = true; [SerializeField] protected bool autoStartGame = true;
[SerializeField] protected bool saveMenuActive = false;
[SerializeField] protected bool restartDeletesSave = false; [SerializeField] protected bool restartDeletesSave = false;
[SerializeField] protected CanvasGroup saveMenuGroup; [SerializeField] protected CanvasGroup saveMenuGroup;
@ -34,19 +30,36 @@ namespace Fungus
[SerializeField] protected Button restartButton; [SerializeField] protected Button restartButton;
[SerializeField] protected SaveGameObjects saveGameObjects = new SaveGameObjects(); protected static bool saveMenuActive = false;
protected AudioSource clickAudioSource; protected AudioSource clickAudioSource;
protected LTDescr fadeTween; protected LTDescr fadeTween;
protected static SaveMenu instance;
protected string startScene = "";
protected virtual void Awake() protected virtual void Awake()
{ {
// Only one instance of SaveMenu may exist
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
GameObject.DontDestroyOnLoad(this);
clickAudioSource = GetComponent<AudioSource>(); clickAudioSource = GetComponent<AudioSource>();
} }
protected virtual void Start() protected virtual void Start()
{ {
startScene = SceneManager.GetActiveScene().name;
var saveManager = FungusManager.Instance.SaveManager; var saveManager = FungusManager.Instance.SaveManager;
if (!saveMenuActive) if (!saveMenuActive)
@ -87,6 +100,9 @@ namespace Fungus
} }
} }
/// <summary>
/// Warn if duplicate SavePointKeys are found.
/// </summary>
protected void CheckSavePointKeys() protected void CheckSavePointKeys()
{ {
List<string> keys = new List<string>(); List<string> keys = new List<string>();
@ -119,15 +135,27 @@ namespace Fungus
} }
} }
#region Public methods /// <summary>
/// Callback for restart scene load
/// </summary>
protected void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name != startScene)
{
return;
}
SceneManager.sceneLoaded -= OnSceneLoaded;
SavePointLoaded.NotifyEventHandlers(NewGameSavePointKey);
}
public SaveGameObjects SaveGameObjects { get { return saveGameObjects; } } #region Public methods
public virtual void ToggleSaveMenu() public virtual void ToggleSaveMenu()
{ {
if (fadeTween != null) if (fadeTween != null)
{ {
LeanTween.cancel(fadeTween.id); LeanTween.cancel(fadeTween.id, true);
fadeTween = null; fadeTween = null;
} }
@ -136,6 +164,8 @@ namespace Fungus
// Switch menu off // Switch menu off
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 0f, 0.5f).setOnUpdate( (t) => { LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 0f, 0.5f).setOnUpdate( (t) => {
saveMenuGroup.alpha = t; saveMenuGroup.alpha = t;
}).setOnComplete( () => {
saveMenuGroup.alpha = 0f;
}); });
} }
else else
@ -143,6 +173,8 @@ namespace Fungus
// Switch menu on // Switch menu on
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 1f, 0.5f).setOnUpdate( (t) => { LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 1f, 0.5f).setOnUpdate( (t) => {
saveMenuGroup.alpha = t; saveMenuGroup.alpha = t;
}).setOnComplete( () => {
saveMenuGroup.alpha = 1f;
}); });
} }
@ -211,16 +243,10 @@ namespace Fungus
saveManager.Delete(SaveDataKey); saveManager.Delete(SaveDataKey);
} }
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(startScene); SceneManager.LoadScene(startScene);
} }
public virtual void LoadScene(string sceneName)
{
PlayClickSound();
SceneManager.LoadScene(sceneName);
}
#endregion #endregion
} }
} }

0
Assets/Fungus/Scripts/Components/SaveGameHelper.cs.meta → Assets/Fungus/Scripts/Components/SaveMenu.cs.meta

17
Assets/Fungus/Scripts/SavePoints/SaveGameObjects.cs

@ -1,17 +0,0 @@
using UnityEngine;
using System.Collections.Generic;
namespace Fungus
{
[System.Serializable]
public class SaveGameObjects
{
[SerializeField] protected List<Flowchart> flowcharts = new List<Flowchart>();
#region Public methods
public List<Flowchart> Flowcharts { get { return flowcharts; } }
#endregion
}
}

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

@ -62,6 +62,12 @@ namespace Fungus
public void Clear() public void Clear()
{ {
savePoints.Clear(); savePoints.Clear();
rewoundSavePoints.Clear();
}
public void ClearRewoundSavePoints()
{
rewoundSavePoints.Clear();
} }
#endregion #endregion

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

@ -88,29 +88,18 @@ namespace Fungus
public string SceneName { get { return sceneName; } set { sceneName = value; } } public string SceneName { get { return sceneName; } set { sceneName = value; } }
public List<FlowchartData> FlowchartDatas { get { return flowchartDatas; } set { flowchartDatas = value; } } public List<FlowchartData> FlowchartDatas { get { return flowchartDatas; } set { flowchartDatas = value; } }
public static string EncodeNewGame(string _description, string _sceneName)
{
var savePointData = Create("start_game", _description, _sceneName);
return JsonUtility.ToJson(savePointData, true);
}
public static string Encode(string _savePointKey, string _savePointDescription, string _sceneName) public static string Encode(string _savePointKey, string _savePointDescription, string _sceneName)
{ {
var savePointData = Create(_savePointKey, _savePointDescription, _sceneName); var savePointData = Create(_savePointKey, _savePointDescription, _sceneName);
var saveGameHelper = GameObject.FindObjectOfType<SaveGameHelper>(); var saveGameObjects = GameObject.FindObjectOfType<SaveGameObjects>();
if (saveGameHelper == null) if (saveGameObjects == null)
{ {
Debug.LogError("Failed to find SaveGameHelper object in scene"); Debug.LogWarning("Failed to find a SaveGameObjects in current scene");
return null;
} }
else
for (int i = 0; i < saveGameHelper.SaveGameObjects.Flowcharts.Count; i++)
{ {
var flowchart = saveGameHelper.SaveGameObjects.Flowcharts[i]; saveGameObjects.Encode(savePointData);
var flowchartData = FlowchartData.Encode(flowchart);
savePointData.FlowchartDatas.Add(flowchartData);
} }
return JsonUtility.ToJson(savePointData, true); return JsonUtility.ToJson(savePointData, true);

2208
Assets/FungusExamples/SaveGame/SaveExample.unity

File diff suppressed because it is too large Load Diff

981
Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab → Assets/FungusExamples/SaveGame/SaveMenu.prefab

File diff suppressed because it is too large Load Diff

4
Assets/FungusExamples/SaveGame/SaveGameCanvas.prefab.meta → Assets/FungusExamples/SaveGame/SaveMenu.prefab.meta

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: ac03b2b3b2c07411da18c6ddc7c6e9ec guid: bd2b99773f3e0489aae9f9b5053ad360
timeCreated: 1479485548 timeCreated: 1481298777
licenseType: Free licenseType: Free
NativeFormatImporter: NativeFormatImporter:
userData: userData:
Loading…
Cancel
Save