You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
252 lines
6.8 KiB
252 lines
6.8 KiB
8 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine.SceneManagement;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
public class SaveMenu : MonoBehaviour
|
||
8 years ago
|
{
|
||
8 years ago
|
const string SaveDataKey = "save_data";
|
||
|
|
||
8 years ago
|
const string NewGameSavePointKey = "new_game";
|
||
8 years ago
|
|
||
|
[SerializeField] protected bool autoStartGame = true;
|
||
|
|
||
|
[SerializeField] protected bool restartDeletesSave = false;
|
||
|
|
||
8 years ago
|
[SerializeField] protected CanvasGroup saveMenuGroup;
|
||
|
|
||
|
[SerializeField] protected Button saveMenuButton;
|
||
|
|
||
8 years ago
|
[SerializeField] protected Button saveButton;
|
||
|
|
||
|
[SerializeField] protected Button loadButton;
|
||
|
|
||
|
[SerializeField] protected Button rewindButton;
|
||
|
|
||
8 years ago
|
[SerializeField] protected Button forwardButton;
|
||
|
|
||
8 years ago
|
[SerializeField] protected Button restartButton;
|
||
8 years ago
|
|
||
8 years ago
|
protected static bool saveMenuActive = false;
|
||
8 years ago
|
|
||
8 years ago
|
protected AudioSource clickAudioSource;
|
||
|
|
||
8 years ago
|
protected LTDescr fadeTween;
|
||
|
|
||
8 years ago
|
protected static SaveMenu instance;
|
||
|
|
||
|
protected string startScene = "";
|
||
|
|
||
8 years ago
|
protected virtual void Awake()
|
||
|
{
|
||
8 years ago
|
// Only one instance of SaveMenu may exist
|
||
|
if (instance != null)
|
||
|
{
|
||
|
Destroy(gameObject);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
instance = this;
|
||
|
|
||
|
GameObject.DontDestroyOnLoad(this);
|
||
|
|
||
8 years ago
|
clickAudioSource = GetComponent<AudioSource>();
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual void Start()
|
||
|
{
|
||
8 years ago
|
startScene = SceneManager.GetActiveScene().name;
|
||
|
|
||
8 years ago
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
|
|
||
8 years ago
|
if (!saveMenuActive)
|
||
|
{
|
||
|
saveMenuGroup.alpha = 0f;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (autoStartGame &&
|
||
|
saveManager.NumSavePoints == 0)
|
||
|
{
|
||
8 years ago
|
SavePointLoaded.NotifyEventHandlers(NewGameSavePointKey);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
CheckSavePointKeys();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
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;
|
||
|
}
|
||
8 years ago
|
if (forwardButton != null)
|
||
|
{
|
||
|
forwardButton.interactable = saveManager.NumRewoundSavePoints > 0;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Warn if duplicate SavePointKeys are found.
|
||
|
/// </summary>
|
||
8 years ago
|
protected void CheckSavePointKeys()
|
||
8 years ago
|
{
|
||
8 years ago
|
List<string> keys = new List<string>();
|
||
|
|
||
|
var savePoints = GameObject.FindObjectsOfType<SavePoint>();
|
||
|
|
||
|
foreach (var savePoint in savePoints)
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(savePoint.SavePointKey))
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (keys.Contains(savePoint.SavePointKey))
|
||
|
{
|
||
|
Debug.LogError("Save Point Key " + savePoint.SavePointKey + " is defined multiple times.");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
keys.Add(savePoint.SavePointKey);
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
protected void PlayClickSound()
|
||
|
{
|
||
|
if (clickAudioSource != null)
|
||
|
{
|
||
|
clickAudioSource.Play();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <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);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
#region Public methods
|
||
8 years ago
|
|
||
8 years ago
|
public virtual void ToggleSaveMenu()
|
||
|
{
|
||
|
if (fadeTween != null)
|
||
|
{
|
||
8 years ago
|
LeanTween.cancel(fadeTween.id, true);
|
||
8 years ago
|
fadeTween = null;
|
||
|
}
|
||
|
|
||
|
if (saveMenuActive)
|
||
|
{
|
||
8 years ago
|
// Switch menu off
|
||
8 years ago
|
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 0f, 0.5f).setOnUpdate( (t) => {
|
||
|
saveMenuGroup.alpha = t;
|
||
8 years ago
|
}).setOnComplete( () => {
|
||
|
saveMenuGroup.alpha = 0f;
|
||
8 years ago
|
});
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
// Switch menu on
|
||
8 years ago
|
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 1f, 0.5f).setOnUpdate( (t) => {
|
||
|
saveMenuGroup.alpha = t;
|
||
8 years ago
|
}).setOnComplete( () => {
|
||
|
saveMenuGroup.alpha = 1f;
|
||
8 years ago
|
});
|
||
|
}
|
||
|
|
||
|
saveMenuActive = !saveMenuActive;
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Save()
|
||
|
{
|
||
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
8 years ago
|
|
||
|
if (saveManager.NumSavePoints > 0)
|
||
|
{
|
||
|
PlayClickSound();
|
||
|
saveManager.Save(SaveDataKey);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
public virtual void Load()
|
||
|
{
|
||
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
8 years ago
|
|
||
|
if (saveManager.SaveDataExists(SaveDataKey))
|
||
|
{
|
||
|
PlayClickSound();
|
||
|
saveManager.Load(SaveDataKey);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
public virtual void Rewind()
|
||
|
{
|
||
8 years ago
|
PlayClickSound();
|
||
|
|
||
8 years ago
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
8 years ago
|
if (saveManager.NumSavePoints > 1)
|
||
|
{
|
||
|
saveManager.Rewind();
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
public virtual void Forward()
|
||
|
{
|
||
|
PlayClickSound();
|
||
|
|
||
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
|
if (saveManager.NumRewoundSavePoints > 0)
|
||
|
{
|
||
|
saveManager.FastForward();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
public virtual void Restart()
|
||
|
{
|
||
8 years ago
|
if (string.IsNullOrEmpty(startScene))
|
||
|
{
|
||
|
Debug.LogError("No start scene specified");
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
var saveManager = FungusManager.Instance.SaveManager;
|
||
8 years ago
|
|
||
8 years ago
|
PlayClickSound();
|
||
|
|
||
|
saveManager.ClearHistory();
|
||
8 years ago
|
if (restartDeletesSave)
|
||
|
{
|
||
8 years ago
|
saveManager.Delete(SaveDataKey);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
||
8 years ago
|
SceneManager.LoadScene(startScene);
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|