feat: load dynamically level music

This commit is contained in:
2025-02-09 19:37:37 +01:00
parent a5cdd3ada1
commit 707dee5063
70 changed files with 230 additions and 122 deletions

View File

@ -7,10 +7,14 @@ public class Level
public string Name { get; set; }
[DataMember]
public string MusicPath { get; set; }
public string MusicName { get; set; }
[DataMember]
public int TotalJumps { get; set; }
[DataMember]
public int TotalAttempts { get; set; }
[DataMember]
public int KilledCount { get; set; }
}

View File

@ -1,28 +1,18 @@
using System.IO;
using System.Runtime.Serialization.Json;
using UnityEngine;
using UnityEngine.UI;
public class LevelLoader : MonoBehaviour
public class LevelsLoader : MonoBehaviour
{
public Text levelNameText;
public Level level;
void Start()
public void Start()
{
LoadLevel();
if (level != null)
{
levelNameText.text = level.Name;
}
else
{
levelNameText.text = "Failed to Load Level";
}
}
DontDestroyOnLoad(gameObject);
// var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
void LoadLevel()
{
string path = Path.Combine(Application.dataPath, "Levels", "back-on-track.json");
if (File.Exists(path))
{

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 946cccb716439eccfa13f90e2fe2abe1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelHomeButton : MonoBehaviour
{
public void GoToHome()
{
SceneManager.LoadScene("HomeScene");
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ef2acea012085a3bb9a981fc334afaec

View File

@ -0,0 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelNameButton : MonoBehaviour
{
public void PlayLevel()
{
SceneManager.LoadScene("LevelScene");
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3617637b5b720115a8f1a856cb43f8a5

View File

@ -0,0 +1,15 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelNameText : MonoBehaviour
{
public LevelsLoader levelsLoader;
public Text levelNameText;
public void Start()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
levelNameText.text = levelsLoader.level.Name;
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b9d2aa88c034d40f187d6ff84705da65

View File

@ -0,0 +1,9 @@
using UnityEngine;
public class LevelNextButton : MonoBehaviour
{
public void NextLevel()
{
// TODO
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c34ef065650ba0802be580828cc0d474

View File

@ -0,0 +1,9 @@
using UnityEngine;
public class LevelPreviousButton : MonoBehaviour
{
public void PreviousLevel()
{
// TODO
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2c53cba3260b30d02b79e69a3198546c

View File

@ -1,13 +1,14 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.Audio;
public class Player : MonoBehaviour
{
public Rigidbody2D rigidBody;
public GameObject playerObject;
public ParticleSystem particle;
public LevelLoader levelLoader;
public LevelsLoader levelsLoader;
public bool isColliding = true;
public AudioSource audioSource;
@ -17,26 +18,24 @@ public class Player : MonoBehaviour
public void Start()
{
// levelLoader = GameObject.FindGameObjectWithTag("LevelLoader").GetComponent<LevelLoader>();
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();
}
var mainModule = particle.main;
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
particle.transform.parent = null;
// if (levelLoader.level != null)
// {
// string musicPath = Path.Combine("Musics", Path.GetFileNameWithoutExtension(levelLoader.level.MusicPath));
// AudioClip clip = Resources.Load<AudioClip>(musicPath);
// if (clip != null)
// {
// audioSource.clip = clip;
// audioSource.Play();
// }
// else
// {
// Debug.LogError("Failed to load music: " + musicPath);
// }
// }
Invoke(nameof(EnableInput), 0.1f);
}

View File

@ -1,33 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SelectLevelMenu : MonoBehaviour
{
public Text levelNameText;
public void PlayLevel()
{
SceneManager.LoadScene("LevelScene");
}
public void BackBtn()
{
SceneManager.LoadScene("HomeScene");
}
public void LastLevel()
{
// TODO
}
public void NextLevel()
{
// TODO
}
public void LevelStatsBtn()
{
// SceneManager.LoadSceneAsync(?);
}
}

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 78914afaa50342f4fbc6b2f0d1a6c036