Order the drawing of sprites based on y-axis

I’m working on a little project which uses 2D TileMaps. I ran into the problem that I want to add transparent fences. It should draw the fence over the player if the player is behind it, and under the player if it is in front of it.

I thought I read somewhere that one can solve this by drawing sprites in order of their y-axis. Is this possible using amethyst and its Tilemap implementation?

Would be behind it:

I don’t know if what you want can be done automatically, but this is one way to achieve the same:

Perhaps you could update the sprites’s z-coordinate as a function of their y-coordinate. In the simplest scenario, you could just set the z-coordinate of all sprites the same as their y-coordinate. Presumably, you’ll update the y-coordinate for the player once every frame, just copy that value to the z as well.

The only thing you’ll have to look out for is that other things on the screen that must always be drawn on top (e.g. healthbars, particles, the camera itself) must receive a y-coordinate that is outside the range of possible values.

I thought of this as well, it is unfortunately not possible with the Tilemaps amethyst provides.