Select Git revision

Sofiane Lasri authored
V7.cs 5.55 KiB
/*
République de Clément Geraudie
*/
using UnityEngine;
public class V7 : MonoBehaviour
{
[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 float scale = 2f;
public int[,] pointsDuChemin;
public int[] ptProches;
int ptsCheminX;
int ptsCheminY;
//public float offsetX = 100f;
//public float offsetY = 100f;
void Start()
{
// 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 y = 0; y < ptsCheminY; y++)
{
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]);
}
}
}
// 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 = heightmapWidth + 1;
terrainData.size = new Vector3(heightmapWidth, terrainDeph, heightmapHeight);
terrainData.SetHeights(0, 0, GenerateHeights());
return terrainData;
}
/*void ptLePlusProche()
{
int ptProche = -1;
double resultPtProche = 256;
double resultPtActuel = 256;
ptProches = new int[ptsCheminY];
for (int i = 0; i < ptsCheminY; i++)
{
for (int j = 0; j < ptsCheminY; j++)
{
if (pointsDuChemin[0, j] != pointsDuChemin[0, i] || pointsDuChemin[1, j] != pointsDuChemin[1, i])
{
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)
{
resultPtProche = resultPtActuel;
ptProche = j;
}
}
}
ptProches[i] = ptProche;
}
// Debug liste de ptProches
for (int i = 0; i < ptsCheminY; i++)
{
Debug.Log("ptProches[" + i + "] = " + ptProches[i]);
}
}*/
// Tracer chemin va se charger de dessinner le chemin au sol
private void tracerChemin()
{
// On va parcourir les pointsDuChemin et tracer le chemin entre ces points
}
float[,] GenerateHeights()
{
// On initialise le tableau
float[,] terrainHeightsList = new float[heightmapWidth, heightmapHeight];
// On remplit le tableau
for (int widthIndex = 0; widthIndex < heightmapWidth; widthIndex++)
{
for (int heightIndex = 0; heightIndex < heightmapHeight; heightIndex++)
{
// On calcule hauteur
terrainHeightsList[widthIndex, heightIndex] = CalculateHeight(widthIndex, heightIndex);
}
}
// Pour chaque point à générer, on va définir la hauteur à 0
for (int i = 0; i < nombreDePointsAGenerer; i++)
{
terrainHeightsList[pointsDuChemin[0, i], pointsDuChemin[1, i]] = 0; // terrainHeightsList[ ptPosX, ptPosY ] = 0;
Debug.Log("Point: "+pointsDuChemin[0, i] + ";" + pointsDuChemin[1, i]);
}
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 / heightmapWidth * scale;//+ offsetX;
float yCoord = (float)y / heightmapHeight * scale;//+ offsetY;
return Mathf.PerlinNoise(xCoord, yCoord);
}
}