Feature/add stars renderer (#70)

Co-authored-by: Djelal BOUDJI <djelal@gmail.com>
This commit is contained in:
djelalb
2025-05-17 02:07:26 +02:00
committed by GitHub
parent d03e8f7107
commit 693bcfccec
20 changed files with 2304 additions and 166 deletions

View File

@ -13,6 +13,7 @@ using UnityEditor;
public class JSONExporter : MonoBehaviour
{
public TMP_Text statusText;
public int difficultyToExport = 1;
private LevelEditor editor;
private string levelsFolder;
private string assetFolderPath;
@ -98,6 +99,7 @@ public class JSONExporter : MonoBehaviour
name = fileName,
musicName = "",
order = 0,
difficulty = difficultyToExport,
elements = elements.ToArray()
};
string json = JsonUtility.ToJson(data, prettyPrint: true);
@ -105,7 +107,7 @@ public class JSONExporter : MonoBehaviour
try
{
File.WriteAllText(destPath, json);
SetStatus("Export successful: " + fileName + ".json", Color.green);
SetStatus($"Export successful: {fileName}.json (diff {difficultyToExport})", Color.green);
}
catch (System.Exception e)
{
@ -146,6 +148,7 @@ public class JSONExporter : MonoBehaviour
public string name;
public string musicName;
public int order;
public int difficulty;
public SerializableElement[] elements;
}
}

View File

@ -26,6 +26,7 @@ public class Level
public string name;
public string musicName;
public int order;
public int difficulty;
public List<LevelElement> elements;

View File

@ -0,0 +1,76 @@
using UnityEngine;
using UnityEngine.UI;
public class SelectDifficulty : MonoBehaviour
{
public GameObject selectDifficultyPanel;
public GameObject mainSaveButton;
public StarsRenderer starsRenderer;
public JSONExporter jsonExporter;
private int currentDifficulty;
private const int MinDiff = 1;
private const int MaxDiff = 5;
void Awake()
{
if (selectDifficultyPanel == null)
{
selectDifficultyPanel = GameObject.Find("SelectDifficultyPanel");
}
if (mainSaveButton == null)
{
mainSaveButton = GameObject.Find("MainSaveButton");
}
}
void Start()
{
currentDifficulty = starsRenderer != null
? starsRenderer.GetCurrentDifficulty()
: MinDiff;
currentDifficulty = Mathf.Clamp(currentDifficulty, MinDiff, MaxDiff);
UpdateUI();
}
private void UpdateUI()
{
starsRenderer.UseAutomaticMode();
starsRenderer?.SetManualDifficulty(currentDifficulty);
}
public void OpenSelectDifficulty()
{
selectDifficultyPanel.SetActive(true);
mainSaveButton.SetActive(false);
UpdateUI();
}
public void PreviousDifficulty()
{
Debug.Log("PreviousDifficulty called");
if (currentDifficulty > MinDiff)
{
currentDifficulty--;
UpdateUI();
}
}
public void NextDifficulty()
{
Debug.Log("NextDifficulty called");
if (currentDifficulty < MaxDiff)
{
currentDifficulty++;
UpdateUI();
}
}
public void Cancel()
{
selectDifficultyPanel.SetActive(false);
mainSaveButton.SetActive(true);
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c9aabd0c1803d3d46b76d6bbfcc0f471

View File

@ -0,0 +1,98 @@
using UnityEngine;
using UnityEngine.UI;
public class StarsRenderer : MonoBehaviour
{
public Image starTemplate;
public RectTransform starsContainer;
public LevelsLoader levelsLoader;
public float extraPadding = 10f;
private bool useManualMode = false;
private int manualDifficulty = 1;
private float starSpacing;
private int lastRenderedDifficulty = -1;
void Start()
{
if (starTemplate == null || starsContainer == null)
{
Debug.LogError("Star template ou starsContainer non assigné !", this);
enabled = false;
return;
}
starTemplate.gameObject.SetActive(false);
if (levelsLoader == null)
{
var loaderObj = GameObject.FindGameObjectWithTag("LevelsLoader");
if (loaderObj != null)
levelsLoader = loaderObj.GetComponent<LevelsLoader>();
}
starSpacing = starTemplate.rectTransform.sizeDelta.x + extraPadding;
lastRenderedDifficulty = -1;
}
void Update()
{
int target = GetCurrentDifficulty();
if (target != lastRenderedDifficulty)
RenderStarsInternal(target);
}
public void SetManualDifficulty(int difficulty)
{
useManualMode = true;
manualDifficulty = Mathf.Clamp(difficulty, 1, 5);
Debug.Log($"[StarsRenderer] SetManualDifficulty → manualDifficulty = {manualDifficulty}");
lastRenderedDifficulty = -1;
RenderStarsInternal(manualDifficulty);
}
public void UseAutomaticMode()
{
useManualMode = false;
lastRenderedDifficulty = -1;
}
public int GetCurrentDifficulty()
{
if (useManualMode)
return manualDifficulty;
if (levelsLoader != null && levelsLoader.levelCurrent != null)
return Mathf.Clamp(levelsLoader.levelCurrent.difficulty, 1, 5);
return 1;
}
private void RenderStarsInternal(int difficulty)
{
Debug.Log($"[StarsRenderer] RenderStarsInternal → difficulté = {difficulty}", this);
for (int i = starsContainer.childCount - 1; i >= 0; i--)
{
var child = starsContainer.GetChild(i);
if (child.gameObject != starTemplate.gameObject)
Destroy(child.gameObject);
}
lastRenderedDifficulty = difficulty;
float totalWidth = difficulty * starSpacing - extraPadding;
float startX = -totalWidth / 2 + starTemplate.rectTransform.sizeDelta.x / 2;
for (int i = 0; i < difficulty; i++)
{
var star = Instantiate(starTemplate, starsContainer);
star.gameObject.SetActive(true);
star.rectTransform.anchoredPosition = new Vector2(startX + i * starSpacing, 0f);
star.rectTransform.SetAsLastSibling();
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 443f905013127a44f9c3dcf5a8fed74b