public void GenerateTerrain(float randomNumber, List <List <GameObject> > gameObjects, List <List <Cell> > cells, int gridSize) { List <GameObject> Towers = new List <GameObject>(); CellObjects = gameObjects; Cells = cells; GridSize = gridSize; UnityEngine.Random.InitState((int)randomNumber); for (int q = 0; q < GridSize * 2 + 1; q++) { for (int r = 0; r < CellObjects[q].Count; r++) { cells[q][r].CellType = BiomeManager.CellType.Grass; cells[q][r].Humidity = 0; cells[q][r].Altitude = Mathf.Clamp(Mathf.PerlinNoise(q * perlinFrequancy + randomNumber, r * perlinFrequancy + randomNumber), 0.01f, 1); } } GenerateRivers(biomeManager.BiomeSeaLevel[(int)Biome]); for (int i = 0; i < TileTypeCount.Length; i++) { TileTypeCount[i] = 0; } for (int q = 0; q < GridSize * 2 + 1; q++) { for (int r = 0; r < CellObjects[q].Count; r++) { if (Cells[q][r].TowerObject) { Destroy(Cells[q][r].TowerObject); } if (Cells[q][r].CellType != BiomeManager.CellType.Water) { Cell ClosestStream = StreamCells[0]; for (int i = 0; i < StreamCells.Count; i++) { if (HexDistance(Cells[q][r].TilePostition, StreamCells[i].TilePostition) < HexDistance(Cells[q][r].TilePostition, ClosestStream.TilePostition)) { ClosestStream = StreamCells[i]; } } float DistanceShade = Mathf.Clamp(HexDistance(Cells[q][r].TilePostition, ClosestStream.TilePostition) / 10, 0, 0.9f); Cells[q][r].Humidity = DistanceShade; } if (Biome == CellBehaviour.BiomeType.Taiga) { cells[q][r] = biomeManager.Taiga(cells[q][r], (int)randomNumber); } else if (Biome == CellBehaviour.BiomeType.Ocean) { cells[q][r] = biomeManager.Ocean(cells[q][r], (int)randomNumber); } else if (Biome == CellBehaviour.BiomeType.Beach) { cells[q][r] = biomeManager.Beach(cells[q][r], (int)randomNumber); } //Mathf.Lerp(1f, 75f, altitude / 7.5f) if (cells[q][r].CellType != BiomeManager.CellType.Water) { CellObjects[q][r].transform.localScale = new Vector3(2f, Mathf.Lerp(0.1f, 10.0f, Cells[q][r].Altitude), 2f); } CellObjects[q][r].transform.position = new Vector3(CellObjects[q][r].transform.position.x, 0, CellObjects[q][r].transform.position.z); if (Cells[q][r].TowerIndex != 404) { // Ore if (Cells[q][r].TowerType == BiomeManager.TowerType.Ore) { Cells[q][r].Tower = System.Convert.ToString((BiomeManager.OreType)cells[q][r].TowerIndex); Cells[q][r].TowerObject = Instantiate(OreTowers[Cells[q][r].TowerIndex]); Cells[q][r].TowerObject.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f); Cells[q][r].TowerObject.transform.name = System.Convert.ToString((BiomeManager.OreType)cells[q][r].TowerIndex); HasOreType[Cells[q][r].TowerIndex] = true; TileTypeCount[Enum.GetNames(typeof(BiomeManager.CellType)).Length + (int)cells[q][r].TowerType] += 1; } // Tree else if (Cells[q][r].TowerType == BiomeManager.TowerType.Tree) { Cells[q][r].Tower = System.Convert.ToString((BiomeManager.TreeType)cells[q][r].TowerIndex); Cells[q][r].TowerObject = Instantiate(TreeTowers[Cells[q][r].TowerIndex]); Cells[q][r].TowerObject.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f); Cells[q][r].TowerObject.transform.name = System.Convert.ToString((BiomeManager.TreeType)cells[q][r].TowerIndex); HasTreeType[Cells[q][r].TowerIndex] = true; TileTypeCount[Enum.GetNames(typeof(BiomeManager.CellType)).Length + (int)cells[q][r].TowerType] += 1; } // Shrub else if (Cells[q][r].TowerType == BiomeManager.TowerType.Shrub) { Cells[q][r].Tower = System.Convert.ToString((BiomeManager.ShrubType)cells[q][r].TowerIndex); Cells[q][r].TowerObject = Instantiate(ShrubTowers[Cells[q][r].TowerIndex]); Cells[q][r].TowerObject.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f); Cells[q][r].TowerObject.transform.name = System.Convert.ToString((BiomeManager.ShrubType)cells[q][r].TowerIndex); HasShrubType[Cells[q][r].TowerIndex] = true; TileTypeCount[Enum.GetNames(typeof(BiomeManager.CellType)).Length + (int)cells[q][r].TowerType] += 1; } // Animals else if (Cells[q][r].TowerType == BiomeManager.TowerType.Animal) { Cells[q][r].Tower = System.Convert.ToString((BiomeManager.AnimalType)cells[q][r].TowerIndex); Cells[q][r].TowerObject = Instantiate(AnimalTowers[Cells[q][r].TowerIndex]); Cells[q][r].TowerObject.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f); Cells[q][r].TowerObject.transform.name = System.Convert.ToString((BiomeManager.AnimalType)cells[q][r].TowerIndex); HasAnimalType[Cells[q][r].TowerIndex] = true; TileTypeCount[Enum.GetNames(typeof(BiomeManager.CellType)).Length + (int)cells[q][r].TowerType] += 1; } else { Debug.Log("Unknown Tower Type"); } Cells[q][r].hasTower = true; Cells[q][r].TowerObject.transform.position = new Vector3(CellObjects[q][r].transform.position.x, (CellObjects[q][r].transform.localScale.y / 5), CellObjects[q][r].transform.position.z); Cells[q][r].TowerObject.transform.parent = TowerParent.transform; Towers.Add(Cells[q][r].TowerObject); } else { TileTypeCount[(int)cells[q][r].CellType] += 1; } } } for (int q = 0; q < GridSize * 2 + 1; q++) { for (int r = 0; r < CellObjects[q].Count; r++) { if (Cells[q][r].CellType != BiomeManager.CellType.Water) { GetComponent <UVScroller>().SetTexture(ObjectType.Cell, CellObjects[q][r], (float)Cells[q][r].CellType, (Cells[q][r].Humidity - 1) * -1); } } } for (int q = 0; q < GridSize * 2 + 1; q++) { for (int r = 0; r < CellObjects[q].Count; r++) { if (Cells[q][r].CellType == BiomeManager.CellType.Water) { #region Getting Neighbours List <float> WaterLevels = new List <float>(); float waterNeighbours = 0; float normalWaterLevel = 0; List <Cell> neighbour = new List <Cell>(); List <Cell> NotWaterNeighbour = new List <Cell>(); bool FoundNonWater = false; if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x + 1, Cells[q][r].TilePostition.y)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x + 1, Cells[q][r].TilePostition.y))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x, Cells[q][r].TilePostition.y + 1)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x, Cells[q][r].TilePostition.y + 1))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x - 1, Cells[q][r].TilePostition.y + 1)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x - 1, Cells[q][r].TilePostition.y + 1))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x - 1, Cells[q][r].TilePostition.y)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x - 1, Cells[q][r].TilePostition.y))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x, Cells[q][r].TilePostition.y - 1)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x, Cells[q][r].TilePostition.y - 1))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } if (GetCellByPos(new Vector2(Cells[q][r].TilePostition.x + 1, Cells[q][r].TilePostition.y - 1)) != null) { neighbour.Add(GetCellByPos(new Vector2(Cells[q][r].TilePostition.x + 1, Cells[q][r].TilePostition.y - 1))); if (neighbour[neighbour.Count - 1].CellType == BiomeManager.CellType.Water) { waterNeighbours++; } else { FoundNonWater = true; } } #endregion if (FoundNonWater) { Cell SmallestNonWaterTile = neighbour[0]; for (int i = 0; i < neighbour.Count; i++) { if (neighbour[i].CellType != BiomeManager.CellType.Water) { SmallestNonWaterTile = neighbour[i]; FoundNonWater = true; break; } } for (int i = 0; i < neighbour.Count; i++) { if (neighbour[i]) { if (neighbour[i].CellType != BiomeManager.CellType.Water) { if (neighbour[i].Altitude < SmallestNonWaterTile.Altitude) { SmallestNonWaterTile = neighbour[i]; } } } } Cells[q][r].transform.localScale = new Vector3(2, Mathf.Clamp(SmallestNonWaterTile.Altitude * 0.1f, 0.1f, 10f), 2); } for (int i = 0; i < neighbour.Count; i++) { if (neighbour[i]) { WaterLevels.Add(neighbour[i].transform.localScale.y); } } for (int v = 0; v < WaterLevels.Count; v++) { normalWaterLevel += WaterLevels[v]; } normalWaterLevel /= WaterLevels.Count; Cells[q][r].transform.localScale = new Vector3(2, Mathf.Clamp(normalWaterLevel * 0.95f, 0.1f, 10f), 2); float waterDepth = Mathf.Lerp(0, 1, waterNeighbours / 6f) / 4; GetComponent <UVScroller>().SetTexture(ObjectType.Cell, CellObjects[q][r], (float)Cells[q][r].CellType, waterDepth); } } } if (TowerMeshParent) { Destroy(TowerMeshParent); } if (Towers.Count > 0) { TowerMeshParent = new GameObject(); TowerMeshParent.AddComponent <MeshRenderer>(); TowerMeshParent.AddComponent <MeshFilter>(); MeshFilter[] meshFilters = new MeshFilter[Towers.Count]; for (int i = 0; i < Towers.Count; i++) { meshFilters[i] = Towers[i].GetComponent <MeshFilter>(); } CombineInstance[] combine = new CombineInstance[meshFilters.Length]; int m = 0; while (m < meshFilters.Length) { combine[m].mesh = meshFilters[m].sharedMesh; combine[m].transform = meshFilters[m].transform.localToWorldMatrix; meshFilters[m].gameObject.GetComponent <MeshRenderer>().enabled = false; m++; } TowerMeshParent.transform.GetComponent <MeshFilter>().mesh = new Mesh(); TowerMeshParent.transform.GetComponent <MeshFilter>().mesh.CombineMeshes(combine); TowerMeshParent.transform.GetComponent <Renderer>().material = meshFilters[0].gameObject.GetComponent <Renderer>().material; TowerMeshParent.transform.gameObject.SetActive(true); } }