- Home /
Other
How to return tile based on heightmap efficiently?
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 AllTileTypes)
{
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 waaay too complex, even if it only happens once on world generation.
Any improvement suggestions are greatly appreciated.
Follow this Question
Related Questions
How do I set up my sprites/skeletons to correctly interact with isometric sorting layers? 2 Answers
Best way to make a big 2D map as smooth as possible? 1 Answer
Best way to handle large tilemap? 2 Answers
Draw 2d map array with SetTile or Instantiate ? 0 Answers
How to assign individual data to a tiles? (Unity3D Tilemap) 0 Answers