I am procedurally generating a world and for each position in the Tilemap I call this function in order to get the tile type that matches the height:
public static NaturalTile GetNaturalTileByHeight(float height)
{
SortedDictionary<float, NaturalTile> tilesByHeight = new SortedDictionary<float, NaturalTile>();
foreach (var tile in NaturalTileTypes)
{
if (tile.MaxGenHeight > 0) tilesByHeight.Add(tile.MaxGenHeight, tile);
}
foreach (var tile in tilesByHeight)
{
if (height <= tile.Key) return tile.Value;
}
return null;
}
For each single tile, I have to first create a sorted dictionary so that it's sorted by height. Then, I have to fill the sorted dictionary with the tiles based on height. Finally, I compare the height with each tile in the sorted dictionary, and since it's sorted by height it'll always return the lower-valued tiles first, which makes it work properly.
I feel like this is way too complex, even if it only happens once on world generation.
Any improvement suggestions are greatly appreciated.