feat: save level statistics

This commit is contained in:
Théo LUDWIG 2025-02-09 20:09:36 +01:00
parent a070c90e3a
commit 0dbc959c19
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
5 changed files with 22 additions and 4 deletions

View File

@ -3,7 +3,6 @@
"musicName": "BackOnTrack", "musicName": "BackOnTrack",
"totalJumps": 0, "totalJumps": 0,
"totalAttempts": 0, "totalAttempts": 0,
"killedCount": 0,
"order": 2, "order": 2,
"elements": [ "elements": [
{ {

View File

@ -3,7 +3,6 @@
"musicName": "StereoMadness", "musicName": "StereoMadness",
"totalJumps": 0, "totalJumps": 0,
"totalAttempts": 0, "totalAttempts": 0,
"killedCount": 0,
"order": 1, "order": 1,
"elements": [ "elements": [
{ {

View File

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

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System.Collections.Generic;
using System.IO;
public class LevelsLoader : MonoBehaviour public class LevelsLoader : MonoBehaviour
{ {
@ -24,6 +25,12 @@ public class LevelsLoader : MonoBehaviour
levels.Sort((x, y) => x.order.CompareTo(y.order)); levels.Sort((x, y) => x.order.CompareTo(y.order));
} }
private void SaveLevelCurrent()
{
string json = JsonUtility.ToJson(levelCurrent, true);
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "Levels", levelCurrent.name + ".json"), json);
}
public void NextLevel() public void NextLevel()
{ {
int currentIndex = levels.IndexOf(levelCurrent); int currentIndex = levels.IndexOf(levelCurrent);
@ -35,4 +42,16 @@ public class LevelsLoader : MonoBehaviour
int currentIndex = levels.IndexOf(levelCurrent); int currentIndex = levels.IndexOf(levelCurrent);
levelCurrent = levels[(currentIndex - 1 + levels.Count) % levels.Count]; levelCurrent = levels[(currentIndex - 1 + levels.Count) % levels.Count];
} }
public void IncreaseTotalJumps()
{
levelCurrent.totalJumps += 1;
SaveLevelCurrent();
}
public void IncreaseTotalAttempts()
{
levelCurrent.totalAttempts += 1;
SaveLevelCurrent();
}
} }

View File

@ -18,6 +18,7 @@ public class Player : MonoBehaviour
public void Start() public void Start()
{ {
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>(); levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
levelsLoader.IncreaseTotalAttempts();
audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.levelCurrent.musicName)); audioSource.clip = Resources.Load<AudioClip>(Path.Combine("Musics", levelsLoader.levelCurrent.musicName));
audioSource.Play(); audioSource.Play();
@ -62,6 +63,7 @@ public class Player : MonoBehaviour
{ {
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0); rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0);
rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse); rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse);
levelsLoader.IncreaseTotalJumps();
} }
private bool IsJumping() private bool IsJumping()