mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-04-10 21:47:07 +02:00
feat: save the stats in a separated json (#38)
This commit is contained in:
parent
827c867052
commit
ce92ba53cc
3
.gitignore
vendored
3
.gitignore
vendored
@ -76,3 +76,6 @@ crashlytics-build.properties
|
|||||||
# Ignore temporaries from GameCI
|
# Ignore temporaries from GameCI
|
||||||
/[Aa]rtifacts/
|
/[Aa]rtifacts/
|
||||||
/[Cc]odeCoverage/
|
/[Cc]odeCoverage/
|
||||||
|
|
||||||
|
# Saves
|
||||||
|
/[Aa]ssets/[Rr]esources/LevelsStats
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Back on Track",
|
"name": "Back on Track",
|
||||||
"musicName": "BackOnTrack",
|
"musicName": "BackOnTrack",
|
||||||
"totalJumps": 0,
|
|
||||||
"totalAttempts": 0,
|
|
||||||
"order": 2,
|
"order": 2,
|
||||||
"elements": [
|
"elements": [
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Stereo Madness",
|
"name": "Stereo Madness",
|
||||||
"musicName": "StereoMadness",
|
"musicName": "StereoMadness",
|
||||||
"totalJumps": 0,
|
|
||||||
"totalAttempts": 0,
|
|
||||||
"order": 1,
|
"order": 1,
|
||||||
"elements": [
|
"elements": [
|
||||||
{
|
{
|
||||||
|
8
Assets/Resources/LevelsStats.meta
Normal file
8
Assets/Resources/LevelsStats.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 59df152a42477a5c785d771b7e2be292
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
0
Assets/Resources/LevelsStats/.gitkeep
Normal file
0
Assets/Resources/LevelsStats/.gitkeep
Normal file
@ -4,11 +4,11 @@ using UnityEngine;
|
|||||||
public class Level
|
public class Level
|
||||||
{
|
{
|
||||||
public string JsonName { get; set; }
|
public string JsonName { get; set; }
|
||||||
|
public int TotalJumps { get; set; }
|
||||||
|
public int TotalAttempts { get; set; }
|
||||||
|
|
||||||
public string name;
|
public string name;
|
||||||
public string musicName;
|
public string musicName;
|
||||||
public int totalJumps;
|
|
||||||
public int totalAttempts;
|
|
||||||
public int order;
|
public int order;
|
||||||
|
|
||||||
public static Level CreateFromJSON(string jsonString)
|
public static Level CreateFromJSON(string jsonString)
|
||||||
|
15
Assets/Scripts/LevelStat.cs
Normal file
15
Assets/Scripts/LevelStat.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
[System.Serializable]
|
||||||
|
public class LevelStat
|
||||||
|
{
|
||||||
|
public string JsonName { get; set; }
|
||||||
|
|
||||||
|
public int totalJumps;
|
||||||
|
public int totalAttempts;
|
||||||
|
|
||||||
|
public static LevelStat CreateFromJSON(string jsonString)
|
||||||
|
{
|
||||||
|
return JsonUtility.FromJson<LevelStat>(jsonString);
|
||||||
|
}
|
||||||
|
}
|
2
Assets/Scripts/LevelStat.cs.meta
Normal file
2
Assets/Scripts/LevelStat.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: be0a07689666b367aaf138fa766075c8
|
@ -17,10 +17,33 @@ public class LevelsLoader : MonoBehaviour
|
|||||||
private void LoadAllLevels()
|
private void LoadAllLevels()
|
||||||
{
|
{
|
||||||
TextAsset[] levelFiles = Resources.LoadAll<TextAsset>("Levels");
|
TextAsset[] levelFiles = Resources.LoadAll<TextAsset>("Levels");
|
||||||
|
TextAsset[] levelStatsFiles = Resources.LoadAll<TextAsset>("LevelsStats");
|
||||||
|
|
||||||
|
Dictionary<string, LevelStat> levelStatsMap = new();
|
||||||
|
foreach (TextAsset jsonTextFileStats in levelStatsFiles)
|
||||||
|
{
|
||||||
|
LevelStat levelStat = LevelStat.CreateFromJSON(jsonTextFileStats.text);
|
||||||
|
levelStatsMap[levelStat.JsonName] = levelStat;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (TextAsset jsonTextFile in levelFiles)
|
foreach (TextAsset jsonTextFile in levelFiles)
|
||||||
{
|
{
|
||||||
Level level = Level.CreateFromJSON(jsonTextFile.text);
|
Level level = Level.CreateFromJSON(jsonTextFile.text);
|
||||||
level.JsonName = jsonTextFile.name;
|
level.JsonName = jsonTextFile.name;
|
||||||
|
level.TotalAttempts = 0;
|
||||||
|
level.TotalJumps = 0;
|
||||||
|
|
||||||
|
if (levelStatsMap.TryGetValue(level.JsonName, out LevelStat levelStat))
|
||||||
|
{
|
||||||
|
level.TotalJumps = levelStat.totalJumps;
|
||||||
|
level.TotalAttempts = levelStat.totalAttempts;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
levelStat = new LevelStat { JsonName = level.JsonName, totalJumps = 0, totalAttempts = 0 };
|
||||||
|
levelStatsMap[level.JsonName] = levelStat;
|
||||||
|
}
|
||||||
|
|
||||||
levels.Add(level);
|
levels.Add(level);
|
||||||
}
|
}
|
||||||
levels.Sort((x, y) => x.order.CompareTo(y.order));
|
levels.Sort((x, y) => x.order.CompareTo(y.order));
|
||||||
@ -28,8 +51,17 @@ public class LevelsLoader : MonoBehaviour
|
|||||||
|
|
||||||
private void SaveLevelCurrent()
|
private void SaveLevelCurrent()
|
||||||
{
|
{
|
||||||
string json = JsonUtility.ToJson(levelCurrent, true) + "\n";
|
string levelJson = JsonUtility.ToJson(levelCurrent, true) + "\n";
|
||||||
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "Levels", levelCurrent.JsonName + ".json"), json);
|
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "Levels", levelCurrent.JsonName + ".json"), levelJson);
|
||||||
|
|
||||||
|
LevelStat levelStat = new()
|
||||||
|
{
|
||||||
|
JsonName = levelCurrent.JsonName,
|
||||||
|
totalJumps = levelCurrent.TotalJumps,
|
||||||
|
totalAttempts = levelCurrent.TotalAttempts
|
||||||
|
};
|
||||||
|
string levelStatJson = JsonUtility.ToJson(levelStat, true) + "\n";
|
||||||
|
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "LevelsStats", levelCurrent.JsonName + ".json"), levelStatJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void NextLevel()
|
public void NextLevel()
|
||||||
@ -46,13 +78,13 @@ public class LevelsLoader : MonoBehaviour
|
|||||||
|
|
||||||
public void IncreaseTotalJumps()
|
public void IncreaseTotalJumps()
|
||||||
{
|
{
|
||||||
levelCurrent.totalJumps += 1;
|
levelCurrent.TotalJumps += 1;
|
||||||
SaveLevelCurrent();
|
SaveLevelCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void IncreaseTotalAttempts()
|
public void IncreaseTotalAttempts()
|
||||||
{
|
{
|
||||||
levelCurrent.totalAttempts += 1;
|
levelCurrent.TotalAttempts += 1;
|
||||||
SaveLevelCurrent();
|
SaveLevelCurrent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user