I would like to change the layer depth of objects, so that the player can go behind them. Right now it looks like this when the player is behind the object:
The problem is that the layer depth doesn't take into account the players location in relation to the object, so it doesn't look correct when the player is in front of the object:
What would be a good way to accomplish this? Could it be done just by comparing the locations of collision boxes (green boxes in the image) when they overlap? Or should I calculate and change the depth for all objects currently on-screen? How could I do that?
Thanks.
[UPDATE]
Here is the draw call from the base class for game objects:
public virtual void Draw(SpriteBatch spriteBatch) {
if (texture != null && Active == true) {
spriteBatch.Draw(texture, position, null, Color, 0f, Vector2.Zero, Scale, SpriteEffects.None, LayerDepth);
}
DrawCollision(spriteBatch); // DEBUG
}
It's an overload of the built-in MonoGame SpriteBatch.Draw()
function. The thing that determines the draw order is LayerDepth
. LayerDepth is a property inside each object. Currently it is just set to a fixed value.
z = y / 1000
to scale it between 0 1 as you go from the top (back) to bottom (front) of the world. \$\endgroup\$