2

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.

Spikee
  • 3,967
  • 7
  • 35
  • 68
  • The top of the tree should always be drawn in a layer above the entity/player layer. In this example, there really should be a layer 3, and the bottom trunk of the tree should be layer 1. While player/entities are layer 2. – Krythic Jun 01 '21 at 23:15

2 Answers2

2

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

Spikee
  • 3,967
  • 7
  • 35
  • 68
0

That's not going to work, because you don't know what layers to assign to that tree relative to your person sprite. Specifically, if the person is south of the tree, you want it to draw on top of the tree, while when it's north of the tree, you want it to be under the tree.

You will instead have to sort your sprites so that they're draw from the bottom of the screen up.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 1
    Sorting? You can use `.OrderBy` and sort by the Y coordinate. – Blindy Jun 02 '21 at 14:23
  • I was thinking about a little more than that :-) I mean the draw logic. Would be good to see a functioning piece of code. – Spikee Jun 02 '21 at 17:34
  • Primarily the layered draw structure. There wouldn't be as much use to Tiled if you can only use the terrain layer. – Spikee Jun 03 '21 at 13:06
  • Sure, but what you're trying to draw *is* on the same layer. – Blindy Jun 03 '21 at 14:21
  • I'm not sure what you mean, the terrain is layer 0, the tree is separate, on the next layer (so 1). – Spikee Jun 03 '21 at 15:04
  • That's what I've been trying to tell you, the tree is on the same layer as your actors, and it must abide to the same y-sorting rules. – Blindy Jun 03 '21 at 15:26