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

@ -51,6 +51,10 @@ public class LevelEditor : MonoBehaviour
HandleBlockDeletion();
}
void OnDestroy()
{
ClearEditor();
}
#region UI
void LoadPrefabs()
@ -176,7 +180,6 @@ public class LevelEditor : MonoBehaviour
foreach (var h in hits)
{
if (h == col) continue;
if (h.CompareTag("Ground")) continue;
if (h.transform.IsChildOf(currentBlock.transform)) continue;
return false;
}
@ -198,7 +201,7 @@ public class LevelEditor : MonoBehaviour
currentBlock = sel;
isPlacingBlock = true;
currentScale = currentBlock.transform.localScale;
Debug.Log($"🟢 Sélection : {sel.name}");
Debug.Log($"Sélection : {sel.name}");
}
}
void PlaceBlock()
@ -211,7 +214,7 @@ public class LevelEditor : MonoBehaviour
// 1) Bloquer si on perçoit un spike de même type dans la direction de snap
if (IsBlockedBySameTypeInSnapDirection())
{
Debug.LogError("Impossible de poser un spike sur un autre spike !");
Debug.LogError("Impossible de poser un spike sur un autre spike !");
Destroy(currentBlock);
}
else
@ -219,7 +222,7 @@ public class LevelEditor : MonoBehaviour
// 2) On snap dans la direction (down/left/up/right), et on détruit si aucun support
if (!SnapSpikeByRotation())
{
Debug.LogError("Impossible de poser un spike dans le vide !");
Debug.LogError("Impossible de poser un spike dans le vide !");
Destroy(currentBlock);
}
else
@ -370,7 +373,7 @@ public class LevelEditor : MonoBehaviour
else if (dir == Vector2.right) p.x = bestHit.point.x - hw;
currentBlock.transform.position = new Vector3(p.x, p.y, -1f);
Debug.Log($"📌 Spike snapé {dir} sur « {bestHit.collider.name} » à {currentBlock.transform.position}");
Debug.Log($"Spike snapé {dir} sur « {bestHit.collider.name} » à {currentBlock.transform.position}");
return true;
}
@ -403,7 +406,7 @@ public class LevelEditor : MonoBehaviour
? ResizeAxis.Horizontal
: ResizeAxis.Vertical;
isResizing = true;
Debug.Log($"🧰 Début redim {tgt.name} (axe {currentResizeAxis})");
Debug.Log($"Début redim {tgt.name} (axe {currentResizeAxis})");
}
void PerformResizing()
@ -419,14 +422,14 @@ public class LevelEditor : MonoBehaviour
if (IsOverlapping(resizingTarget))
{
resizingTarget.transform.localScale = originalScale;
Debug.Log("Redim annulé : collision");
Debug.Log("Redim annulé : collision");
}
if (Input.GetMouseButtonUp(0))
{
isResizing = false;
resizingTarget = null;
currentResizeAxis = ResizeAxis.None;
Debug.Log("Fin redim");
Debug.Log("Fin redim");
}
}
@ -452,7 +455,7 @@ public class LevelEditor : MonoBehaviour
toD = toD.transform.parent.gameObject;
if (toD == currentBlock) { currentBlock = null; isPlacingBlock = false; }
Destroy(toD);
Debug.Log($"🗑️ Supprimé {toD.name}");
Debug.Log($"Supprimé {toD.name}");
}
}
@ -470,45 +473,88 @@ public class LevelEditor : MonoBehaviour
var col = currentBlock.GetComponent<Collider2D>();
var b = col.bounds;
float snapDistance = 1f;
float verticalEps = 0.05f; // petite marge pour exclure trop hauts ou trop bas
float verticalEps = 0.1f;
// Taille et positions des deux zones de recherche latérales
Vector2 boxSize = new Vector2(snapDistance, b.size.y - verticalEps * 2f);
// à droite
// === SNAP HORIZONTAL (droite)
Vector2 hBoxSize = new Vector2(snapDistance, b.size.y - verticalEps * 2f);
Vector2 rightCenter = new Vector2(b.max.x + snapDistance / 2f, b.center.y);
// à gauche
Vector2 leftCenter = new Vector2(b.min.x - snapDistance / 2f, b.center.y);
// Cherche à droite
var hits = Physics2D.OverlapBoxAll(rightCenter, boxSize, 0f);
var hits = Physics2D.OverlapBoxAll(rightCenter, hBoxSize, 0f);
foreach (var h in hits)
{
if (h != null && h.gameObject != currentBlock && !h.isTrigger)
{
float newX = h.bounds.min.x - b.extents.x;
currentBlock.transform.position = new Vector3(newX, currentBlock.transform.position.y, -1f);
Debug.Log($"↔️ Snap horizontal à droite contre {h.name}");
return;
}
if (IsInvalidSnapTarget(h)) continue;
float newX = h.bounds.min.x - b.extents.x;
currentBlock.transform.position = new Vector3(newX, currentBlock.transform.position.y, -1f);
Debug.Log($"Snap horizontal à droite contre {h.name}");
return;
}
// Cherche à gauche
hits = Physics2D.OverlapBoxAll(leftCenter, boxSize, 0f);
// === SNAP HORIZONTAL (gauche)
Vector2 leftCenter = new Vector2(b.min.x - snapDistance / 2f, b.center.y);
hits = Physics2D.OverlapBoxAll(leftCenter, hBoxSize, 0f);
foreach (var h in hits)
{
if (h != null && h.gameObject != currentBlock && !h.isTrigger)
{
float newX = h.bounds.max.x + b.extents.x;
currentBlock.transform.position = new Vector3(newX, currentBlock.transform.position.y, -1f);
Debug.Log($"↔️ Snap horizontal à gauche contre {h.name}");
return;
}
if (IsInvalidSnapTarget(h)) continue;
float newX = h.bounds.max.x + b.extents.x;
currentBlock.transform.position = new Vector3(newX, currentBlock.transform.position.y, -1f);
Debug.Log($"Snap horizontal à gauche contre {h.name}");
return;
}
// === SNAP VERTICAL (dessous)
Vector2 downBoxSize = new Vector2(b.size.x - 0.1f, snapDistance);
Vector2 downCenter = new Vector2(b.center.x, b.min.y - snapDistance / 2f);
hits = Physics2D.OverlapBoxAll(downCenter, downBoxSize, 0f);
foreach (var h in hits)
{
if (IsInvalidSnapTarget(h)) continue;
float newY = h.bounds.max.y + b.extents.y;
currentBlock.transform.position = new Vector3(currentBlock.transform.position.x, newY, -1f);
Debug.Log($"Snap vertical (bas) contre {h.name}");
return;
}
// === SNAP VERTICAL (au-dessus)
Vector2 upCenter = new Vector2(b.center.x, b.max.y + snapDistance / 2f);
hits = Physics2D.OverlapBoxAll(upCenter, downBoxSize, 0f);
foreach (var h in hits)
{
if (IsInvalidSnapTarget(h)) continue;
float newY = h.bounds.min.y - b.extents.y;
currentBlock.transform.position = new Vector3(currentBlock.transform.position.x, newY, -1f);
Debug.Log($"Snap vertical (haut) contre {h.name}");
return;
}
}
bool IsInvalidSnapTarget(Collider2D h)
{
if (h == null || h.gameObject == currentBlock || h.isTrigger) return true;
var t = h.transform;
if (t.parent != null && t.parent.name.Contains("ObstacleBlock"))
{
if (t.name.Contains("ObstacleKiller") || t.name.Contains("ObstacleSafer"))
return true;
}
return false;
}
// Filtrage des enfants parasites
bool IsChildOfObstacleBlock(Collider2D col)
{
var t = col.transform;
if (t.parent == null) return false;
bool isNamedObstacleChild = t.name.Contains("ObstacleSafer") || t.name.Contains("ObstacleKiller");
bool parentIsBlock = t.parent.name.Contains("ObstacleBlock");
return isNamedObstacleChild && parentIsBlock;
}
void HandleBlockRotation()
{
currentBlock.transform.Rotate(0, 0, -90f);
Debug.Log("🔄 Rotation 90°!");
Debug.Log("Rotation 90°!");
}
void InstantiateAndPrepare(GameObject prefab, Vector3? scaleOverride = null)
@ -523,8 +569,25 @@ public class LevelEditor : MonoBehaviour
public void Save()
{
// TODO
}
#endregion
public void ClearEditor()
{
if (persistentBlockContainer == null) return;
foreach (Transform child in persistentBlockContainer)
{
Destroy(child.gameObject);
}
Debug.Log("Éditeur vidé.");
currentBlock = null;
isPlacingBlock = false;
currentPage = 0;
ClearCurrentButtons();
GenerateButtons();
}
}