refactor: level resource loader

This commit is contained in:
Théo LUDWIG 2025-02-09 19:52:35 +01:00
parent 707dee5063
commit 50945f807d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
9 changed files with 33 additions and 63 deletions

View File

@ -1,16 +0,0 @@
{
"Name": "Back on Track",
"MusicName": "BackOnTrack.mp3",
"TotalJumps": 0,
"TotalAttempts": 0,
"KilledCount": 0,
"elements": [
{
"type": "Spike",
"position": {
"x": 0,
"y": 0
}
}
]
}

View File

@ -0,0 +1,16 @@
{
"name": "Back on Track",
"musicName": "BackOnTrack",
"totalJumps": 0,
"totalAttempts": 0,
"killedCount": 0,
"elements": [
{
"type": "Spike",
"position": {
"x": 0,
"y": 0
}
}
]
}

View File

@ -1,20 +1,16 @@
using System.Runtime.Serialization;
using UnityEngine;
[DataContract]
[System.Serializable]
public class Level
{
[DataMember]
public string Name { get; set; }
public string name;
public string musicName;
public int totalJumps;
public int totalAttempts;
public int killedCount;
[DataMember]
public string MusicName { get; set; }
[DataMember]
public int TotalJumps { get; set; }
[DataMember]
public int TotalAttempts { get; set; }
[DataMember]
public int KilledCount { get; set; }
public static Level CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Level>(jsonString);
}
}

View File

@ -1,5 +1,4 @@
using System.IO;
using System.Runtime.Serialization.Json;
using UnityEngine;
public class LevelsLoader : MonoBehaviour
@ -10,22 +9,7 @@ public class LevelsLoader : MonoBehaviour
{
DontDestroyOnLoad(gameObject);
// var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
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);
}
TextAsset jsonTextFile = Resources.Load<TextAsset>(Path.Combine("Levels", "BackOnTrack"));
level = JsonUtility.FromJson<Level>(jsonTextFile.text);
}
}

View File

@ -10,6 +10,6 @@ public class LevelNameText : MonoBehaviour
public void Start()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
levelNameText.text = levelsLoader.level.Name;
levelNameText.text = levelsLoader.level.name;
}
}

View File

@ -1,7 +1,6 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.Audio;
public class Player : MonoBehaviour
{
@ -20,17 +19,8 @@ public class Player : MonoBehaviour
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
string musicPath = Path.Combine("Musics", Path.GetFileNameWithoutExtension(levelsLoader.level.MusicName));
AudioClip clip = Resources.Load<AudioClip>(musicPath);
if (clip == null)
{
Debug.LogError("Music file not found at: " + musicPath);
}
else
{
audioSource.clip = clip;
audioSource.Play();
}
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.level.musicName));
audioSource.Play();
var mainModule = particle.main;
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;

View File

@ -17,7 +17,7 @@ class GameManager {
class Level {
- name: String
- musicPath: String
- musicName: String
+ StartLevel()
+ EndLevel()
+ CheckCompletion(): Boolean