bool TestMapCandidate(int[,] grid, float requiredFillPercent) { Vector2 randomOpenGridCoord = GetRandomOpenGridCoord(); FloodFiller floodFillTester = new FloodFiller(grid); bool[,] filled = floodFillTester.Fill((int)randomOpenGridCoord.x, (int)randomOpenGridCoord.y); int total = 0; int totalFilled = 0; for (int y = 0; y < filled.GetLength(0); y++) { for (int x = 0; x < filled.GetLength(1); x++) { total++; if (filled[y, x]) { totalFilled++; } } } float filledPercent = (float)totalFilled / (float)total; return(filledPercent >= requiredFillPercent); }
void BoardSetup() { bool passed = false; int count = 0; bool[,] randomContinuousRegion = new bool[mapHeight, mapWidth]; while (!passed && count < 30) { count++; grid = GenerateMapCandidate(); Vector2 randomOpenGridCoord = GetRandomOpenGridCoord(); FloodFiller floodFillTester = new FloodFiller(grid); randomContinuousRegion = floodFillTester.Fill((int)randomOpenGridCoord.x, (int)randomOpenGridCoord.y); float total = 0f; float totalFilled = 0f; for (int y = 0; y < randomContinuousRegion.GetLength(0); y++) { for (int x = 0; x < randomContinuousRegion.GetLength(1); x++) { total++; if (randomContinuousRegion[y, x]) { totalFilled++; } } } passed = totalFilled / total >= 0.3f; } terrainArray = new GameObject[mapHeight, mapWidth]; for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { terrainArray[y, x] = randomContinuousRegion[y, x] ? floorTile : wallTile; } } board = new GameObject("Board").transform; boardTiles = new GameObject[mapHeight, mapWidth]; for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { GameObject toInstantiate = terrainArray[y, x]; boardTiles[y, x] = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity, board); } } }