static IEnumerator GenerateCoroutine(int sizeX, int sizeY, float seed, int minSpawnPoints, WorldFinishedEvent callback) { WorldMap world; // Repeat until we generate a valid world while (true) { world = new WorldMap { mapDict = new Dictionary <Vector2Int, TileType>() }; int tilesPerFrame = 100; int tilesDoneSinceFrame = 0; Dictionary <Vector2Int, float> heightmap = GenerateHeightMap(sizeX, sizeY, seed); // Loop through every tile defined by the size, fill it with grass and maybe add a plant for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { Vector2Int currentPosition = new Vector2Int(x, y); TileType newTileType; // Assign ground material based on height if (heightmap[currentPosition] > waterLevel) { if (UnityEngine.Random.value < forestProbability) { newTileType = TileTypeLibrary.GetTileType(ForestTileId); } else if (UnityEngine.Random.value < farmProbability) { newTileType = TileTypeLibrary.GetTileType(FarmTileId); } else { newTileType = TileTypeLibrary.GetTileType(PlainsTileId); } } else { newTileType = TileTypeLibrary.GetTileType(WaterTileId); } world.mapDict.Add(currentPosition, newTileType); tilesDoneSinceFrame++; if (tilesDoneSinceFrame >= tilesPerFrame) { tilesDoneSinceFrame = 0; yield return(null); } } } if (!WorldIsValid(world, minSpawnPoints)) { yield return(new WaitForSeconds(0.1f)); seed = Random.value * 1000; continue; } break; } callback(world); }
public static void StartGeneration(int sizeX, int sizeY, float seed, int minSpawnPoints, WorldFinishedEvent callback, MonoBehaviour genObject) { //float seed = Random.value * 1000; void OnFinished(WorldMap world) { callback(world); } genObject.StartCoroutine(GenerateCoroutine(sizeX, sizeY, seed, minSpawnPoints, OnFinished)); }