From 9560c9c7f5aa9e4246e2a1b9ec111a037fa8ae66 Mon Sep 17 00:00:00 2001
From: ACKERMANNGUE <gawen.ackermann@hesge.ch>
Date: Mon, 7 Apr 2025 11:40:33 +0200
Subject: [PATCH] BUGFIX : RoadGeneration error when End was colliding with
 another piece. Wasn't never replaced afterward

---
 impulse/Assets/Scripts/Road/RoadGenerator.cs | 23 +++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/impulse/Assets/Scripts/Road/RoadGenerator.cs b/impulse/Assets/Scripts/Road/RoadGenerator.cs
index 902be7d..fb6cbc3 100644
--- a/impulse/Assets/Scripts/Road/RoadGenerator.cs
+++ b/impulse/Assets/Scripts/Road/RoadGenerator.cs
@@ -35,9 +35,10 @@ public class RoadGenerator : MonoBehaviour
 
     void Start()
     {
-        //int seed = (int)System.DateTime.Now.Ticks;
-        //Debug.Log("Seed custom utilisé : " + seed);
-        Random.InitState(-1324498718);
+        int seed = (int)System.DateTime.Now.Ticks;
+        //seed = 1728374951;
+        Debug.Log("SEED : " + seed);
+        Random.InitState(seed);
         ApplyPlayerPrefs(this.roadPartBlueprintList);
 
         // Fix iterations
@@ -150,6 +151,21 @@ public class RoadGenerator : MonoBehaviour
                 placedRoads.Add(nextRoadPart);
             }
         }
+
+        // Ensures that if the very last road part (end) collided with any road part, to fix it and re-inject it.
+        GameObject lastPiece = gameObject.transform.childCount > 0 ? gameObject.transform.GetChild(gameObject.transform.childCount - 1).gameObject : null;
+        if (lastPiece == null || !lastPiece.name.StartsWith(roadPartEnd.gameObject.name))
+        {
+            Debug.LogWarning("Missing End road part. Forcing End placement.");
+            GameObject previousRoad = gameObject.transform.GetChild(gameObject.transform.childCount - 1).gameObject;
+            GameObject forcedEnd = SpawnRoadPart(roadPartEnd);
+            ConnectRoadParts(previousRoad, forcedEnd);
+            placedRoads.Add(forcedEnd);
+        }
+
+        // Allows to filter ALL destroyed road part.
+        placedRoads = placedRoads.Where(r => r != null).ToList();
+
         LogManager.Instance.RegisterParcourLength(CalculateTotalDistanceFromStartEnd());
     }
 
@@ -251,6 +267,7 @@ public class RoadGenerator : MonoBehaviour
             }
 
             badRoadPart = newRoadPart;
+            placedRoads.RemoveAll(r => r == null || r.name == badRoadPart.name);
 
             // If every road part bluepint are still colliding, take one step back and try again
             if (i == 1 && IsColliding(badRoadPart))
-- 
GitLab