mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-04-10 21:47:07 +02:00
92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
public class LevelsLoader : MonoBehaviour
|
|
{
|
|
public List<Level> levels = new();
|
|
public Level levelCurrent;
|
|
|
|
private void Start()
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
LoadAllLevels();
|
|
levelCurrent = levels[0];
|
|
}
|
|
|
|
private void LoadAllLevels()
|
|
{
|
|
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);
|
|
levelStat.JsonName = jsonTextFileStats.name;
|
|
levelStatsMap[levelStat.JsonName] = levelStat;
|
|
}
|
|
|
|
foreach (TextAsset jsonTextFile in levelFiles)
|
|
{
|
|
Level level = Level.CreateFromJSON(jsonTextFile.text);
|
|
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.Sort((x, y) => x.order.CompareTo(y.order));
|
|
}
|
|
|
|
private void SaveLevelCurrent()
|
|
{
|
|
string levelJson = JsonUtility.ToJson(levelCurrent, true) + "\n";
|
|
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()
|
|
{
|
|
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];
|
|
}
|
|
|
|
public void IncreaseTotalJumps()
|
|
{
|
|
levelCurrent.TotalJumps += 1;
|
|
SaveLevelCurrent();
|
|
}
|
|
|
|
public void IncreaseTotalAttempts()
|
|
{
|
|
levelCurrent.TotalAttempts += 1;
|
|
SaveLevelCurrent();
|
|
}
|
|
}
|