mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-05-18 12:02:58 +02:00
97 lines
2.6 KiB
C#
97 lines
2.6 KiB
C#
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);
|
|
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)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|