diff --git a/Assets/Scripts/DataPersistence.meta b/Assets/Scripts/DataPersistence.meta new file mode 100644 index 0000000000000000000000000000000000000000..47669cca986d88213601ded5cd6a241bf849af40 --- /dev/null +++ b/Assets/Scripts/DataPersistence.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e7fc8ed2bdad40e419949dcbef76dba9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataPersistence/Data.meta b/Assets/Scripts/DataPersistence/Data.meta new file mode 100644 index 0000000000000000000000000000000000000000..7ef8ea983bf5743167dabb199611380a080a110c --- /dev/null +++ b/Assets/Scripts/DataPersistence/Data.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0a7799ad29900e243b18f05ebf641e14 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataPersistence/Data/GameData.cs b/Assets/Scripts/DataPersistence/Data/GameData.cs new file mode 100644 index 0000000000000000000000000000000000000000..3efdecc5d35ca3653eac2a8f826bcd506c6cfe9e --- /dev/null +++ b/Assets/Scripts/DataPersistence/Data/GameData.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[System.Serializable] +public class GameData +{ + public int currentLevel; + public Vector3 playerPosition; + + // from PlayerHealth.cs + public int currentHealth; + public bool godMode; + + // from PlayerMovement.cs + public float speed; + public bool isDoubleSpeed; + + // from PlayerShooting.cs + public int damagePerShot; + public bool isOneHitKill; + + // from PlayerGold.cs + public int currentGold; + public bool isMotherlode; + + public List<PetData> pet = new List<PetData>(); + + + // the values defined in this constructor will be the default values + // the game starts with when there's no data to load + public GameData() + { + + } +} + +[System.Serializable] +public class PetData +{ + public string type; + public int currentHealth; + public int damagePerShot; + public float speed; +} \ No newline at end of file diff --git a/Assets/Scripts/DataPersistence/Data/GameData.cs.meta b/Assets/Scripts/DataPersistence/Data/GameData.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..024fbe05093d6c4551789690800b526272cf5161 --- /dev/null +++ b/Assets/Scripts/DataPersistence/Data/GameData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3c28cba78b9fedf4d9b02eda17c76378 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataPersistence/DataPersistenceManager.cs b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs new file mode 100644 index 0000000000000000000000000000000000000000..0aa0a9906d047065aa1890235befcabf162a21ed --- /dev/null +++ b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs @@ -0,0 +1,80 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.Linq; + +public class DataPersistenceManager : MonoBehaviour +{ + [Header("File Storage Config")] + [SerializeField] private string fileName; + + private GameData gameData; + private List<IDataPersistence> dataPersistenceObjects; + private FileDataHandler dataHandler; + + 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 Start() + { + this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName); + this.dataPersistenceObjects = FindAllDataPersistenceObjects(); + LoadGame(); + } + + public void NewGame() + { + this.gameData = new GameData(); + } + + 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"); + } + + 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); + } + + private void OnApplicationQuit() + { + SaveGame(); + } + + private List<IDataPersistence> FindAllDataPersistenceObjects() + { + IEnumerable<IDataPersistence> dataPersistenceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistence>(); + return new List<IDataPersistence>(dataPersistenceObjects); + } +} diff --git a/Assets/Scripts/DataPersistence/DataPersistenceManager.cs.meta b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..6b91d7510b52e7ab83d73551582c6470dbcfcb3e --- /dev/null +++ b/Assets/Scripts/DataPersistence/DataPersistenceManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3dcebec8b76c1cb44ac0cb6a99c71ac6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataPersistence/FileDataHandler.cs b/Assets/Scripts/DataPersistence/FileDataHandler.cs new file mode 100644 index 0000000000000000000000000000000000000000..bada6ff06cf8003552c878ae83feb07bc16599ea --- /dev/null +++ b/Assets/Scripts/DataPersistence/FileDataHandler.cs @@ -0,0 +1,73 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; +using System.IO; + +public class FileDataHandler +{ + private string dataDirPath = ""; + private string 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(); + } + } + + //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)); + + //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); + } + } +} diff --git a/Assets/Scripts/DataPersistence/FileDataHandler.cs.meta b/Assets/Scripts/DataPersistence/FileDataHandler.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..4ede2b2aeb3a4d119cbb99378ad7407350dcade5 --- /dev/null +++ b/Assets/Scripts/DataPersistence/FileDataHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 09e9d558054597e408a7c247b33606db +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataPersistence/IDataPersistence.cs b/Assets/Scripts/DataPersistence/IDataPersistence.cs new file mode 100644 index 0000000000000000000000000000000000000000..dc7749d87a22e8f9f4aa7f3a0140c1d309b0c33b --- /dev/null +++ b/Assets/Scripts/DataPersistence/IDataPersistence.cs @@ -0,0 +1,9 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public interface IDataPersistence +{ + void LoadData(GameData data); + void SaveData(ref GameData data); +} diff --git a/Assets/Scripts/DataPersistence/IDataPersistence.cs.meta b/Assets/Scripts/DataPersistence/IDataPersistence.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..ee40ca73e3c8298f706f08c4f9a4ec81a9e95eca --- /dev/null +++ b/Assets/Scripts/DataPersistence/IDataPersistence.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68658cb79c76d4041b7ba6999d30ffa1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SaveData.cs b/Assets/Scripts/SaveData.cs index 28fec9f17157c6856f4daf6f4e722e51b9f741f9..52fa8bbcb10c3bfaa54b11a942c07b7e709e6213 100644 --- a/Assets/Scripts/SaveData.cs +++ b/Assets/Scripts/SaveData.cs @@ -1,3 +1,4 @@ +/* using static UnityEditor.Progress; using System.Collections.Generic; using System; @@ -60,3 +61,4 @@ public class PetData public int damagePerShot; public float speed; } +*/ \ No newline at end of file