diff --git a/Assets/LevelLoader.cs b/Assets/LevelLoader.cs index 8ef16ce..7187874 100644 --- a/Assets/LevelLoader.cs +++ b/Assets/LevelLoader.cs @@ -1,38 +1,41 @@ +using System.IO; +using System.Runtime.Serialization.Json; using UnityEngine; using UnityEngine.UI; -using System.IO; -using System.Text.Json; public class LevelLoader : MonoBehaviour { - [SerializeField] public Text levelNameText; + private Level level; void Start() { - levelNameText.text = "Coucou"; - string filePath = "level.json"; - - if (File.Exists(filePath)) + LoadLevel(); + if (level != null) { - string json = File.ReadAllText(filePath); - Level level = JsonSerializer.Deserialize(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); - - Console.WriteLine($"Name: {level.Name}"); - Console.WriteLine($"MusicPath: {level.MusicPath}"); - Console.WriteLine($"TotalJumps: {level.TotalJumps}"); - Console.WriteLine($"TotalAttempts: {level.TotalAttempts}"); - Console.WriteLine($"KilledCount: {level.KilledCount}"); + levelNameText.text = level.Name; } else { - Console.WriteLine("JSON file not found."); + levelNameText.text = "Failed to Load Level"; } - } - void Update() + void LoadLevel() { - + string path = Path.Combine(Application.dataPath, "Levels", "back-on-track.json"); + if (File.Exists(path)) + { + string json = File.ReadAllText(path); + using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))) + { + DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Level)); + level = (Level)serializer.ReadObject(stream); + } + } + else + { + Debug.LogError("Level file not found: " + path); + } } } diff --git a/Assets/Levels/back-on-track.json b/Assets/Levels/back-on-track.json index 765836b..fa0fff9 100644 --- a/Assets/Levels/back-on-track.json +++ b/Assets/Levels/back-on-track.json @@ -1,6 +1,9 @@ { - "name": "Back on Track", - "music": "", + "Name": "Back on Track", + "MusicPath": "back-on-track.mp3", + "TotalJumps": 0, + "TotalAttempts": 0, + "KilledCount": 0, "elements": [ { "type": "Spike",