From f030903b92b8b92ae8c9c0e3a721db934eca955e Mon Sep 17 00:00:00 2001
From: SofianeLasri <alasri250@gmail.com>
Date: Tue, 29 Mar 2022 13:49:00 +0200
Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20du=20script=20de=20g=C3=A9n?=
 =?UTF-8?q?=C3=A9ration=20de=20terrain?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../CourseAPieds/sceneCourseAPied.unity       |  11 +-
 Assets/Scripts/CourseAPieds/V7.cs             | 159 +++++++++++-------
 Assets/Terrains/terrainCourseAPied.asset      | Bin 150564 -> 150564 bytes
 3 files changed, 101 insertions(+), 69 deletions(-)

diff --git a/Assets/Scenes/CourseAPieds/sceneCourseAPied.unity b/Assets/Scenes/CourseAPieds/sceneCourseAPied.unity
index 3301a7e9..656723f0 100644
--- a/Assets/Scenes/CourseAPieds/sceneCourseAPied.unity
+++ b/Assets/Scenes/CourseAPieds/sceneCourseAPied.unity
@@ -38,7 +38,7 @@ RenderSettings:
   m_ReflectionIntensity: 1
   m_CustomReflection: {fileID: 0}
   m_Sun: {fileID: 705507994}
-  m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481694, a: 1}
+  m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
   m_UseRadianceAmbientProbe: 0
 --- !u!157 &3
 LightmapSettings:
@@ -214,10 +214,13 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 86c7bfd105ba6e14180f833234cbc0b8, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  depth: 20
-  width: 256
-  height: 256
+  nombreDePointsAGenerer: 10
+  terrain: {fileID: 0}
+  terrainDeph: 20
+  heightmapWidth: 256
+  heightmapHeight: 256
   scale: 2
+  ptProches: 
 --- !u!1 &705507993
 GameObject:
   m_ObjectHideFlags: 0
