Original Post
Okay, I sort of got a tile demo working. But I can''t figure out how to do a good depth sort. I tried to implement my own sorter, but it didn''t work.
How would I do this?
, and the ordering used: Suppose you have 2 entities, A and B, if A.x + A.y > B.x + B.y, then B will be drawn before A (convince yourself that the equation x + y = c describes a horizontal line on the screen). I also keep pointers to the first entity in each layer, these have to be fixed every frame to ensure that they really do point to the first entity in the list.
/**
Moves the Entity to the right position in the linked list.
The ordering used is the order in which the Entities should be painted,that is,if A.y + A.x< B.y + B.x, then A should be painted before B.
*/
public void updateInLayer()
{
//If the Entity (if any) that comes after this one should be painted first,
//then we have to move forwards in the list until the next Entity
//(if any) is to be painted after this one
if(next != null && (next.position.y + next.position.x) < (position.y + position.x))
{ //First remove this drawable from current position, and patch "gap"
if(last != null)
last.next = next;
next.last = last;
//Now move until positioned correctly
do
{
last = next;
next = next.next;
}
while(next != null && (next.position.y + next.position.x) < (position.y + position.x));
//Make last and next point to this, to insert in list
if(next != null)
next.last = this;
last.next = this;
}
//If the Entity that comes before this one should be painted later,
//then we have to move backwards in the list
else if(last != null && (last.position.y + last.position.x) > (position.y + position.x))
{
if(next != null)
next.last = last;
last.next = next;
do
{
next = last;
last = last.last;
}
while(last != null && (last.position.y + last.position.x) > (position.y + position.x));
if(last != null)
last.next = this;
next.last = this;
}
}
/**
Some Entity objects might have been moved to a position before the previous first Entity.
Fix this.
*/
private void updateLayers()
{
for(int i = 0; i < layers.length; i++)
layers[i] = layers[i].getFirst();
}
public Entity getFirst()
{
Entity first = this;
while(first.last != null)
first = first.last;
return first;
}
public void insertInLayer(Entity layer)
{
next = layer;
if(layer != null)
layer.last = this;
updateInLayer();
}
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more