Search code examples
c#monogametiledmonogame-extended

Monogame C# - How do I draw my sprites between tilemap layer 0 and 1?


I have a tilemap:

this.TileMap = Content.Load<TiledMap>("Maps/MyTileMap");
_tiledMapRenderer = new TiledMapRenderer(GraphicsDevice, this.TileMap);

I would like to render my normal sprites between layer 0 and 1. Kinda like this:

DrawLayer(0, 0f);
foreach(var sprite in this.Sprites)
{
    sprite.Draw(spriteBatch, gameTime);
}
DrawLayer(1, 1f);

sprite.Draw does:

var destRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
spriteBatch.Draw(Texture, destRectangle, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.2f);

And DrawLayer does:

var layer = this.TileMap.Layers[idx];
_tiledMapRenderer.Draw(layer, this.Camera.GetViewMatrix(), depth: depth);

But my regular sprites are still on top of everything.

enter image description here

The terrain is layer 0, the tree is on layer 1 (obviously).

I would think providing layerDepth should assure the correct draw order, but clearly it doesn't.

Any pointers on how to fix this?

Or, alternatively,

how can I add my sprites to a layer? 

I heard that's an option, but I never found out how.


Solution

  • As expected / hoped, it had to be something simple because I can't believe everyone would use Tiled, if it doesn't actually produce a workable result.

    So what worked is adding this to the .Begin():

    _spriteBatch.Begin(sortMode: SpriteSortMode.ImmediatetransformMatrix: this.Camera.GetViewMatrix(), );
    

    That gets me this:

    enter image description here