From 21dc573dbf2575d35508f6bba7fa22aeb2b02006 Mon Sep 17 00:00:00 2001 From: MarcelRyan <marcel.ryan2004@gmail.com> Date: Mon, 6 May 2024 16:05:53 +0700 Subject: [PATCH] [FEAT] default save file --- Assets/Scenes/Level01.unity | 1 + Assets/Scripts/Camera/CameraFollow.cs | 18 ++++- Assets/Scripts/DataUtils/FileDataHandler.cs | 75 +++++++++++++++++++ .../Scripts/DataUtils/FileDataHandler.cs.meta | 11 +++ Assets/Scripts/DataUtils/GameData.cs | 2 +- .../Managers/DataPersistenceManager.cs | 25 ++++++- Assets/Scripts/Managers/PetManager.cs | 4 - Assets/Scripts/Weapon/WeaponManager.cs | 2 +- ProjectSettings/ProjectVersion.txt | 4 +- 9 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 Assets/Scripts/DataUtils/FileDataHandler.cs create mode 100644 Assets/Scripts/DataUtils/FileDataHandler.cs.meta diff --git a/Assets/Scenes/Level01.unity b/Assets/Scenes/Level01.unity index a45e492..a7fca79 100644 --- a/Assets/Scenes/Level01.unity +++ b/Assets/Scenes/Level01.unity @@ -15886,6 +15886,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ff080c65d846fce40916179a81d1a151, type: 3} m_Name: m_EditorClassIdentifier: + fileName: data.game --- !u!1 &2025801928 stripped GameObject: m_CorrespondingSourceObject: {fileID: 2179698135647974157, guid: a6cf23af3ca4087408302fb3311a1fd3, type: 3} diff --git a/Assets/Scripts/Camera/CameraFollow.cs b/Assets/Scripts/Camera/CameraFollow.cs index e02e4fa..e1e6dd7 100644 --- a/Assets/Scripts/Camera/CameraFollow.cs +++ b/Assets/Scripts/Camera/CameraFollow.cs @@ -3,7 +3,7 @@ using System.Collections; namespace Nightmare { - public class CameraFollow : MonoBehaviour + public class CameraFollow : MonoBehaviour, IDataPersistence { public Transform target; // The position that that camera will be following. public float smoothing = 5f; // The speed with which the camera will be following. @@ -15,6 +15,22 @@ namespace Nightmare offset = transform.position - target.position; } + public void LoadData(GameData data) + { + target.position = data.currentPosition; + + Vector3 targetCamPos; + targetCamPos.y = 15f; + targetCamPos.x = target.position.x + transform.position.x; + targetCamPos.z = target.position.z + transform.position.z; + + transform.position = targetCamPos; + } + + public void SaveData(ref GameData data) + { + + } void FixedUpdate () { diff --git a/Assets/Scripts/DataUtils/FileDataHandler.cs b/Assets/Scripts/DataUtils/FileDataHandler.cs new file mode 100644 index 0000000..121d116 --- /dev/null +++ b/Assets/Scripts/DataUtils/FileDataHandler.cs @@ -0,0 +1,75 @@ +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() + { + string fullPath = Path.Combine(dataDirPath, dataFileName); + + GameData loadedData = null; + + if (File.Exists(fullPath)) + { + try + { + // load the serialized data + string dataToLoad = ""; + using (FileStream stream = new FileStream(fullPath, FileMode.Open)) + { + using (StreamReader reader = new StreamReader(stream)) + { + dataToLoad = reader.ReadToEnd(); + } + } + + // deserialize the data from JSON to C# object + loadedData = JsonUtility.FromJson<GameData>(dataToLoad); + } + catch (Exception e) + { + Debug.LogError("Error occured while trying to load data to file : " + fullPath + "\n" + e); + } + } + + return loadedData; + } + + public void Save(GameData data) + { + string fullPath = Path.Combine(dataDirPath, dataFileName); + try + { + // create the directory file + Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); + + // serialize the C# game data object into json + string dataToStore = JsonUtility.ToJson(data, true); + + // write the serialized data to 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 while trying to save data to file : " + fullPath + "\n" + e); + } + } +} diff --git a/Assets/Scripts/DataUtils/FileDataHandler.cs.meta b/Assets/Scripts/DataUtils/FileDataHandler.cs.meta new file mode 100644 index 0000000..21b6653 --- /dev/null +++ b/Assets/Scripts/DataUtils/FileDataHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a0bbb13c8c99a51408365a1289b619d3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/DataUtils/GameData.cs b/Assets/Scripts/DataUtils/GameData.cs index 45cf87e..935212a 100644 --- a/Assets/Scripts/DataUtils/GameData.cs +++ b/Assets/Scripts/DataUtils/GameData.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using UnityEngine; [System.Serializable] -public class GameData : MonoBehaviour +public class GameData { public int score; public float coin; diff --git a/Assets/Scripts/Managers/DataPersistenceManager.cs b/Assets/Scripts/Managers/DataPersistenceManager.cs index 1ed2474..17432a1 100644 --- a/Assets/Scripts/Managers/DataPersistenceManager.cs +++ b/Assets/Scripts/Managers/DataPersistenceManager.cs @@ -4,8 +4,10 @@ using UnityEngine; using System.Linq; public class DataPersistenceManager : MonoBehaviour { + [Header("File Storage Config")] + [SerializeField] private string fileName; + private FileDataHandler dataHandler; public static DataPersistenceManager instance { get; private set; } - private GameData gameData; private List<IDataPersistence> dataPersistenceList; @@ -20,6 +22,7 @@ public class DataPersistenceManager : MonoBehaviour private void Start() { + this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName); this.dataPersistenceList = FindAllDataPersistence(); LoadGame(); } @@ -33,7 +36,12 @@ public class DataPersistenceManager : MonoBehaviour private void OnApplicationQuit() { - SaveGame(); + if (gameData.playerHealth > 0) + { + SaveGame(); + return; + } + NewGame(); } public void NewGame() @@ -44,8 +52,10 @@ public class DataPersistenceManager : MonoBehaviour public void LoadGame() { // TODO - load saved data + this.gameData = dataHandler.Load(); + // if no data can be loaded, initialize new game data - if (this.gameData == null) + if (this.gameData == null || this.gameData.playerHealth <= 0) { Debug.Log("No data was found. Initializing data to defaults"); NewGame(); @@ -57,6 +67,10 @@ public class DataPersistenceManager : MonoBehaviour dataPersistence.LoadData(gameData); } + GameObject player = GameObject.FindGameObjectWithTag("Player"); + + player.transform.position = this.gameData.currentPosition; + Debug.Log("Data Loaded."); } @@ -70,6 +84,11 @@ public class DataPersistenceManager : MonoBehaviour Debug.Log("Data Saved."); } + GameObject player = GameObject.FindGameObjectWithTag("Player"); + + gameData.currentPosition = player.transform.position; + // TODO - save data to a file + dataHandler.Save(gameData); } } diff --git a/Assets/Scripts/Managers/PetManager.cs b/Assets/Scripts/Managers/PetManager.cs index c609add..0c57db3 100644 --- a/Assets/Scripts/Managers/PetManager.cs +++ b/Assets/Scripts/Managers/PetManager.cs @@ -43,8 +43,6 @@ public class PetManager : MonoBehaviour, IDataPersistence { SpawnPet(data.pets[i], data.petsHealth[i]); } - - player.transform.position = data.currentPosition; } public void SaveData(ref GameData data) @@ -70,8 +68,6 @@ public class PetManager : MonoBehaviour, IDataPersistence data.petsHealth.Add(petHealth.currentHealth); } } - - data.currentPosition = player.transform.position; } } diff --git a/Assets/Scripts/Weapon/WeaponManager.cs b/Assets/Scripts/Weapon/WeaponManager.cs index 31fe8ed..c1b3278 100644 --- a/Assets/Scripts/Weapon/WeaponManager.cs +++ b/Assets/Scripts/Weapon/WeaponManager.cs @@ -29,7 +29,7 @@ namespace Nightmare public void LoadData(GameData data) { - this.currIdx = data.currentWeapon; + SwitchWeapon(data.currentWeapon); this.baseDamage = data.baseDamage; } diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index bc915f1..e3269c9 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2022.3.24f1 -m_EditorVersionWithRevision: 2022.3.24f1 (334eb2a0b267) +m_EditorVersion: 2022.3.25f1 +m_EditorVersionWithRevision: 2022.3.25f1 (530ae0ba3889) -- GitLab