hello! thank you for testing my game!
before i begin:
“cutting the assets” is by default the easiest way. sure… if all your assets are made with multi-tiled objects already, then its a ton of time to cut them. the blessing is that you are at the start of the journey, meaning that you can change the design to accommodate an easier coding process. AKA: you could decide to do regular tiles the regular way (you can have tall object, my game UNLANDER have those. just gotta place origin at the base). this way you only use the easy regular Y sort:
here, my new game. and its just the mockup
that said…
because, you understand the complexity of your original problem, i figure that i can just explain what my “almost solution” is without showing too much thought process
the idea:
step 1) declare:
every time that you add one sprites, you should add these 3 info at declaration:
- an “origin” point. (doesn’t have to be the actual origin, you could add a custom point if you want). if we do @petlimpet’s method, its going to be the bottom most corner point of the base of the sprite.
- the length of the sprite (ex: how long the bench is, lets say 3 tiles. a chair would be 1)
- the direction that the sprite is oriented (north or west)
step 2) initialize an ID system:
for each objects → variable ID = loopIndex
step 2) scene array:
for each object ordered from highest variable to lowest. the variable is the length. if two objects have the same length, start with the one with highest Y (bottom most)
- put the object ID at the end of the array.
while stop is false
- check object in the array that is right before, if its in front or behind (i will discuss method below).
- if its behind the previous one swap these two.
- stop if at the end of array, or that the new object is in front.
step 3) assign Z
for each child in array change objects z order to child name (which will be the index)
Method problem:
there is one more problem in petlimpet’s mothod that i did not mention previously.
we know that we can sort a bench according to another object of length 1. but how do you sort a bench according to another bench?
consider this problem:
if bench blue is placed in this configuration in comparison to bench red, then it will be rendered in front even though its behind
to fix this you to take in account if the second bench is to the left or to the right, then use the green point or blue point accordingly.
one last problem in this method:
if a chair is very far away from the bench, then due to the angle of the math function, the chair will be ordered (in the array) in front. even though this would be fine between each other (would not mess the order between them), it may mess-up the order of the chair compared to other furnitures:
to fix this, the ideal function should look like this instead:
it does require two input points but thankfully, we can fetch the other points with math and the length of the bench.
de-ordering problem
lastly, about the ordering in the array, swapping the active object with another placed object can potentially change their order. this means that you potentially have to re-call the sorting function for objects previously sorted.
the elephant in the room: performance
also, if we talk performance, doing such a sorting every frame may become very costly. if nothing moves than you only have to call the sort function once at initialization. but if character or NPCs move, then the sorting has to happen all throughout their movement. it could be ok if the de-ordering problem doesn’t occur (because sorting one item could only be as hard as sorting one item). but if it does occur, that means that sorting a single item may mean sorting so many more (and every frame at that!)