173 lines
5.0 KiB
C#
173 lines
5.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using PixelCrushers;
|
|
using PixelCrushers.DialogueSystem;
|
|
using PixelCrushers.QuestMachine;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using QuestState = PixelCrushers.QuestMachine.QuestState;
|
|
|
|
namespace Beyond
|
|
{
|
|
|
|
public class SaveUtility : MonoBehaviour, IMessageHandler
|
|
{
|
|
private const string CURRENT_SLOT = "CURRENT_SLOT";
|
|
private const string CURRENT_DIFFICULTY = "CURRENT_DIFFICULTY";
|
|
private const string SLOT_DESC = "SLOT_DESC";
|
|
private const float SAVE_DELAY = 1f;
|
|
private const double MIN_SAVE_TIME_TH = 10.0;
|
|
private static SaveUtility s_instance = null;
|
|
private DateTime m_time;
|
|
public static SaveUtility Instance
|
|
{
|
|
get
|
|
{
|
|
if (s_instance == null)
|
|
{
|
|
GameObject go = new GameObject("_SaveUtility");
|
|
DontDestroyOnLoad(go);
|
|
s_instance = go.AddComponent<SaveUtility>();
|
|
MessageSystem.AddListener(s_instance, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
|
|
SceneManager.sceneLoaded += (scene, mode) =>
|
|
{
|
|
if (scene.buildIndex != 0)
|
|
{
|
|
Instance.StartCoroutine(Instance.SaveWithDelay(1f));
|
|
}
|
|
};
|
|
HideUI.SetActive += active =>
|
|
{
|
|
if (active)
|
|
{
|
|
s_instance.SaveCheckpoint();
|
|
}
|
|
};
|
|
|
|
}
|
|
return s_instance;
|
|
}
|
|
}
|
|
|
|
public IEnumerator SaveWithDelay(float delay)
|
|
{
|
|
yield return new WaitForSeconds(delay);
|
|
SaveCheckpoint();
|
|
}
|
|
public void OnDestroy()
|
|
{
|
|
MessageSystem.RemoveListener(s_instance, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
|
|
}
|
|
|
|
public int GetCurrentSlot()
|
|
{
|
|
if (PlayerPrefs.HasKey(CURRENT_SLOT))
|
|
{
|
|
return PlayerPrefs.GetInt(CURRENT_SLOT);
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public int GetCurrentDifficulty()
|
|
{
|
|
if (PlayerPrefs.HasKey(CURRENT_DIFFICULTY))
|
|
{
|
|
return PlayerPrefs.GetInt(CURRENT_DIFFICULTY);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
public void SetCurrentDifficulty(int d)
|
|
{
|
|
PlayerPrefs.SetInt(CURRENT_DIFFICULTY, d);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public string GetSlotDesc(int id)
|
|
{
|
|
string slotname = SLOT_DESC + id;
|
|
if (PlayerPrefs.HasKey(slotname))
|
|
{
|
|
return PlayerPrefs.GetString(slotname);
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public void UpdateSlotDesc(int id)
|
|
{
|
|
var now = DateTime.Now;
|
|
PlayerPrefs.SetString(SLOT_DESC+id,now.ToString());
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public void SetCurrentSlot(int num)
|
|
{
|
|
PlayerPrefs.SetInt(CURRENT_SLOT, num);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public void SaveCheckpoint()
|
|
{
|
|
if (m_time != null)
|
|
{
|
|
if ((DateTime.Now - m_time).TotalSeconds < MIN_SAVE_TIME_TH)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
m_time = DateTime.Now;
|
|
if (Player.Instance == null)
|
|
return;
|
|
Player.Instance.OnCheckpoint();
|
|
int currentSlot = GetCurrentSlot();
|
|
if (currentSlot < 0)
|
|
{
|
|
Debug.LogError("Current save slot is <0 !");
|
|
currentSlot = 0;
|
|
}
|
|
SaveSystem.SaveToSlot(currentSlot);
|
|
UpdateSlotDesc(currentSlot);
|
|
//DialogueManager.BarkString("Checkpoint Saved.", Player.Instance.transform);
|
|
}
|
|
|
|
public void ClearCheckpoint()
|
|
{
|
|
int currentSlot = GetCurrentSlot();
|
|
if (currentSlot < 0)
|
|
{
|
|
return;
|
|
}
|
|
SaveSystem.DeleteSavedGameInSlot(currentSlot);
|
|
SetCurrentSlot(-1);
|
|
}
|
|
|
|
public void LoadCheckpoint()
|
|
{
|
|
int currentSlot = GetCurrentSlot();
|
|
if (currentSlot < 0)
|
|
{
|
|
Debug.LogError("Current save slot is <0 !");
|
|
currentSlot = 0;
|
|
return;
|
|
}
|
|
SaveSystem.LoadFromSlot(currentSlot);
|
|
}
|
|
|
|
public void OnMessage(MessageArgs messageArgs)
|
|
{
|
|
switch (messageArgs.message)
|
|
{
|
|
case QuestMachineMessages.QuestStateChangedMessage:
|
|
if ((QuestState)messageArgs.values[1] == QuestState.Active)
|
|
{
|
|
SaveCheckpoint();
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} |