List <Vector3Int> CreateTunnelFromRoom(Rect room) { int maxLength = 12; int lengthBeforeChangingDirection = (int)(Random.value * 5) + 2; Directions dir; bool connected = false; int length = 0; int changeDirectionCounter = 0; Vector3Int cursor = VectorOperations.GenerateRandomPositionOnBorder(room); dir = DirectonsOperations.GetTunnelDirectionFromRoom(cursor, room); map.structures.SetTile(cursor, null); List <Vector3Int> tunnelTiles = new List <Vector3Int>(); while (!connected) { if (cursor.x > -map.sizeX / 2 + 1 && cursor.x < map.sizeX / 2 - 1 && cursor.y > -map.sizeY / 2 + 1 && cursor.y < map.sizeY / 2 - 1) { map.ground.SetTile(cursor, tiles.GetIndoorTile()); tunnelTiles.Add(cursor); cursor = VectorOperations.MoveCursor(cursor, dir); } else { connected = true; } if (tiles.GetIndoorTiles().Contains(map.ground.GetTile(cursor))) { //merge into another tunnel connected = true; } if (changeDirectionCounter == lengthBeforeChangingDirection) { //random direction change lengthBeforeChangingDirection = (int)(Random.value * 5) + 2; dir = DirectonsOperations.ChangeDirection(dir); changeDirectionCounter = 0; } length++; changeDirectionCounter++; if (length == maxLength) { // end of tunnel map.ground.SetTile(cursor, tiles.GetIndoorTile()); connected = true; } } return(tunnelTiles); }