My game is in HTML5 canvas, but I assume this concept applies to any language.
In the past I've used a multi-dimensional array to keep track of sprite positions. Here is a simplified example:
[ [ ,[[plant12]],,, ], [ ,[[apple]],,, ], [ ,,,, ], [ [[apple]],,[[apple]],, ], [ ,,,, ], ]
And this would always draw sprites with the correct z-index, as the map was always read left-to-right, top-to-bottom:
But with a more experienced game developer's advice, I started using quad trees for collision.
Since I will have a tree of objects with x/y coordinates, I thought I might as well abandon using a sprite map, and just draw the sprites based upon the quad tree data.
But the problem is that if the objects are not collected in a precise order, the sprites will be drawn in incorrect order, resulting in such:
If I am not using a sprite map, what is the best way for me to ensure I am drawing sprites in the correct order/z-index?
I am thinking that maybe I will make a collection method for the quad tree that will collect objects from a left->right, top->bottom order, and then draw each object's corresponding sprite in a loop. But honestly, that sounds a lot less inefficient that a having a predefined map, especially considering that all that traversing is a lot of recursive calls.
Another approach is to create some sort of z-index system, but I think that may be over-complicating things as well.
Any advice on how to approach this?