fix: edit a level (#66)

Co-authored-by: Djelal BOUDJI <djelal@gmail.com>
Co-authored-by: Théo LUDWIG <contact@theoludwig.fr>
This commit is contained in:
M VINCENT PETT
2025-05-16 20:03:37 +02:00
committed by GitHub
parent 0b32ce7036
commit 728c57c66c
7 changed files with 209 additions and 66 deletions

View File

@ -13,7 +13,12 @@ public class LevelLoader : MonoBehaviour
private GameObject GetPrefab(string type)
{
return Resources.Load<GameObject>("Prefabs/" + type);
var prefab = Resources.Load<GameObject>("Prefabs/" + type);
if (prefab == null)
{
Debug.LogError($"Prefab introuvable pour : {type}");
}
return prefab;
}
private void LoadAudio()
@ -28,29 +33,95 @@ public class LevelLoader : MonoBehaviour
{
musicSource.volume = 1f;
}
musicSource.Play();
}
private void LoadElements()
{
Level current = levelsLoader.levelCurrent;
foreach (var element in current.elements)
{
GameObject prefab = GetPrefab(element.type);
GameObject instance = Instantiate(prefab, new Vector3(element.x, element.y, 0), Quaternion.identity);
if (prefab == null) continue;
if (prefab.CompareTag("Kill"))
GameObject instance = Instantiate(
prefab,
new Vector3(element.x, element.y, 0),
Quaternion.identity
);
if (editMode)
{
Instantiate(Resources.Load<GameObject>("AICollider"), new Vector3(element.x - 1, element.y, 0), Quaternion.identity);
foreach (Transform child in instance.transform)
{
if (child.name.Contains("ObstacleKiller"))
{
var col = child.GetComponent<BoxCollider2D>();
if (col != null && col.size.y > 2f) // Trop grand
{
Debug.LogWarning($"⚠️ Collider {child.name} trop grand, réduction appliquée.");
col.size = new Vector2(col.size.x, 1f);
col.offset = new Vector2(col.offset.x, -2f); // Ajuste selon ton design
}
}
}
}
// En mode jeu/test uniquement → ajout du AICollider
if (!editMode)
{
if (prefab.CompareTag("Kill"))
{
Instantiate(
Resources.Load<GameObject>("AICollider"),
new Vector3(element.x - 1, element.y, 0),
Quaternion.identity
);
}
}
// Appliquer l'échelle personnalisée
Vector3 originalScale = instance.transform.localScale;
float newScaleX = element.scaleX > 0 ? element.scaleX : originalScale.x;
float newScaleY = element.scaleY > 0 ? element.scaleY : originalScale.y;
instance.transform.localScale = new Vector3(newScaleX, newScaleY, originalScale.z);
}
// Sol uniquement en mode jeu
if (!editMode)
{
GameObject groundPrefab = GetPrefab("Ground");
if (groundPrefab != null)
{
GameObject groundInstance = Instantiate(
groundPrefab,
new Vector3(current.LastX / 2, groundY, 0),
Quaternion.identity
);
groundInstance.transform.localScale = new Vector3(current.LastX / 5f * 2, 1, 1);
}
}
// Mur de fin toujours placé
GameObject winWall = GetPrefab("WinnerWall");
if (winWall != null)
{
Instantiate(
winWall,
new Vector3(current.LastX, 0, 0),
Quaternion.Euler(0, 0, 90)
);
}
}
private void Awake()
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
Level current = levelsLoader.levelCurrent;
createMode = PlayerPrefs.GetInt("CreateMode", 0) == 1;
if (!editMode)
{
GameObject groundPrefab = GetPrefab("Ground");
@ -63,10 +134,12 @@ public class LevelLoader : MonoBehaviour
public void Start()
{
createMode = PlayerPrefs.GetInt("CreateMode", 0) == 1;
if (!createMode)
{
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
levelsLoader = GameObject
.FindGameObjectWithTag("LevelsLoader")
.GetComponent<LevelsLoader>();
levelsLoader.IncreaseTotalAttempts();
LoadElements();
@ -79,8 +152,7 @@ public class LevelLoader : MonoBehaviour
{
if (!editMode)
{
Level current = levelsLoader.levelCurrent;
progressionText.text = current.ProgressionPercent + "%";
progressionText.text = levelsLoader.levelCurrent.ProgressionPercent + "%";
}
}
}