public Grid <string> CreateDungeon() { var valueGrid = PerlinNoiseGrid.CreatePerlinNoiseGrid(_width, _height, _scale); var grid = new Grid <string>(valueGrid.Width, valueGrid.Height); grid.ForEachNode((node, x, y) => { var valueNode = valueGrid.GetNode(x, y); var avalibleTileExists = false; for (int i = 0; i < _tiles.Length; i++) { if (valueNode > _tiles[i]._min && valueNode < _tiles[i]._max) { node = _tiles[i]._name; avalibleTileExists = true; break; } } if (!avalibleTileExists) { node = _default; } return(node); }); return(grid); }
void Start() { tilemap = GetComponent <Tilemap>(); wallTiles = new List <Vector3Int>(); groundTiles = new List <Vector3Int>(); PerlinNoiseGrid noise = new PerlinNoiseGrid(width, height, perlinScale); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Vector3Int pos = new Vector3Int(i, j, 0); if (noise[i, j] < wallThreshold) { tilemap.SetTile(pos, wall); wallTiles.Add(pos); } else { groundTiles.Add(pos); } } } StartCoroutine(NextPath()); }
IEnumerator NextMap() { float perlinScale = Random.Range(perlinScaleMin, perlinScaleMax); tilemap.ClearAllTiles(); PerlinNoiseGrid noise = new PerlinNoiseGrid(width, height, perlinScale); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Vector3Int pos = new Vector3Int(i, j, 0); Tile t = tiles[GetIndex(noise[i, j])]; tilemap.SetTile(pos, t); } } yield return(new WaitForSeconds(duration)); StartCoroutine(NextMap()); }
Tile[,] generateTileGrid(PerlinNoiseGrid noise) { Tile[,] tiles = new Tile[worldHeight, worldWidth]; for (int x = 0; x < worldWidth; x++) { for (int z = 0; z < worldHeight; z++) { float currentNoise = noise[x + xOrg, z + yOrg]; float vScale = (currentNoise * 3f) - 0.9f; Color currentColor; string type; string occupiedBy = "-"; int[] coords = { x, z }; switch (currentNoise) { case float n when(currentNoise >= 0.45): var tileSeed = Random.Range(0, 10); var buildingSeed = Random.Range(0.0f, 1.0f); if (tileSeed > 8) { currentColor = new Color(0f, 0.42f, 0f); type = "Forest"; if (x > 0 && z > 0) { var additionalTreeSeed = Random.Range(0, 10); if (additionalTreeSeed > 3) { if (tiles[x - 1, z].type == "Plains") { tiles[x - 1, z].color = new Color(0f, 0.42f, 0f); tiles[x - 1, z].type = "Forest"; } } if (additionalTreeSeed > 6) { if (tiles[x, z - 1].type == "Plains") { tiles[x, z - 1].color = new Color(0f, 0.42f, 0f); tiles[x, z - 1].type = "Forest"; } } } } else { if (buildingSeed > 0.99f) { currentColor = Color.magenta; type = "Plains"; occupiedBy = "Building"; } else { currentColor = new Color(0.02f, 0.85f, 0f); type = "Plains"; } } break; case float n when(currentNoise >= 0.35): currentColor = new Color(255f / 255f, 249f / 255f, 177f / 255f); type = "Sand"; break; default: currentColor = Color.blue; type = "Ocean"; vScale = 0f; break; } Tile tile = new Tile(occupiedBy, type, currentColor, currentNoise, coords, null, vScale); tiles[x, z] = tile; tilesList = tiles; } } return(tiles); }
// Start is called before the first frame update void Start() { isGenerating = false; noise = new PerlinNoiseGrid(pixWidth, pixHeight, scale); StartCoroutine(CreateWorld(generateTileGrid(noise))); }