Added new tile generation to the game

This commit is contained in:
TAASONI3
2023-12-06 17:02:34 +01:00
parent 3640f8a21f
commit 5fd78ed030
20 changed files with 941 additions and 982 deletions

View File

@@ -9,44 +9,7 @@ using System.Linq;
public class NoiseGenerator
{
System.Random rand = new System.Random();
public void applyNoise(GameObject tile, string name, Dictionary<Vector3, GameObject> map)
{
/*if(name.Length > 0){
applyCityNoise(tile);
}
else{
applyNormalNoise(tile, map);
}*/
applyNewNoise(tile, map);
}
private void applyCityNoise(GameObject tile)
{
//resetMesh(tile);
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples;
Color32[] colors;
int chance = rand.Next(1, 101);
samples = calculateSamplesCity(tile);
string tiletype = "City";
colors = new Color32[samples.Length];
for (int i = 0; i < samples.Length; i++)
{
colors[i] = new Color32(0, 185, 0, 255);
}
for (int i = 0; i < samples.Length; i++)
{
vertices[i].y = samples[i] * 3;
}
applyMesh(tile, vertices, mesh, colors);
tile.GetComponent<Tile>().setType(tiletype);
}
private void applyNewNoise(GameObject tile, Dictionary<Vector3, GameObject> map)
public void applyNoise(GameObject tile, Dictionary<Vector3, GameObject> map, Vector3 index)
{
//resetMesh(tile);
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
@@ -55,90 +18,44 @@ public class NoiseGenerator
Color32[] colors;
Color32 low;
Color32 high;
string tiletype = "";
samples = calculateBasicSamples(tile);
low = new Color32(0, 150, 0, 255);
high = new Color32(0, 110, 20, 255);
float lowestValue = 10;
float highestValue = 0;
for (int i = 0; i < samples.Length; i++)
{
if (lowestValue > samples[i])
{
lowestValue = samples[i];
}
if (highestValue < samples[i])
{
highestValue = samples[i];
}
}
float modifier = highestValue - lowestValue;
colors = new Color32[samples.Length];
for (int i = 0; i < samples.Length; i++)
{
colors[i] = Color32.Lerp(low, high, (1 / modifier) * (samples[i] - lowestValue));
}
for (int i = 0; i < samples.Length; i++)
{
vertices[i].y = samples[i] * 3;
}
applyMesh(tile, vertices, mesh, colors);
tile.GetComponent<Tile>().setType(tiletype);
}
private void applyNormalNoise(GameObject tile, Dictionary<Vector3, GameObject> map)
{
//resetMesh(tile);
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples;
Color32[] colors;
Color32 low;
Color32 high;
int chance = rand.Next(1, 101);
string tiletype = "";
if (chance > 85 && chance <= 100)
{
samples = calculateSamplesForest(tile);
low = new Color32(0, 150, 0, 255);
high = new Color32(0, 110, 20, 255);
tiletype = "Forest";
}
else if (chance > 70 && chance <= 85)
{
samples = calculateSamplesMountain(tile);
low = new Color32(0, 150, 0, 255);
high = new Color32(140, 140, 140, 255);
tiletype = "Mountain";
}
else if (chance > 55 && chance <= 70)
{
samples = calculateSamplesLake(tile);
low = new Color32(30, 110, 190, 255);
high = new Color32(0, 150, 0, 255);
tiletype = "Lake";
}
else if (chance > 40 && chance <= 55)
{
samples = calculateSamplesRiver(tile);
low = new Color32(30, 160, 190, 255);
high = new Color32(0, 150, 0, 255);
tiletype = "River";
List<TileType> availableTypes = checkAvailability(map, index);
TileType tiletype = TileType.NULL;
if (index == new Vector3(0, 0, 0))
{ //IS SPAWN
tiletype = (TileType)rand.Next(0, TileTypeMethods.getHighest() + 1);
}
else
{
samples = calculateSamplesPlane(tile);
low = new Color32(0, 150, 0, 255);
high = new Color32(0, 185, 0, 255);
tiletype = "Plane";
if (availableTypes.Count == 0)
{
Debug.Log("NO MATCH FOUND");
}
else
{
if (availableTypes.Contains(TileType.CITY))
{
if (rand.Next(0, 101) < 10)
{
tiletype = TileType.CITY;
SteamWorksHandler.getStandardAchievement("CityAchievement");
}
else
{
availableTypes.Remove(TileType.CITY);
tiletype = availableTypes[rand.Next(0, availableTypes.Count)];
}
}
else
{
tiletype = availableTypes[rand.Next(0, availableTypes.Count)];
}
}
}
tile.name = tiletype.ToString() + "_" + map.Count;
low = TileTypeMethods.getLowestColor(tiletype);
high = TileTypeMethods.getHighestColor(tiletype);
samples = TileTypeMethods.generateSamples(tiletype, vertices, rand);
float lowestValue = 10;
float highestValue = 0;
@@ -163,14 +80,156 @@ public class NoiseGenerator
colors[i] = Color32.Lerp(low, high, (1 / modifier) * (samples[i] - lowestValue));
}
//Connect samples to neighbours
samples = connectNeighbourSamples(samples, map, index);
colors = connectNeighbourColors(colors, map, index);
for (int i = 0; i < samples.Length; i++)
{
vertices[i].y = samples[i] * 3;
}
applyMesh(tile, vertices, mesh, colors);
tile.GetComponent<Tile>().setType(tiletype);
applyMesh(tile, mesh, vertices, colors);
}
private List<TileType> checkAvailability(Dictionary<Vector3, GameObject> tiles, Vector3 index)
{
List<List<TileType>> toCheck = new List<List<TileType>>();
List<TileType> result = new List<TileType>();
for (int i = 0; i < TileTypeMethods.getHighest() + 1; i++)
{
result.Add((TileType)i);
}
if (tiles.ContainsKey(index - new Vector3(1, 0, 0)))
{
toCheck.Add(tiles[index - new Vector3(1, 0, 0)].GetComponent<Tile>().getPossibleNeighbours());
}
if (tiles.ContainsKey(index - new Vector3(-1, 0, 0)))
{
toCheck.Add(tiles[index - new Vector3(-1, 0, 0)].GetComponent<Tile>().getPossibleNeighbours());
}
if (tiles.ContainsKey(index - new Vector3(0, 0, 1)))
{
toCheck.Add(tiles[index - new Vector3(0, 0, 1)].GetComponent<Tile>().getPossibleNeighbours());
}
if (tiles.ContainsKey(index - new Vector3(0, 0, -1)))
{
toCheck.Add(tiles[index - new Vector3(0, 0, -1)].GetComponent<Tile>().getPossibleNeighbours());
}
for (int i = 0; i < toCheck.Count; i++)
{
result = result.Intersect(toCheck[i]).ToList();
}
return result;
}
private float[] connectNeighbourSamples(float[] basis, Dictionary<Vector3, GameObject> tiles, Vector3 index)
{
float[] result = basis;
Mesh mesh;
Vector3[] vertices;
int sidelength = (int)Mathf.Sqrt(result.Length);
if (tiles.ContainsKey(index - new Vector3(1, 0, 0))) //Links
{
mesh = tiles[index - new Vector3(1, 0, 0)].GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
for (int i = 0; i < sidelength; i++)
{
result[sidelength - 1 + i * sidelength] = vertices[0 + i * sidelength].y / 3;
result[sidelength - 2 + i * sidelength] = (result[sidelength - 1 + i * sidelength] + result[sidelength - 3 + i * sidelength]) / 2;
}
}
if (tiles.ContainsKey(index - new Vector3(-1, 0, 0))) //Rechts
{
mesh = tiles[index - new Vector3(-1, 0, 0)].GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
for (int i = 0; i < sidelength; i++)
{
result[0 + i * sidelength] = vertices[sidelength - 1 + i * sidelength].y / 3;
result[1 + i * sidelength] = (result[0 + i * sidelength] + result[2 + i * sidelength]) / 2;
}
}
if (tiles.ContainsKey(index - new Vector3(0, 0, 1))) //Unten
{
mesh = tiles[index - new Vector3(0, 0, 1)].GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
for (int i = 0; i < sidelength; i++)
{
result[sidelength * sidelength - (sidelength - i)] = vertices[i].y / 3;
result[sidelength * (sidelength - 1) - (sidelength - i)] = (result[sidelength * sidelength - (sidelength - i)] + result[sidelength * (sidelength - 2) - (sidelength - i)]) / 2;
}
}
if (tiles.ContainsKey(index - new Vector3(0, 0, -1))) //Oben
{
mesh = tiles[index - new Vector3(0, 0, -1)].GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
for (int i = 0; i < sidelength; i++)
{
result[i] = vertices[sidelength * sidelength - (sidelength - i)].y / 3;
result[i + sidelength] = (result[i] + result[i + sidelength * 2]) / 2;
}
}
return result;
}
private Color32[] connectNeighbourColors(Color32[] basis, Dictionary<Vector3, GameObject> tiles, Vector3 index)
{
Color32[] result = basis;
Mesh mesh;
Color32[] colors;
int sidelength = (int)Mathf.Sqrt(result.Length);
if (tiles.ContainsKey(index - new Vector3(1, 0, 0))) //Links
{
mesh = tiles[index - new Vector3(1, 0, 0)].GetComponent<MeshFilter>().mesh;
colors = mesh.colors32;
for (int i = 0; i < sidelength; i++)
{
result[sidelength - 1 + i * sidelength] = colors[0 + i * sidelength];
result[sidelength - 2 + i * sidelength] = Color32.Lerp(result[sidelength - 1 + i * sidelength], result[sidelength - 3 + i * sidelength], 0.5f);
}
}
if (tiles.ContainsKey(index - new Vector3(-1, 0, 0))) //Rechts
{
mesh = tiles[index - new Vector3(-1, 0, 0)].GetComponent<MeshFilter>().mesh;
colors = mesh.colors32;
for (int i = 0; i < sidelength; i++)
{
result[0 + i * sidelength] = colors[sidelength - 1 + i * sidelength];
result[1 + i * sidelength] = Color32.Lerp(result[0 + i * sidelength], result[2 + i * sidelength], 0.5f);
}
}
if (tiles.ContainsKey(index - new Vector3(0, 0, 1))) //Unten
{
mesh = tiles[index - new Vector3(0, 0, 1)].GetComponent<MeshFilter>().mesh;
colors = mesh.colors32;
for (int i = 0; i < sidelength; i++)
{
result[sidelength * sidelength - (sidelength - i)] = colors[i];
result[sidelength * (sidelength - 1) - (sidelength - i)] = Color32.Lerp(result[sidelength * sidelength - (sidelength - i)], result[sidelength * (sidelength - 2) - (sidelength - i)], 0.5f);
}
}
if (tiles.ContainsKey(index - new Vector3(0, 0, -1))) //Oben
{
mesh = tiles[index - new Vector3(0, 0, -1)].GetComponent<MeshFilter>().mesh;
colors = mesh.colors32;
for (int i = 0; i < sidelength; i++)
{
result[i] = colors[sidelength * sidelength - (sidelength - i)];
result[i + sidelength] = Color32.Lerp(result[i], result[i + sidelength * 2], 0.5f);
}
}
return result;
}
private void resetMesh(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshCollider>().sharedMesh;
@@ -185,144 +244,15 @@ public class NoiseGenerator
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
}
private float[] calculateBasicSamples(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples = new float[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
float xCord = tile.GetComponent<Tile>().getPosition().x + vertices[i].x / (vertices.Length - 1) * 10;
float yCord = tile.GetComponent<Tile>().getPosition().z + vertices[i].z / (vertices.Length - 1) * 10;
float sample = Mathf.PerlinNoise(xCord, yCord) - 0.1f * rand.Next(0, 6);
samples[i] = sample;
}
return samples;
}
private float[] calculateSamplesCity(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples = new float[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
samples[i] = 0;
}
return samples;
}
private float[] calculateSamplesPlane(GameObject tile)
{
float[] samples = calculateBasicSamples(tile);
return samples;
}
private float[] calculateSamplesForest(GameObject tile)
{
float[] samples = calculateBasicSamples(tile);
return samples;
}
private float[] calculateSamplesMountain(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples = calculateBasicSamples(tile);
int amount = rand.Next(1, 5);
int index = 0;
for (int i = 0; i < amount; i++)
{
do
{
index = rand.Next(0, samples.Length);
if (vertices[index].x != 5 && vertices[index].z != 5 && vertices[index].x != -5 && vertices[index].z != -5)
{
samples[index] = 3;
break;
}
} while (true);
}
return samples;
}
private float[] calculateSamplesRiver(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples = calculateBasicSamples(tile);
bool isVertical = (rand.Next(0, 2) == 0 ? true : false);
int startX = 0;
int startZ = 0;
if (isVertical)
{
startZ = (rand.Next(0, 2) == 0 ? 4 : -4);
startX = rand.Next(-4, 5);
}
else
{
startZ = rand.Next(-4, 5);
startX = (rand.Next(0, 2) == 0 ? 4 : -4);
}
for (int k = 0; k < vertices.Length; k++)
{
if (isVertical)
{
if (Mathf.Round(vertices[k].x) == startX && Mathf.Round(vertices[k].z) != 5 && Mathf.Round(vertices[k].z) != -5)
{
samples[k] = samples[k] - rand.Next(2, 4) + 0.5f;
}
}
else
{
if (Mathf.Round(vertices[k].x) != 5 && Mathf.Round(vertices[k].x) != -5 && Mathf.Round(vertices[k].z) == startZ)
{
samples[k] = samples[k] - rand.Next(2, 4) + 0.5f;
}
}
}
return samples;
}
private float[] calculateSamplesLake(GameObject tile)
{
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float[] samples = calculateBasicSamples(tile);
int randX = rand.Next(-3, 4);
int randZ = rand.Next(-3, 4);
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
for (int k = 0; k < vertices.Length; k++)
{
if (Mathf.Round(vertices[k].x) == randX + i && Mathf.Round(vertices[k].z) == randZ + j)
{
samples[k] = samples[k] - rand.Next(1, 3) - 0.25f * rand.Next(0, 4);
break;
}
}
}
}
return samples;
}
private void applyMesh(GameObject tile, Vector3[] vertices, Mesh mesh, Color32[] colors)
public void applyMesh(GameObject tile, Mesh mesh, Vector3[] vertices, Color32[] colors)
{
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.colors32 = colors;
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
tile.GetComponent<MeshFilter>().sharedMesh = mesh;
}
public void saveTile(GameObject tile, string path)
@@ -374,6 +304,6 @@ public class NoiseGenerator
parts = current.Value<string>().Split('/');
vertices[i] = new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
}
applyMesh(tile, vertices, tile.GetComponent<MeshFilter>().mesh, colors);
applyMesh(tile, tile.GetComponent<MeshFilter>().mesh, vertices, colors);
}
}