diff --git a/Assets/Scripts/CourseAPieds/V7.cs b/Assets/Scripts/CourseAPieds/V7.cs
index 123ffd6a..dcab6fd2 100644
--- a/Assets/Scripts/CourseAPieds/V7.cs
+++ b/Assets/Scripts/CourseAPieds/V7.cs
@@ -1,17 +1,28 @@
+/* 
+République de Clément Geraudie
+*/
+
 using UnityEngine;
 
 public class V7 : MonoBehaviour
 {
-    public int depth = 20;
+    [Header("Initialisation")]
+    public int nombreDePointsAGenerer = 5;
+
+    [Header("Terrain")]
+    [SerializeField] private Terrain terrain;
+    public int terrainDeph = 20;
+
+    // Constantes pour la heightmap
+    [SerializeField] private int heightmapWidth = 256;
+    [SerializeField] private int heightmapHeight = 256;
 
-    public int width = 256;
-    public int height = 256;
 
     public float scale = 2f;
-    public int[,] values;
+    public int[,] pointsDuChemin;
     public int[] ptProches;
-    int dim1;
-    int dim2;
+    int ptsCheminX;
+    int ptsCheminY;
 
 
     //public float offsetX = 100f;
@@ -19,71 +30,80 @@ public class V7 : MonoBehaviour
 
     void Start()
     {
-        values = new int[2, 5];
-        dim1 = values.GetLength(0);
-        dim2 = values.GetLength(1);
-        for (int i = 0; i < dim1; i++)
+        // Création du tableau des points aléatoires (chemin)
+        pointsDuChemin = new int[2, nombreDePointsAGenerer];
+        // Récupère les dimensions du tableau de manière optimisée
+        ptsCheminX = pointsDuChemin.GetLength(0);
+        ptsCheminY = pointsDuChemin.GetLength(1);
+        
+        // On va générer les points de sorte à ce qu'il ne puissent pas se croiser
+        // On va découper le terrain en N partie, n étant le nombre de pts à générer
+        int lastBorder = 0;
+        int newBorder;
+        for (int x = 0; x < ptsCheminX; x++)
         {
-            for (int j = 0; j < dim2; j++)
+            for (int y = 0; y < ptsCheminY; y++)
             {
-                values[i, j] = Random.Range(0, width);
+                if(x==1){
+                    // Si i = 0, ça veut dire qu'on est sur l'axe Y du terrain
+                    newBorder = (heightmapWidth / nombreDePointsAGenerer) + lastBorder; // On calcule dynamiquement la taille de la zone de terrain où l'on va générer le pt
+                    pointsDuChemin[x, y] = Random.Range(lastBorder, newBorder); // On génère un pt aléatoire dans la zone définie
+                    // Debug.Log("Point Y=" + pointsDuChemin[x, y]);
+                    lastBorder = newBorder; // On met à jour la zone de terrain où l'on va générer le pt
+                }else{
+                    // Normalement, on est sur 0 (c'set un tableau de taille 2)
+                    pointsDuChemin[x, y] = Random.Range(0, heightmapWidth);
+                    // Debug.Log("Point X=" + pointsDuChemin[x, y]);
+                }
             }
         }
 
-    }
-
-    void Update()
-    {
-        Terrain terrain = GetComponent<Terrain>();
+        // On va récupérer le terrain et on va générer sa forme
+        terrain = GetComponent<Terrain>();
         terrain.terrainData = GenerateTerrain(terrain.terrainData);
+
+        // On va faire quelques vérifications
+        if(terrain == null)
+        {
+            Debug.LogError("Terrain non trouvé");
+            return;
+        }
+        if(terrain.terrainData == null)
+        {
+            Debug.LogError("TerrainData non trouvé");
+            return;
+        }
+        
+        // On va tracer le chemin
+        tracerChemin();
     }
     TerrainData GenerateTerrain(TerrainData terrainData)
     {
-        terrainData.heightmapResolution = width + 1;
+        terrainData.heightmapResolution = heightmapWidth + 1;
 
-        terrainData.size = new Vector3(width, depth, height);
+        terrainData.size = new Vector3(heightmapWidth, terrainDeph, heightmapHeight);
 
         terrainData.SetHeights(0, 0, GenerateHeights());
 
-        // offsetX += Time.deltaTime * 5f;
-
         return terrainData;
     }
 
-    bool PisteTest(int x, int y)
-    {
-        int ptLePlusProche;
-        for (int i = 0; i < dim2; i++)
-        {
-            if (x == values[0, i] && y == values[1, i])
-            {
-                int x1 = values[0, i];
-                int y1 = values[1, i];
-                int x2 = values[0, ptProches[i]];
-                int y2 = values[1, ptProches[i]];
-
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    void ptLePlusProche()
+    /*void ptLePlusProche()
     {
         int ptProche = -1;
         double resultPtProche = 256;
         double resultPtActuel = 256;
-        for (int i = 0; i < dim2; i++)
+        ptProches = new int[ptsCheminY];
+        for (int i = 0; i < ptsCheminY; i++)
         {
-            for (int j = 0; j < dim2; j++)
+            for (int j = 0; j < ptsCheminY; j++)
             {
-                if (values[0, j] != values[0, i] || values[1, j] != values[1, i])
+                if (pointsDuChemin[0, j] != pointsDuChemin[0, i] || pointsDuChemin[1, j] != pointsDuChemin[1, i])
                 {
-                    int x1 = values[0, i];
-                    int y1 = values[1, i];
-                    int x2 = values[0, j];
-                    int y2 = values[1, j];
+                    int x1 = pointsDuChemin[0, i];
+                    int y1 = pointsDuChemin[1, i];
+                    int x2 = pointsDuChemin[0, j];
+                    int y2 = pointsDuChemin[1, j];
                     resultPtActuel = Mathf.Sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2);
                     if (resultPtActuel < resultPtProche)
                     {
@@ -94,40 +114,49 @@ public class V7 : MonoBehaviour
             }
             ptProches[i] = ptProche;
         }
-    }
+        // Debug liste de ptProches
+        for (int i = 0; i < ptsCheminY; i++)
+        {
+            Debug.Log("ptProches[" + i + "] = " + ptProches[i]);
+        }
+    }*/
 
-    bool tracerChemin(int x1, int y1, int x2, int y2, int x, int y)
+    // Tracer chemin va se charger de dessinner le chemin au sol
+    private void tracerChemin()
     {
-        double m = (y2 - y1) / (x2 - x1);
-        double p = y - m * x;
-        bool equation = m == (p - y) / x;
-        return equation;
+        // On va parcourir les pointsDuChemin et tracer le chemin entre ces points
     }
 
     float[,] GenerateHeights()
     {
-        float[,] heights = new float[width, height];
-
-        for (int x = 0; x < width; x++)
+        // On initialise le tableau
+        float[,] terrainHeightsList = new float[heightmapWidth, heightmapHeight];
+        
+        // On remplit le tableau
+        for (int widthIndex = 0; widthIndex < heightmapWidth; widthIndex++)
         {
-            for (int y = 0; y < height; y++)
+            for (int heightIndex = 0; heightIndex < heightmapHeight; heightIndex++)
             {
-                heights[x, y] = CalculateHeight(x, y);
+                // On calcule hauteur
+                terrainHeightsList[widthIndex, heightIndex] = CalculateHeight(widthIndex, heightIndex);
             }
         }
-
-        for (int i = 0; i < dim2; i++)
+        
+        // Pour chaque point à générer, on va définir la hauteur à 0
+        for (int i = 0; i < nombreDePointsAGenerer; i++)
         {
-            heights[values[0, i], values[1, i]] = 0;
+            terrainHeightsList[pointsDuChemin[0, i], pointsDuChemin[1, i]] = 0; // terrainHeightsList[ ptPosX, ptPosY ] = 0;
+            Debug.Log("Point: "+pointsDuChemin[0, i] + ";" + pointsDuChemin[1, i]);
         }
-        return heights;
+        return terrainHeightsList;
     }
 
 
+    // Renvoie une valeur semi-aléaoite (perlin noise) entre un point x et y
     float CalculateHeight(int x, int y)
     {
-        float xCoord = (float)x / width * scale;//+ offsetX;
-        float yCoord = (float)y / height * scale;//+ offsetY;
+        float xCoord = (float)x / heightmapWidth * scale;//+ offsetX;
+        float yCoord = (float)y / heightmapHeight * scale;//+ offsetY;
 
         return Mathf.PerlinNoise(xCoord, yCoord);
     }
diff --git a/Assets/Terrains/terrainCourseAPied.asset b/Assets/Terrains/terrainCourseAPied.asset
index 178b1f87c5e15621eb3d78180da6f5eedaef4a83..3841391704b1421f5b1abc88baa4670b6d2fc4ca 100644
GIT binary patch
delta 830
zcmZ27gLBCY&J8`4ObiU0`zizYLG*);KNd{Qft&9gd}avZAN#Dq45B}L%hO<DVA!4@
z&p3q}#Q3JgSS82gXfVCVlW`Z*L7(k<UW|8CKr%e}jA@KaLh;*63K*9uf<$B{F`m-{
zG2R?tbP)nEj4m@yF=FcW-+qFXDZ;8=`}Qfj0H!IHJ1z6<9GC+v8<-R<e|*|&=fEUj
z`GCp7Qv2p9I|Zf=OTX-3yM;<Cz-ksSXILsQdsv1%yJ81Ye}KurviXvNUH;nJb_c%a
zgE7b~kQpGHz$9V0(>TxW0P_b61||ke5Z{6Mg2e$Q16fN3W(Tmq1Ys3>kf95hG%P`i
zJcNbpr|)rKQW5x<ZEx@Bdcn>t_pIGc^E|uh9~_tz{C{xnv(sMVW#7GQlU@G$+ja-l
z3+zPxU$HyAc$3`+CL600;zITe%nnu;<W%fWFW+RByzz!zZLGchM>`q&yuRCZTKQh~
zD?SU_+n@2b-+j|>db}gkRTc&Y28QVtPE0-!R)-Ul4urMCiRllEsR6J3^c~JjCJ?a?
zaF&J((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