Browse Source

Button interactable state updates based on save data state

master
Christopher 8 years ago
parent
commit
467ea81f02
  1. 67
      Assets/Fungus/Scripts/Components/SaveGameHelper.cs
  2. 13
      Assets/Fungus/Scripts/Components/SaveManager.cs

67
Assets/Fungus/Scripts/Components/SaveGameHelper.cs

@ -8,6 +8,8 @@ namespace Fungus
{
public class SaveGameHelper : MonoBehaviour
{
const string SaveDataKey = "save_data";
const string NewGameSavePointKey = "new_game";
[SerializeField] protected string startScene = "";
@ -16,7 +18,13 @@ namespace Fungus
[SerializeField] protected bool restartDeletesSave = false;
[SerializeField] protected AudioClip buttonClickClip;
[SerializeField] protected Button saveButton;
[SerializeField] protected Button loadButton;
[SerializeField] protected Button rewindButton;
[SerializeField] protected Button restartButton;
[SerializeField] protected SaveGameObjects saveGameObjects = new SaveGameObjects();
@ -40,6 +48,26 @@ namespace Fungus
CheckSavePointKeys();
}
protected virtual void Update()
{
var saveManager = FungusManager.Instance.SaveManager;
if (saveButton != null)
{
// Don't allow saving unless there's at least one save point in the history,
// This avoids the case where you could try to load a save data with 0 save points.
saveButton.interactable = saveManager.NumSavePoints > 0;
}
if (loadButton != null)
{
loadButton.interactable = saveManager.SaveDataExists(SaveDataKey);
}
if (rewindButton != null)
{
rewindButton.interactable = saveManager.NumSavePoints > 1;
}
}
protected void CheckSavePointKeys()
{
List<string> keys = new List<string>();
@ -79,18 +107,24 @@ namespace Fungus
public virtual void Save()
{
PlayClickSound();
var saveManager = FungusManager.Instance.SaveManager;
saveManager.Save();
if (saveManager.NumSavePoints > 0)
{
PlayClickSound();
saveManager.Save(SaveDataKey);
}
}
public virtual void Load()
{
PlayClickSound();
var saveManager = FungusManager.Instance.SaveManager;
saveManager.Load();
if (saveManager.SaveDataExists(SaveDataKey))
{
PlayClickSound();
saveManager.Load(SaveDataKey);
}
}
public virtual void Rewind()
@ -98,21 +132,30 @@ namespace Fungus
PlayClickSound();
var saveManager = FungusManager.Instance.SaveManager;
saveManager.Rewind();
if (saveManager.NumSavePoints > 1)
{
saveManager.Rewind();
}
}
public virtual void Restart()
{
if (string.IsNullOrEmpty(startScene))
{
Debug.LogError("No start scene specified");
return;
}
var saveManager = FungusManager.Instance.SaveManager;
saveManager.ClearHistory();
PlayClickSound();
saveManager.ClearHistory();
if (restartDeletesSave)
{
saveManager.Delete();
saveManager.Delete(SaveDataKey);
}
PlayClickSound();
SceneManager.LoadScene(startScene);
}

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

@ -5,8 +5,6 @@ namespace Fungus
{
public class SaveManager : MonoBehaviour
{
const string DefaultSaveDataKey = "save_data";
protected static SaveHistory saveHistory = new SaveHistory();
protected virtual bool ReadSaveHistory(string saveDataKey)
@ -42,14 +40,14 @@ namespace Fungus
public virtual int NumSavePoints { get { return saveHistory.NumSavePoints; } }
public virtual void Save(string saveDataKey = DefaultSaveDataKey)
public virtual void Save(string saveDataKey)
{
WriteSaveHistory(saveDataKey);
SaveManagerSignals.DoGameSaved(saveDataKey);
}
public void Load(string saveDataKey = DefaultSaveDataKey)
public void Load(string saveDataKey)
{
if (ReadSaveHistory(saveDataKey))
{
@ -59,12 +57,17 @@ namespace Fungus
}
}
public void Delete(string saveDataKey = DefaultSaveDataKey)
public void Delete(string saveDataKey)
{
PlayerPrefs.DeleteKey(saveDataKey);
PlayerPrefs.Save();
}
public bool SaveDataExists(string saveDataKey)
{
return PlayerPrefs.HasKey(saveDataKey);
}
public virtual void AddSavePoint(string savePointKey, string savePointDescription)
{
saveHistory.AddSavePoint(savePointKey, savePointDescription);

Loading…
Cancel
Save