mirror of
				https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
				synced 2025-06-27 11:58:51 +02:00 
			
		
		
		
	refactor: level resource loader
This commit is contained in:
		@@ -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
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								Assets/Resources/Levels/BackOnTrack.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Assets/Resources/Levels/BackOnTrack.json
									
									
									
									
									
										Normal 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
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.level.musicName));
 | 
			
		||||
        audioSource.Play();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var mainModule = particle.main;
 | 
			
		||||
        mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ class GameManager {
 | 
			
		||||
 | 
			
		||||
class Level {
 | 
			
		||||
  - name: String
 | 
			
		||||
  - musicPath: String
 | 
			
		||||
  - musicName: String
 | 
			
		||||
  + StartLevel()
 | 
			
		||||
  + EndLevel()
 | 
			
		||||
  + CheckCompletion(): Boolean
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user