An easy to use Unity 3D library for creating illustrated Interactive Fiction games and more.
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

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace Fungus
{
public class SaveMenu : MonoBehaviour
{
const string SaveDataKey = "save_data";
const string NewGameSavePointKey = "new_game";
[SerializeField] protected bool autoStartGame = true;
[SerializeField] protected bool restartDeletesSave = false;
8 years ago
[SerializeField] protected CanvasGroup saveMenuGroup;
[SerializeField] protected Button saveMenuButton;
[SerializeField] protected Button saveButton;
[SerializeField] protected Button loadButton;
[SerializeField] protected Button rewindButton;
[SerializeField] protected Button forwardButton;
[SerializeField] protected Button restartButton;
protected static bool saveMenuActive = false;
protected AudioSource clickAudioSource;
protected LTDescr fadeTween;
protected static SaveMenu instance;
protected string startScene = "";
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>();
}
protected virtual void Start()
{
startScene = SceneManager.GetActiveScene().name;
var saveManager = FungusManager.Instance.SaveManager;
if (!saveMenuActive)
{
saveMenuGroup.alpha = 0f;
}
if (autoStartGame &&
saveManager.NumSavePoints == 0)
{
SavePointLoaded.NotifyEventHandlers(NewGameSavePointKey);
}
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;
}
if (forwardButton != null)
{
forwardButton.interactable = saveManager.NumRewoundSavePoints > 0;
}
}
/// <summary>
/// Warn if duplicate SavePointKeys are found.
/// </summary>
protected void CheckSavePointKeys()
{
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);
}
}
}
protected void PlayClickSound()
{
if (clickAudioSource != null)
{
clickAudioSource.Play();
}
}
/// <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);
}
#region Public methods
8 years ago
public virtual void ToggleSaveMenu()
{
if (fadeTween != null)
{
LeanTween.cancel(fadeTween.id, true);
8 years ago
fadeTween = null;
}
if (saveMenuActive)
{
// Switch menu off
8 years ago
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 0f, 0.5f).setOnUpdate( (t) => {
saveMenuGroup.alpha = t;
}).setOnComplete( () => {
saveMenuGroup.alpha = 0f;
8 years ago
});
}
else
{
// Switch menu on
8 years ago
LeanTween.value(saveMenuGroup.gameObject, saveMenuGroup.alpha, 1f, 0.5f).setOnUpdate( (t) => {
saveMenuGroup.alpha = t;
}).setOnComplete( () => {
saveMenuGroup.alpha = 1f;
8 years ago
});
}
saveMenuActive = !saveMenuActive;
}
public virtual void Save()
{
var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.NumSavePoints > 0)
{
PlayClickSound();
saveManager.Save(SaveDataKey);
}
}
public virtual void Load()
{
var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.SaveDataExists(SaveDataKey))
{
PlayClickSound();
saveManager.Load(SaveDataKey);
}
}
public virtual void Rewind()
{
PlayClickSound();
var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.NumSavePoints > 1)
{
saveManager.Rewind();
}
}
public virtual void Forward()
{
PlayClickSound();
var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.NumRewoundSavePoints > 0)
{
saveManager.FastForward();
}
}
public virtual void Restart()
{
if (string.IsNullOrEmpty(startScene))
{
Debug.LogError("No start scene specified");
return;
}
var saveManager = FungusManager.Instance.SaveManager;
PlayClickSound();
saveManager.ClearHistory();
if (restartDeletesSave)
{
saveManager.Delete(SaveDataKey);
}
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(startScene);
}
#endregion
}
}