mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-06-10 22:20:40 +02:00
feat: level progression (#46)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user