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
 | 
					public class Level
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    [DataMember]
 | 
					    public string name;
 | 
				
			||||||
    public string Name { get; set; }
 | 
					    public string musicName;
 | 
				
			||||||
 | 
					    public int totalJumps;
 | 
				
			||||||
 | 
					    public int totalAttempts;
 | 
				
			||||||
 | 
					    public int killedCount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [DataMember]
 | 
					    public static Level CreateFromJSON(string jsonString)
 | 
				
			||||||
    public string MusicName { get; set; }
 | 
					    {
 | 
				
			||||||
 | 
					        return JsonUtility.FromJson<Level>(jsonString);
 | 
				
			||||||
    [DataMember]
 | 
					    }
 | 
				
			||||||
    public int TotalJumps { get; set; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    [DataMember]
 | 
					 | 
				
			||||||
    public int TotalAttempts { get; set; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    [DataMember]
 | 
					 | 
				
			||||||
    public int KilledCount { get; set; }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,4 @@
 | 
				
			|||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Runtime.Serialization.Json;
 | 
					 | 
				
			||||||
using UnityEngine;
 | 
					using UnityEngine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class LevelsLoader : MonoBehaviour
 | 
					public class LevelsLoader : MonoBehaviour
 | 
				
			||||||
@@ -10,22 +9,7 @@ public class LevelsLoader : MonoBehaviour
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        DontDestroyOnLoad(gameObject);
 | 
					        DontDestroyOnLoad(gameObject);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //  var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
 | 
					        TextAsset jsonTextFile = Resources.Load<TextAsset>(Path.Combine("Levels", "BackOnTrack"));
 | 
				
			||||||
        //Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
 | 
					        level = JsonUtility.FromJson<Level>(jsonTextFile.text);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        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);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,6 @@ public class LevelNameText : MonoBehaviour
 | 
				
			|||||||
    public void Start()
 | 
					    public void Start()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
 | 
					        levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
 | 
				
			||||||
        levelNameText.text = levelsLoader.level.Name;
 | 
					        levelNameText.text = levelsLoader.level.name;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,6 @@
 | 
				
			|||||||
using UnityEngine;
 | 
					using UnityEngine;
 | 
				
			||||||
using UnityEngine.SceneManagement;
 | 
					using UnityEngine.SceneManagement;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using UnityEngine.Audio;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class Player : MonoBehaviour
 | 
					public class Player : MonoBehaviour
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -20,17 +19,8 @@ public class Player : MonoBehaviour
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
 | 
					        levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        string musicPath = Path.Combine("Musics", Path.GetFileNameWithoutExtension(levelsLoader.level.MusicName));
 | 
					        audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.level.musicName));
 | 
				
			||||||
        AudioClip clip = Resources.Load<AudioClip>(musicPath);
 | 
					        audioSource.Play();
 | 
				
			||||||
        if (clip == null)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            Debug.LogError("Music file not found at: " + musicPath);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            audioSource.clip = clip;
 | 
					 | 
				
			||||||
            audioSource.Play();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var mainModule = particle.main;
 | 
					        var mainModule = particle.main;
 | 
				
			||||||
        mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
 | 
					        mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ class GameManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Level {
 | 
					class Level {
 | 
				
			||||||
  - name: String
 | 
					  - name: String
 | 
				
			||||||
  - musicPath: String
 | 
					  - musicName: String
 | 
				
			||||||
  + StartLevel()
 | 
					  + StartLevel()
 | 
				
			||||||
  + EndLevel()
 | 
					  + EndLevel()
 | 
				
			||||||
  + CheckCompletion(): Boolean
 | 
					  + CheckCompletion(): Boolean
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user