fix: levels loader in production build

This commit is contained in:
Théo LUDWIG 2025-04-28 14:38:16 +02:00
parent cc27037ea7
commit 3108c18744
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
2 changed files with 30 additions and 22 deletions

View File

@ -18,46 +18,39 @@ 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);
levelStat.JsonName = jsonTextFileStats.name;
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;
level.ProgressionPercent = 0;
level.ProgressionPercentMax = 0;
if (levelStatsMap.TryGetValue(level.JsonName, out LevelStat levelStat)) string statPath = Path.Combine(Application.persistentDataPath, level.JsonName + ".json");
if (File.Exists(statPath))
{ {
string statJson = File.ReadAllText(statPath);
LevelStat levelStat = JsonUtility.FromJson<LevelStat>(statJson);
level.TotalAttempts = levelStat.totalAttempts; level.TotalAttempts = levelStat.totalAttempts;
level.TotalJumps = levelStat.totalJumps; level.TotalJumps = levelStat.totalJumps;
level.ProgressionPercentMax = levelStat.progressionPercent; level.ProgressionPercentMax = levelStat.progressionPercent;
} }
else else
{ {
levelStat = new LevelStat { JsonName = level.JsonName, totalJumps = 0, totalAttempts = 0 }; level.TotalAttempts = 0;
levelStatsMap[level.JsonName] = levelStat; level.TotalJumps = 0;
level.ProgressionPercentMax = 0;
} }
level.ProgressionPercent = 0;
levels.Add(level); levels.Add(level);
} }
levels.Sort((x, y) => x.order.CompareTo(y.order)); levels.Sort((x, y) => x.order.CompareTo(y.order));
} }
private void SaveLevelCurrent() private void SaveLevelCurrent()
{ {
string levelJson = JsonUtility.ToJson(levelCurrent, true) + "\n"; if (levelCurrent == null) return;
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "Levels", levelCurrent.JsonName + ".json"), levelJson);
LevelStat levelStat = new() LevelStat levelStat = new()
{ {
@ -66,39 +59,54 @@ public class LevelsLoader : MonoBehaviour
totalAttempts = levelCurrent.TotalAttempts, totalAttempts = levelCurrent.TotalAttempts,
progressionPercent = levelCurrent.ProgressionPercentMax, progressionPercent = levelCurrent.ProgressionPercentMax,
}; };
string levelStatJson = JsonUtility.ToJson(levelStat, true) + "\n"; string levelStatJson = JsonUtility.ToJson(levelStat, true) + "\n";
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "LevelsStats", levelCurrent.JsonName + ".json"), levelStatJson);
string savePath = Path.Combine(Application.persistentDataPath, levelCurrent.JsonName + ".json");
File.WriteAllText(savePath, levelStatJson);
} }
public void NextLevel() public void NextLevel()
{ {
if (levels.Count == 0) return;
int currentIndex = levels.IndexOf(levelCurrent); int currentIndex = levels.IndexOf(levelCurrent);
levelCurrent = levels[(currentIndex + 1) % levels.Count]; levelCurrent = levels[(currentIndex + 1) % levels.Count];
} }
public void PreviousLevel() public void PreviousLevel()
{ {
if (levels.Count == 0) return;
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() public void IncreaseTotalJumps()
{ {
if (levelCurrent == null) return;
levelCurrent.TotalJumps += 1; levelCurrent.TotalJumps += 1;
SaveLevelCurrent(); SaveLevelCurrent();
} }
public void IncreaseTotalAttempts() public void IncreaseTotalAttempts()
{ {
if (levelCurrent == null) return;
levelCurrent.TotalAttempts += 1; levelCurrent.TotalAttempts += 1;
SaveLevelCurrent(); SaveLevelCurrent();
} }
public int CalculateCurrentProgressionPercent(Vector3 playerPosition) public int CalculateCurrentProgressionPercent(Vector3 playerPosition)
{ {
if (levelCurrent == null) return 0;
float lastX = levelCurrent.LastX; float lastX = levelCurrent.LastX;
GameObject winnerWallPrefab = Resources.Load<GameObject>("Prefabs/WinnerWall"); GameObject winnerWallPrefab = Resources.Load<GameObject>("Prefabs/WinnerWall");
float winnerWallWidth = winnerWallPrefab.GetComponent<Renderer>().bounds.size.x; float winnerWallWidth = winnerWallPrefab != null
? winnerWallPrefab.GetComponent<Renderer>().bounds.size.x
: 0f;
float marginError = 0.5f; float marginError = 0.5f;
float totalDistance = lastX - (winnerWallWidth / 2) - marginError; float totalDistance = lastX - (winnerWallWidth / 2) - marginError;

View File

@ -12,7 +12,7 @@ PlayerSettings:
targetDevice: 2 targetDevice: 2
useOnDemandResources: 0 useOnDemandResources: 0
accelerometerFrequency: 60 accelerometerFrequency: 60
companyName: DefaultCompany companyName: CNAM
productName: GeometryDash productName: GeometryDash
defaultCursor: {fileID: 0} defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0} cursorHotspot: {x: 0, y: 0}
@ -106,7 +106,7 @@ PlayerSettings:
xboxEnableFitness: 0 xboxEnableFitness: 0
visibleInBackground: 1 visibleInBackground: 1
allowFullscreenSwitch: 1 allowFullscreenSwitch: 1
fullscreenMode: 1 fullscreenMode: 3
xboxSpeechDB: 0 xboxSpeechDB: 0
xboxEnableHeadOrientation: 0 xboxEnableHeadOrientation: 0
xboxEnableGuest: 0 xboxEnableGuest: 0