miepzerino
2023-12-23 fe3c8a5515acdfcc50913d5b83de8f9504e95b73
Assets/Scripts/SaveSystem.cs
@@ -5,14 +5,13 @@
public static class SaveSystem
{
    public static bool isGameLoaded = false;
    public static void SavePlayer(PlayerController player)
    public static void SavePlayer(SaveDataPlayer player)
    {
        if (player != null)
        {
            string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile");
            SaveData saveData = new SaveData(player);
            string saveDataJSON = JsonUtility.ToJson(saveData);
            string saveDataJSON = JsonUtility.ToJson(player);
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
@@ -25,13 +24,13 @@
        }
        else
        {
            Debug.Log("Not saved as no playerController was sent");
            Debug.Log("Not saved as no playerData was sent");
        }
    }
    public static SaveData LoadPlayer ()
    public static SaveDataPlayer LoadPlayer()
    {
        SaveData result;
        SaveDataPlayer result;
        string path = System.IO.Path.Combine(Application.persistentDataPath, "player.savefile");
        if (File.Exists(path))
        {
@@ -40,7 +39,51 @@
                using (StreamReader sr = new StreamReader(fs))
                {
                    string saveData = sr.ReadToEnd();
                    result = JsonUtility.FromJson<SaveData>(saveData);
                    result = JsonUtility.FromJson<SaveDataPlayer>(saveData);
                }
            }
        }
        else
        {
            Debug.LogError("Save file not found in: " + path);
            return null;
        }
        return result;
    }
    public static void SaveMapState(SaveDataMap mapState)
    {
        if (mapState != null)
        {
            string path = System.IO.Path.Combine(Application.persistentDataPath, "map.savefile");
            string saveDataJSON = JsonUtility.ToJson(mapState);
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(saveDataJSON);
                }
            }
        }
        else
        {
            Debug.Log("Not saved as no mapData was sent");
        }
    }
    public static SaveDataMap LoadMapState()
    {
        SaveDataMap result;
        string path = System.IO.Path.Combine(Application.persistentDataPath, "map.savefile");
        if (File.Exists(path))
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    string saveData = sr.ReadToEnd();
                    result = JsonUtility.FromJson<SaveDataMap>(saveData);
                }
            }
        }