diff --git a/Assets/_Scripts/Core/Game/Data/Currency/CurrencyData.cs b/Assets/_Scripts/Core/Game/Data/Currency/CurrencyData.cs index 8ebf334a99bb27c29725e60b851b42874a40322a..d6764b24fdabb4e0036267d65916889d8103d9e8 100644 --- a/Assets/_Scripts/Core/Game/Data/Currency/CurrencyData.cs +++ b/Assets/_Scripts/Core/Game/Data/Currency/CurrencyData.cs @@ -1,9 +1,47 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + namespace _Scripts.Core.Game.Data.Currency { - public class CurrencyData + [Serializable] + public class CurrencyData: DataClass { - public int Balance = 0; + [Serializable] + public class Transaction + { + public int amount; + public String description; + } - // public List<Transaction> = TransactionHistory; + public int balance; + public List<Transaction> transactions = new(); + + public void AddTransaction(int amount, string description) + { + transactions.Add(new Transaction { amount = amount, description = description }); + balance += amount; + } + public override void Load(string json) + { + CurrencyData data = JsonUtility.FromJson<CurrencyData>(json); + if (data != null) + { + transactions.Clear(); + if (data.transactions != null) + { + foreach (Transaction transaction in data.transactions) + { + transactions.Add(new Transaction { amount = transaction.amount, description = transaction.description }); + } + } + balance = data.balance; + } + } + + public override string SaveToJson() + { + return JsonUtility.ToJson(this); + } } } \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/DataClass.cs b/Assets/_Scripts/Core/Game/Data/DataClass.cs new file mode 100644 index 0000000000000000000000000000000000000000..190a5237001acea600cf11c0972498d9ce2a7a37 --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/DataClass.cs @@ -0,0 +1,8 @@ +namespace _Scripts.Core.Game.Data +{ + public abstract class DataClass + { + public abstract void Load(string json); + public abstract string SaveToJson(); + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/DataClass.cs.meta b/Assets/_Scripts/Core/Game/Data/DataClass.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..e7312644232bb4bd9855451046d1c014bdbc627f --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/DataClass.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1409dd2bfcd949c9b9ac094b146df878 +timeCreated: 1713927410 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/GameSaveData.cs b/Assets/_Scripts/Core/Game/Data/GameSaveData.cs index e4ff7c0edbbcf828420dc3899b71e7edf944b573..0b2c50f0b6082e0ac362528ef2a2a1e672b396ff 100644 --- a/Assets/_Scripts/Core/Game/Data/GameSaveData.cs +++ b/Assets/_Scripts/Core/Game/Data/GameSaveData.cs @@ -1,23 +1,86 @@ using System; using System.Collections.Generic; +using System.IO; +using _Scripts.Core.Game.Data.Currency; +using _Scripts.Core.Game.Data.Position; +using _Scripts.Core.Game.Data.Statistics; +using _Scripts.Core.Game.Data.Story; using Unity.VisualScripting; using UnityEngine; +using UnityEngine.Serialization; -public class GameSaveData : MonoBehaviour +public class GameSaveData : MonoBehaviour { // Static Instance public static GameSaveData Instance; // Attributes public DifficultyType difficulty = DifficultyType.NORMAL; + public List<String> events = new(); // Save the currency of the player - // Save the story state of the player + public CurrencyData currencyData = new(); // Save the position and the level of the player - + public PositionData positionData = new(); + // Save the story state of the player + public StoryData storyData; + public StatisticsData statisticsData = new(); + // Constructor private void Awake() { Instance = this; + storyData = new StoryData(events); DontDestroyOnLoad(gameObject); } + + public void SaveGame() + { + GameDataWrapper wrapper = new GameDataWrapper + { + difficulty = this.difficulty, + events = this.events, + currencyData = this.currencyData, + positionData = this.positionData, + storyData = this.storyData, + statisticsData = this.statisticsData + }; + + string json = JsonUtility.ToJson(wrapper, true); + File.WriteAllText(Application.persistentDataPath + "/savegame.json", json); + Debug.Log("Game saved to " + Application.persistentDataPath + "/savegame.json"); + } + + public void LoadGame() + { + string path = Application.persistentDataPath + "/savegame.json"; + if (File.Exists(path)) + { + string json = File.ReadAllText(path); + GameDataWrapper wrapper = JsonUtility.FromJson<GameDataWrapper>(json); + + this.difficulty = wrapper.difficulty; + this.events = wrapper.events; + this.currencyData = wrapper.currencyData; + this.positionData = wrapper.positionData; + this.storyData = wrapper.storyData; + this.statisticsData = wrapper.statisticsData; + Debug.Log("Game loaded from " + path); + } + else + { + Debug.LogError("Save file not found in " + path); + } + } + +} + +[Serializable] +public class GameDataWrapper +{ + public DifficultyType difficulty; + public List<String> events; + public CurrencyData currencyData; + public PositionData positionData; + public StoryData storyData; + public StatisticsData statisticsData; } diff --git a/Assets/_Scripts/Core/Game/Data/Position.meta b/Assets/_Scripts/Core/Game/Data/Position.meta new file mode 100644 index 0000000000000000000000000000000000000000..dc0226c1dcab44f8f9a2dd65d6578182c0569a96 --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Position.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c180590641784f4dbcbfa7b82df76b14 +timeCreated: 1713925100 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs b/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs new file mode 100644 index 0000000000000000000000000000000000000000..a5ee83617505a41214510a838c6d27b29dca7ffa --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs @@ -0,0 +1,29 @@ +using System; +using UnityEngine; + +namespace _Scripts.Core.Game.Data.Position +{ + [Serializable] + public class PositionData: DataClass + { + public int level = 1; + public Vector3 point = new(0, 0, 0); + + + public override void Load(string json) + { + PositionData data = JsonUtility.FromJson<PositionData>(json); + if (data != null) + { + level = data.level; + point = data.point; + } + + } + + public override string SaveToJson() + { + return JsonUtility.ToJson(this); + } + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs.meta b/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..bb6850992ce6b199b91ab0585e0e02bea09dd02a --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Position/PositionData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 21e6e7f20e444735970bf93c4711fd4d +timeCreated: 1713925119 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Statistics.meta b/Assets/_Scripts/Core/Game/Data/Statistics.meta new file mode 100644 index 0000000000000000000000000000000000000000..71f65bd01f223f83ba89129d0cc05a636d219641 --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Statistics.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4479f1739fe8476aa8170ea9ad5a7feb +timeCreated: 1713929215 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs b/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs new file mode 100644 index 0000000000000000000000000000000000000000..e09753159794073814e644aba76ab363e400dad3 --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs @@ -0,0 +1,24 @@ +using System; +using UnityEngine; + +namespace _Scripts.Core.Game.Data.Statistics +{ + [Serializable] + public class StatisticsData: DataClass + { + public int enemiesKilled = 0; + public override void Load(string json) + { + StatisticsData data = JsonUtility.FromJson<StatisticsData>(json); + if (data != null) + { + enemiesKilled = data.enemiesKilled; + } + } + + public override string SaveToJson() + { + return JsonUtility.ToJson(this); + } + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs.meta b/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..63e1559b18837e89e0fbe954b2820b1009de2bf4 --- /dev/null +++ b/Assets/_Scripts/Core/Game/Data/Statistics/StatisticsData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 699af7536f1e4c5584c4d74309a3eea3 +timeCreated: 1713929223 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Game/Data/Story/StoryData.cs b/Assets/_Scripts/Core/Game/Data/Story/StoryData.cs index b9400a1ab416a6e54dfabb3e02f9d136b8d49e8c..d70226c0561183159ad7a21959300ec2ad8c7913 100644 --- a/Assets/_Scripts/Core/Game/Data/Story/StoryData.cs +++ b/Assets/_Scripts/Core/Game/Data/Story/StoryData.cs @@ -1,9 +1,46 @@ +using System; using System.Collections.Generic; +using UnityEditor.PackageManager; +using UnityEngine; namespace _Scripts.Core.Game.Data.Story { - public class StoryData + [Serializable] + public class StoryData: DataClass { - public List<int> List; + public List<String> events = new(); + public List<Boolean> progress = new(); + + public StoryData(List<String> eventsList) + { + foreach (string eventId in eventsList) + { + events.Add(eventId); + progress.Add(false); + } + } + + public void CompleteEvent(String eventId) + { + int idx = events.FindIndex(0, e => e == eventId); + progress[idx] = true; + } + + public override void Load(string json) + { + StoryData data = JsonUtility.FromJson<StoryData>(json); + if (data != null) + { + events = data.events; + progress = data.progress; + } + + } + + public override string SaveToJson() + { + return JsonUtility.ToJson(this); + } + } } \ No newline at end of file