feat: navigate between previous and next levels

This commit is contained in:
Théo LUDWIG 2025-02-09 20:02:13 +01:00
parent 50945f807d
commit a070c90e3a
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
9 changed files with 78 additions and 11 deletions

View File

@ -4,6 +4,7 @@
"totalJumps": 0,
"totalAttempts": 0,
"killedCount": 0,
"order": 2,
"elements": [
{
"type": "Spike",

View File

@ -0,0 +1,17 @@
{
"name": "Stereo Madness",
"musicName": "StereoMadness",
"totalJumps": 0,
"totalAttempts": 0,
"killedCount": 0,
"order": 1,
"elements": [
{
"type": "Spike",
"position": {
"x": 0,
"y": 0
}
}
]
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b865ad45000d21465bdcfbbca0225616
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,6 +8,7 @@ public class Level
public int totalJumps;
public int totalAttempts;
public int killedCount;
public int order;
public static Level CreateFromJSON(string jsonString)
{

View File

@ -1,15 +1,38 @@
using System.IO;
using System.Collections.Generic;
using UnityEngine;
public class LevelsLoader : MonoBehaviour
{
public Level level;
public List<Level> levels = new();
public Level levelCurrent;
public void Start()
private void Start()
{
DontDestroyOnLoad(gameObject);
LoadAllLevels();
levelCurrent = levels[0];
}
TextAsset jsonTextFile = Resources.Load<TextAsset>(Path.Combine("Levels", "BackOnTrack"));
level = JsonUtility.FromJson<Level>(jsonTextFile.text);
private void LoadAllLevels()
{
TextAsset[] levelFiles = Resources.LoadAll<TextAsset>("Levels");
foreach (TextAsset jsonTextFile in levelFiles)
{
Level loadedLevel = JsonUtility.FromJson<Level>(jsonTextFile.text);
levels.Add(loadedLevel);
}
levels.Sort((x, y) => x.order.CompareTo(y.order));
}
public void NextLevel()
{
int currentIndex = levels.IndexOf(levelCurrent);
levelCurrent = levels[(currentIndex + 1) % levels.Count];
}
public void PreviousLevel()
{
int currentIndex = levels.IndexOf(levelCurrent);
levelCurrent = levels[(currentIndex - 1 + levels.Count) % levels.Count];
}
}

View File

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

View File

@ -2,8 +2,15 @@ using UnityEngine;
public class LevelNextButton : MonoBehaviour
{
public LevelsLoader levelsLoader;
public void Start()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
}
public void NextLevel()
{
// TODO
levelsLoader.NextLevel();
}
}

View File

@ -2,8 +2,15 @@ using UnityEngine;
public class LevelPreviousButton : MonoBehaviour
{
public LevelsLoader levelsLoader;
public void Start()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
}
public void PreviousLevel()
{
// TODO
levelsLoader.PreviousLevel();
}
}

View File

@ -19,7 +19,7 @@ public class Player : MonoBehaviour
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.level.musicName));
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.levelCurrent.musicName));
audioSource.Play();
var mainModule = particle.main;