I am creating a 2D Top-Down RPG game like the Legend of Zelda: A link to the past.
when the player is drawn i want to update the tree depth so that it is over and under the player when the player Y position is higher or lower than the trees Y position, i think whether the tree is on top or not should be decided by the players Y position, being lower or higher. is it possible to do this in this way? can i change the layer depth in the update method?
I'm not sure you can. i'm not very experienced when coding. If there's a right way of doing this even without useing the spriteBatch layer depth i would appreciate any help.
I have discovered how to use the layer depth in the
spriteBatch.Draw();
Method.
What i am attempting to do is shown in the images below
The code i am using to separate trees from grass tiles uses layer depth like below:
spriteBatch.Begin(SpriteSortMode.FrontToBack, blendState = null, samplerState = null, DepthStencilState.DepthRead);
spriteBatch.Draw(tree, treePos, null, null, Vector2.Zero, 0, null, Color.White, SpriteEffects.None, 0.6f);
spriteBatch.End();
Tile draw method for the
tile
has the float value set
0.5f
Here's a way to do it:
Slice your sprites and do tile-based rendering, starting from the ground up to the sky.
Say you have a tile grid whose each is 32*32 pixels, if you have a tree that is 32*96 pixels, then when sliced it will be like:
A // layer 2
A // layer 1
A // layer 0 (floor)
Logic:
Then obviously the part that should hide your player will be drawn correctly (before, if player is behind).
(note that the lowest tile of the tree should not be walk-able)