bool GenerateMap(ArrayGrid <MapElement> map, ArrayGrid <int> valueMap) { // create a game of life cave int densityCount = Mathf.FloorToInt(map.Count * density); //fill with random walls for (int i = 0; i < densityCount; ++i) { Vector2Int?fillPos = map.GetRandomPositionOfType(defaultFillElement); if (fillPos == null || !fillPos.HasValue) { return(false); } map[fillPos.Value] = defaultWallElement; } for (int i = 0; i < iterations; ++i) { IEnumerator <Vector2Int> mapIter = IterateOverMap(map); while (mapIter.MoveNext()) { Vector2Int current = mapIter.Current; int n = map.GetAdjacentPositionsOfType(current, true, defaultWallElement).Count; if (map[current] == defaultWallElement) { if (n < 4) { map[current] = defaultRoomElement; } } else { if (n > 4) { map[current] = defaultWallElement; } } if (map.IsPositionOnEdge(current)) { map[current] = defaultWallElement; } } } ConnectClosestRooms(map, valueMap, false, false); ConvertValuesToTiles(map, valueMap); return(true); }