feat: level progression (#46)

This commit is contained in:
2025-03-17 18:28:27 +01:00
committed by GitHub
parent a9bd2a0048
commit 872772d3f0
18 changed files with 1717 additions and 35 deletions

View File

@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System;
public class LevelsLoader : MonoBehaviour
{
@ -33,11 +34,14 @@ public class LevelsLoader : MonoBehaviour
level.JsonName = jsonTextFile.name;
level.TotalAttempts = 0;
level.TotalJumps = 0;
level.ProgressionPercent = 0;
level.ProgressionPercentMax = 0;
if (levelStatsMap.TryGetValue(level.JsonName, out LevelStat levelStat))
{
level.TotalJumps = levelStat.totalJumps;
level.TotalAttempts = levelStat.totalAttempts;
level.TotalJumps = levelStat.totalJumps;
level.ProgressionPercentMax = levelStat.progressionPercent;
}
else
{
@ -59,7 +63,8 @@ public class LevelsLoader : MonoBehaviour
{
JsonName = levelCurrent.JsonName,
totalJumps = levelCurrent.TotalJumps,
totalAttempts = levelCurrent.TotalAttempts
totalAttempts = levelCurrent.TotalAttempts,
progressionPercent = levelCurrent.ProgressionPercentMax,
};
string levelStatJson = JsonUtility.ToJson(levelStat, true) + "\n";
File.WriteAllText(Path.Combine(Application.dataPath, "Resources", "LevelsStats", levelCurrent.JsonName + ".json"), levelStatJson);
@ -88,4 +93,22 @@ public class LevelsLoader : MonoBehaviour
levelCurrent.TotalAttempts += 1;
SaveLevelCurrent();
}
public int CalculateCurrentProgressionPercent(Vector3 playerPosition)
{
float lastX = levelCurrent.LastX;
GameObject winnerWallPrefab = Resources.Load<GameObject>("Prefabs/WinnerWall");
float winnerWallWidth = winnerWallPrefab.GetComponent<Renderer>().bounds.size.x;
float marginError = 0.5f;
float totalDistance = lastX - (winnerWallWidth / 2) - marginError;
float rawPercentage = (playerPosition.x / totalDistance) * 100;
int clampedPercentage = Mathf.Clamp(Mathf.RoundToInt(rawPercentage), 0, 100);
levelCurrent.ProgressionPercent = clampedPercentage;
levelCurrent.ProgressionPercentMax = Math.Max(levelCurrent.ProgressionPercentMax, levelCurrent.ProgressionPercent);
SaveLevelCurrent();
return clampedPercentage;
}
}