Files
HauntedBloodlines/Assets/Scripts/SaveLoad/SaveSlotManager.cs
2025-05-29 22:31:40 +03:00

265 lines
8.6 KiB
C#

using UnityEngine;
using UnityEngine.Playables;
using System.IO;
public class SaveSlotManager : MonoBehaviour
{
public PlayableDirector savingUIPlayable;
public Animator loadingUIAnimator;
public GameObject LoadingScreen;
public int selectedSaveSlotID; // Replace with the actual save slot ID chosen by the player
private static SaveSlotManager _instance;
public static SaveSlotManager GetInstance() { return _instance; }
private void Awake()
{
if (!_instance)
{
_instance = this;
}
}
private void OnEnable()
{
//If there is no save game file at all then create on in save slot0.
//Implementation:
if (!IsSaveSlotAvailable(0) && !IsSaveSlotAvailable(1) && !IsSaveSlotAvailable(2)) //If saveslot 0,1,2 does not exist.
{
CreateNewSaveSlot(0);
SetChosenSaveSlot(0);
print("Making a new save slot because there is none!");
}
else
{
#region Set chosen SaveSlot
selectedSaveSlotID = GetChosenSaveSlot();
#endregion
}
}
//private void Start()
//{
// //If there is no save game file at all then create on in save slot0.
// //Implementation:
// if (!IsSaveSlotAvailable(0) && !IsSaveSlotAvailable(1) && !IsSaveSlotAvailable(0)) //If saveslot 0,1,2 does not exist.
// {
// CreateNewSaveSlot(0);
// SetChosenSaveSlot(0);
// print("Making a new save slot because there is none!");
// }
//}
public void SetChosenSaveSlot(int saveSlotID)
{
PlayerPrefs.SetInt("ChosenSaveSlot", saveSlotID);
#region Set chosen SaveSlot
selectedSaveSlotID = GetChosenSaveSlot();
#endregion
}
public int GetChosenSaveSlot()
{
return PlayerPrefs.GetInt("ChosenSaveSlot", 0); // -1 is a default value if the key is not found
}
// Create a new save slot
public void CreateNewSaveSlot(int saveSlotID)
{
if (IsSaveSlotAvailable(saveSlotID))
{ //Delete saved data when you make a new slot.
string filePath = GetSaveSlotFilePath(saveSlotID);
if (File.Exists(filePath))
{
File.Delete(filePath);
Debug.Log($"Deleted save data for slot");
print("Deleted data");
}
PlayerData playerData = new PlayerData();
// Initialize player data here
GameOptionsData gameOptionsData = new GameOptionsData();
// Initialize game options data here
GameProgressData gameProgressData = new GameProgressData();
CollectablesData collectablesData = new CollectablesData();
InventoryData inventoryData = new InventoryData();
SceneSaveData sceneSaveData = new SceneSaveData();
GameSaveData newData = new GameSaveData();
newData.saveSlot = saveSlotID;
newData.playerData = playerData;
newData.gameOptionsData = gameOptionsData;
newData.gameProgressData = gameProgressData;
newData.collectablesData = collectablesData;
newData.inventoryData = inventoryData;
newData.saveTimestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
newData.playerData.continueButtonEnabled = false;
newData.sceneSaveData = sceneSaveData;
if (GameplayController.GetInstance().IsOnState(GameplayController.State.GameLaunched) || GameplayController.GetInstance().IsOnState(GameplayController.State.MainMenu))
{
MainMenu.GetInstance().continueButtonIsNotInteractable();
}
SavePlayerData(newData);
}
else
{
Debug.LogWarning("Save slot is not available.");
print("Creating new save slot");
PlayerData playerData = new PlayerData();
// Initialize player data here
GameOptionsData gameOptionsData = new GameOptionsData();
// Initialize game options data here
GameProgressData gameProgressData = new GameProgressData();
CollectablesData collectablesData = new CollectablesData();
InventoryData inventoryData = new InventoryData();
SceneSaveData sceneSaveData = new SceneSaveData();
GameSaveData newData = new GameSaveData();
newData.saveSlot = saveSlotID;
newData.playerData = playerData;
newData.gameOptionsData = gameOptionsData;
newData.gameProgressData = gameProgressData;
newData.collectablesData = collectablesData;
newData.inventoryData = inventoryData;
newData.sceneSaveData = sceneSaveData;
newData.saveTimestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
newData.playerData.continueButtonEnabled = false;
if (GameplayController.GetInstance().IsOnState(GameplayController.State.GameLaunched) || GameplayController.GetInstance().IsOnState(GameplayController.State.MainMenu))
{
MainMenu.GetInstance().continueButtonIsNotInteractable();
}
SavePlayerData(newData);
}
}
private bool IsSaveSlotAvailable(int saveSlotID)
{
string path = GetSaveSlotFilePath(saveSlotID);
// Debugging: Print the file path to check its correctness
Debug.Log($"Checking save slot availability for path: {path}");
// Ensure the directory exists
string directoryPath = Path.GetDirectoryName(path);
if (!Directory.Exists(directoryPath))
{
Debug.Log($"Directory doesn't exist, creating: {directoryPath}");
Directory.CreateDirectory(directoryPath);
}
return /*!*/File.Exists(path);
}
// Save player data to a specific save slot
public static void SavePlayerData(GameSaveData data)
{
string json = JsonUtility.ToJson(data);
File.WriteAllText(GetSaveSlotFilePath(data.saveSlot), json);
}
public static void UpdateAndSaveData(GameSaveData newData)
{
// Load existing data
GameSaveData existingData = LoadPlayerData(newData.saveSlot);
// Merge new data with existing data
if (existingData != null)
{
if (newData.playerData != null)
{
// Update player data if provided
existingData.playerData = newData.playerData;
}
if (newData.gameOptionsData != null)
{
// Update game options data if provided
existingData.gameOptionsData = newData.gameOptionsData;
}
if (newData.gameOptionsData != null)
{
// Update game options data if provided
existingData.gameProgressData = newData.gameProgressData;
}
if(newData.collectablesData != null)
{
//Update collectables data if provided
existingData.collectablesData = newData.collectablesData;
}
if (newData.inventoryData != null)
{
//Update inventory data if provided
existingData.inventoryData = newData.inventoryData;
}
if(newData.sceneSaveData != null)
{
existingData.sceneSaveData = newData.sceneSaveData;
}
}
else
{
// If there's no existing data, save the new data as is
existingData = newData;
}
// Save the merged data
SavePlayerData(existingData);
}
// Load player data from a specific save slot
public static GameSaveData LoadPlayerData(int saveSlot)
{
string path = GetSaveSlotFilePath(saveSlot);
if (File.Exists(path))
{
string json = File.ReadAllText(path);
return JsonUtility.FromJson<GameSaveData>(json);
}
else
{
Debug.LogWarning($"Save file for slot {saveSlot} not found.");
return null;
}
}
public static SettingsData LoadSettingsData(string path)
{
if (File.Exists(path))
{
string json = File.ReadAllText(path);
return JsonUtility.FromJson<SettingsData>(json);
}
else
{
Debug.LogWarning($"Save file for LoadSettingsData not found.");
return null;
}
}
// Get the file path for a save slot
private static string GetSaveSlotFilePath(int saveSlot)
{
return Application.persistentDataPath + $"/saveSlot_{saveSlot}.json";
}
}