diff --git a/Assets/Scripts/DataPersistence/DataPersistenceManager.cs b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs index 0aa0a9906d047065aa1890235befcabf162a21ed..1b0a77dd17559cb3fb0a2239b89bb64a222682ed 100644 --- a/Assets/Scripts/DataPersistence/DataPersistenceManager.cs +++ b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs @@ -1,80 +1,80 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using System.Linq; +//using System.Collections; +//using System.Collections.Generic; +//using UnityEngine; +//using System.Linq; -public class DataPersistenceManager : MonoBehaviour -{ - [Header("File Storage Config")] - [SerializeField] private string fileName; +//public class DataPersistenceManager : MonoBehaviour +//{ +// [Header("File Storage Config")] +// [SerializeField] private string fileName; - private GameData gameData; - private List<IDataPersistence> dataPersistenceObjects; - private FileDataHandler dataHandler; +// private GameData gameData; +// private List<IDataPersistence> dataPersistenceObjects; +// private FileDataHandler dataHandler; - public static DataPersistenceManager instance { get; private set; } +// public static DataPersistenceManager instance { get; private set; } - private void Awake() - { - if (instance != null) - { - Debug.LogError("Found more than one Data Persistence Manager in the scene.") - }; - instance = this; - } +// private void Awake() +// { +// if (instance != null) +// { +// Debug.LogError("Found more than one Data Persistence Manager in the scene."); +// }; +// instance = this; +// } - private void Start() - { - this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName); - this.dataPersistenceObjects = FindAllDataPersistenceObjects(); - LoadGame(); - } +// private void Start() +// { +// this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName); +// this.dataPersistenceObjects = FindAllDataPersistenceObjects(); +// LoadGame(); +// } - public void NewGame() - { - this.gameData = new GameData(); - } +// public void NewGame() +// { +// this.gameData = new GameData(); +// } - public void LoadGame() - { - // load any saved data from a file using data handler - this.gameData = dataHandler.Load(); +// public void LoadGame() +// { +// // load any saved data from a file using data handler +// this.gameData = dataHandler.Load(); - // if no data can be loaded, initialize to a new game - if (this.gameData == null) - { - Debug.Log("Mo data was found. Initializing data to defaults"); - NewGame(); - } - // push the loaded data to all other scripts that need it - foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) - { - dataPersistenceObj.LoadData(gameData); - } - Debug.Log("Loaded game data"); - } +// // if no data can be loaded, initialize to a new game +// if (this.gameData == null) +// { +// Debug.Log("Mo data was found. Initializing data to defaults"); +// NewGame(); +// } +// // push the loaded data to all other scripts that need it +// foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) +// { +// dataPersistenceObj.LoadData(gameData); +// } +// Debug.Log("Loaded game data"); +// } - public void SaveGame() - { - // pass the data to other scripts so they can update it - foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) - { - dataPersistenceObj.SaveData(ref gameData); - } - Debug.Log("Saved game data"); +// public void SaveGame() +// { +// // pass the data to other scripts so they can update it +// foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) +// { +// dataPersistenceObj.SaveData(ref gameData); +// } +// Debug.Log("Saved game data"); - // save that data to a file using data handler - dataHandler.Save(gameData); - } +// // save that data to a file using data handler +// dataHandler.Save(gameData); +// } - private void OnApplicationQuit() - { - SaveGame(); - } +// private void OnApplicationQuit() +// { +// SaveGame(); +// } - private List<IDataPersistence> FindAllDataPersistenceObjects() - { - IEnumerable<IDataPersistence> dataPersistenceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistence>(); - return new List<IDataPersistence>(dataPersistenceObjects); - } -} +// private List<IDataPersistence> FindAllDataPersistenceObjects() +// { +// IEnumerable<IDataPersistence> dataPersistenceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistence>(); +// return new List<IDataPersistence>(dataPersistenceObjects); +// } +//} diff --git a/Assets/Scripts/DataPersistence/FileDataHandler.cs b/Assets/Scripts/DataPersistence/FileDataHandler.cs index bada6ff06cf8003552c878ae83feb07bc16599ea..6f9f17bff89605f296c434bacb81c2e155e9fa82 100644 --- a/Assets/Scripts/DataPersistence/FileDataHandler.cs +++ b/Assets/Scripts/DataPersistence/FileDataHandler.cs @@ -1,73 +1,73 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using System; -using System.IO; +//using System.Collections; +//using System.Collections.Generic; +//using UnityEngine; +//using System; +//using System.IO; -public class FileDataHandler -{ - private string dataDirPath = ""; - private string dataFileName = ""; +//public class FileDataHandler +//{ +// private string dataDirPath = ""; +// private string dataFileName = ""; - public FileDataHandler(string dataDirPath, string dataFileName) - { - this.dataDirPath = dataDirPath; - this.dataFileName = dataFileName; - } +// public FileDataHandler(string dataDirPath, string dataFileName) +// { +// this.dataDirPath = dataDirPath; +// this.dataFileName = dataFileName; +// } - public GameData Load() - { - // use Path.combine to account for different OS's having different path separators - string fullPath = Path.Combine(dataDirPath, dataFileName); - GameData loadedData = null; - if (File.Exists(fullPath)) - { - try - { - // load the serialized data from the file - string dataToLoad = ""; - using (FileStream stream = new FileStream(fullPath, FileMode.Open)) - { - using (StreamReader reader = new StreamReader(stream)) - { - dataToLoad = reader.ReadToEnd(); - } - } +// public GameData Load() +// { +// // use Path.combine to account for different OS's having different path separators +// string fullPath = Path.Combine(dataDirPath, dataFileName); +// GameData loadedData = null; +// if (File.Exists(fullPath)) +// { +// try +// { +// // load the serialized data from the file +// string dataToLoad = ""; +// using (FileStream stream = new FileStream(fullPath, FileMode.Open)) +// { +// using (StreamReader reader = new StreamReader(stream)) +// { +// dataToLoad = reader.ReadToEnd(); +// } +// } - //deserialize the data from Json back into the C# object - loadedData = JsonUtility.FromJson<GameData>(dataToLoad); - } - catch (Exception e) - { - Debug.LogError("Error occured when trying to load data to file: " + fullPath + "\n" + e); - } - } - } +// //deserialize the data from Json back into the C# object +// loadedData = JsonUtility.FromJson<GameData>(dataToLoad); +// } +// catch (Exception e) +// { +// Debug.LogError("Error occured when trying to load data to file: " + fullPath + "\n" + e); +// } +// } +// } - public void Save(GameData data) - { - // use Path.combine to account for different OS's having different path separators - string fullPath = Path.Combine(dataDirPath, dataFileName); - try - { - // create the directory the file will be written to if it doesn't already exist - Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); +// public void Save(GameData data) +// { +// // use Path.combine to account for different OS's having different path separators +// string fullPath = Path.Combine(dataDirPath, dataFileName); +// try +// { +// // create the directory the file will be written to if it doesn't already exist +// Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); - //serialize the C# game data object into Json - string dataToStore = JsonUtility.ToJson(data, true); +// //serialize the C# game data object into Json +// string dataToStore = JsonUtility.ToJson(data, true); - // write the serialized data to the file - using (FileStream stream = new FileStream(fullPath, FileMode.Create)) - { - using (StreamWriter writer = new StreamWriter(stream)) - { - writer.Write(dataToStore); - } - } - } - catch (Exception e) - { - Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e); - } - } -} +// // write the serialized data to the file +// using (FileStream stream = new FileStream(fullPath, FileMode.Create)) +// { +// using (StreamWriter writer = new StreamWriter(stream)) +// { +// writer.Write(dataToStore); +// } +// } +// } +// catch (Exception e) +// { +// Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e); +// } +// } +//} diff --git a/Assets/Scripts/SaveManager.cs b/Assets/Scripts/SaveManager.cs index 85bdd4c6338fd6ce9db6798b3092451a32ea7243..5eafba7670eee223e7beb93da9505c011cd58395 100644 --- a/Assets/Scripts/SaveManager.cs +++ b/Assets/Scripts/SaveManager.cs @@ -1,73 +1,74 @@ -using System; -using System.Collections.Generic; -using System.IO; -using UnityEngine; +//using System; +//using System.Collections.Generic; +//using System.IO; +//using UnityEngine; -public class SaveManager : MonoBehaviour -{ - private string saveFileName = "savegame.json"; // Change this to your desired filename - public void SaveGame(SaveData saveData) - { - try - { - string jsonData = JsonUtility.ToJson(saveData); - File.WriteAllText(Application.persistentDataPath + "/" + saveFileName, jsonData); - } - catch (Exception e) - { - Debug.LogError("Error saving game: " + e.Message); - } - } +//public class SaveManager : MonoBehaviour +//{ +// private string saveFileName = "savegame.json"; // Change this to your desired filename - public SaveData LoadGame() - { - if (File.Exists(Application.persistentDataPath + "/" + saveFileName)) - { - string jsonData = File.ReadAllText(Application.persistentDataPath + "/" + saveFileName); - return JsonUtility.FromJson<SaveData>(jsonData); - } - else - { - return new SaveData(); // Return a default save data if no save file exists - } - } -} +// public void SaveGame(SaveData saveData) +// { +// try +// { +// string jsonData = JsonUtility.ToJson(saveData); +// File.WriteAllText(Application.persistentDataPath + "/" + saveFileName, jsonData); +// } +// catch (Exception e) +// { +// Debug.LogError("Error saving game: " + e.Message); +// } +// } -public class SaveSlotManager : MonoBehaviour -{ - private SaveManager saveManager; - private List<SaveData> saveSlots = new List<SaveData>(); +// public SaveData LoadGame() +// { +// if (File.Exists(Application.persistentDataPath + "/" + saveFileName)) +// { +// string jsonData = File.ReadAllText(Application.persistentDataPath + "/" + saveFileName); +// return JsonUtility.FromJson<SaveData>(jsonData); +// } +// else +// { +// return new SaveData(); // Return a default save data if no save file exists +// } +// } +//} - public int GetNumSaveSlots() - { - return saveSlots.Count; // Assuming saveSlots holds the save data - } +//public class SaveSlotManager : MonoBehaviour +//{ +// private SaveManager saveManager; +// private List<SaveData> saveSlots = new List<SaveData>(); - private void Start() - { - saveManager = GetComponent<SaveManager>(); - LoadSaveSlots(); // Load save data from existing save files - } +// public int GetNumSaveSlots() +// { +// return saveSlots.Count; // Assuming saveSlots holds the save data +// } - public void SaveGameToSlot(int slotIndex, SaveData saveData) - { - saveSlots[slotIndex] = saveData; - SaveSaveSlots(); // Save updated save data to files - } +// private void Start() +// { +// saveManager = GetComponent<SaveManager>(); +// LoadSaveSlots(); // Load save data from existing save files +// } - public SaveData LoadGameFromSlot(int slotIndex) - { - return saveSlots[slotIndex]; - } +// public void SaveGameToSlot(int slotIndex, SaveData saveData) +// { +// saveSlots[slotIndex] = saveData; +// SaveSaveSlots(); // Save updated save data to files +// } - private void LoadSaveSlots() - { - // Load save data from multiple save files - } +// public SaveData LoadGameFromSlot(int slotIndex) +// { +// return saveSlots[slotIndex]; +// } - private void SaveSaveSlots() - { - // Save updated save data to multiple files - } -} \ No newline at end of file +// private void LoadSaveSlots() +// { +// // Load save data from multiple save files +// } + +// private void SaveSaveSlots() +// { +// // Save updated save data to multiple files +// } +//} \ No newline at end of file