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:
@ -16,9 +16,12 @@ public class LevelElement
|
||||
[System.Serializable]
|
||||
public class Level
|
||||
{
|
||||
public static readonly int LAST_X = 15;
|
||||
public string JsonName { get; set; }
|
||||
public int TotalJumps { get; set; }
|
||||
public int TotalAttempts { get; set; }
|
||||
public int ProgressionPercent { get; set; }
|
||||
public int ProgressionPercentMax { get; set; }
|
||||
|
||||
public string name;
|
||||
public string musicName;
|
||||
@ -26,6 +29,20 @@ public class Level
|
||||
|
||||
public List<LevelElement> elements;
|
||||
|
||||
public float LastX
|
||||
{
|
||||
get
|
||||
{
|
||||
LevelElement lastElement = elements[^1];
|
||||
float lastX = LAST_X;
|
||||
if (lastElement != null)
|
||||
{
|
||||
lastX += lastElement.x;
|
||||
}
|
||||
return lastX;
|
||||
}
|
||||
}
|
||||
|
||||
public static Level CreateFromJSON(string jsonString)
|
||||
{
|
||||
return JsonUtility.FromJson<Level>(jsonString);
|
||||
|
@ -1,10 +1,12 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.IO;
|
||||
|
||||
public class LevelLoader : MonoBehaviour
|
||||
{
|
||||
public LevelsLoader levelsLoader;
|
||||
public AudioSource audioSource;
|
||||
public Text progressionText;
|
||||
|
||||
private GameObject GetPrefab(string type)
|
||||
{
|
||||
@ -31,14 +33,7 @@ public class LevelLoader : MonoBehaviour
|
||||
|
||||
instance.transform.localScale = new Vector3(newScaleX, newScaleY, originalScale.z);
|
||||
}
|
||||
|
||||
LevelElement lastElement = current.elements[^1];
|
||||
float lastX = 15;
|
||||
if (lastElement != null)
|
||||
{
|
||||
lastX += lastElement.x;
|
||||
}
|
||||
Instantiate(GetPrefab("WinnerWall"), new Vector3(lastX, 0, 0), Quaternion.Euler(0, 0, 90));
|
||||
Instantiate(GetPrefab("WinnerWall"), new Vector3(current.LastX, 0, 0), Quaternion.Euler(0, 0, 90));
|
||||
}
|
||||
|
||||
public void Start()
|
||||
@ -52,6 +47,7 @@ public class LevelLoader : MonoBehaviour
|
||||
|
||||
public void Update()
|
||||
{
|
||||
|
||||
Level current = levelsLoader.levelCurrent;
|
||||
progressionText.text = current.ProgressionPercent + "%";
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ public class LevelStat
|
||||
|
||||
public int totalJumps;
|
||||
public int totalAttempts;
|
||||
public int progressionPercent;
|
||||
|
||||
public static LevelStat CreateFromJSON(string jsonString)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ public class LevelProgression : MonoBehaviour
|
||||
|
||||
private string GetText()
|
||||
{
|
||||
return "Progression: ";
|
||||
return "Progression Max: " + levelsLoader.levelCurrent.ProgressionPercentMax + "%";
|
||||
}
|
||||
|
||||
public void Start()
|
||||
|
33
Assets/Scripts/PauseMenu.cs
Normal file
33
Assets/Scripts/PauseMenu.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PauseMenu : MonoBehaviour
|
||||
{
|
||||
public GameObject pauseMenu;
|
||||
public GameObject pauseButton;
|
||||
public LevelLoader levelLoader;
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
Time.timeScale = 0;
|
||||
levelLoader.audioSource.Pause();
|
||||
|
||||
pauseMenu.SetActive(true);
|
||||
pauseButton.SetActive(false);
|
||||
}
|
||||
|
||||
public void Home()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
SceneManager.LoadScene("HomeScene");
|
||||
}
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
levelLoader.audioSource.Play();
|
||||
|
||||
pauseMenu.SetActive(false);
|
||||
pauseButton.SetActive(true);
|
||||
}
|
||||
}
|
2
Assets/Scripts/PauseMenu.cs.meta
Normal file
2
Assets/Scripts/PauseMenu.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe007f250f006dae5b98ae5c5a87113b
|
@ -42,6 +42,7 @@ public class Player : MonoBehaviour
|
||||
public void Update()
|
||||
{
|
||||
CurrentGameMode.Update(this);
|
||||
LevelsLoader.CalculateCurrentProgressionPercent(transform.position);
|
||||
}
|
||||
|
||||
public void OnCollisionEnter2D(Collision2D collision)
|
||||
|
Reference in New Issue
Block a user