0

I have isometric tile game engine (diamond map style) and I need to sort objects what I draw. My objects are 1x1, 2x1, 4x2. How can I do it based on this code?

for (int osaY = 0; osaY < mapSize; osaY++)
        {
            for (int osaX = 0; osaX < mapSize; osaX++)
            {
                int x = osaX * 32;
                int y = osaY * 32;

                PlaceObject(thisObject, CartToIso(new Vector2(x, y)), new Vector2(osaX, osaY));
            }
        }
Earlgray
  • 647
  • 1
  • 8
  • 31
  • I'm not sure how the code you're showing matters, but generally you can get away with drawing an isometric scene purely based on the viewport's Y axis, with higher items being drawn first. You may also have layers, but within a layer, that should remain true. It should only get difficult if you have elevation, but that can be treated as layers as well. – Magus Jan 30 '14 at 23:53
  • What have you tried? Have you got a screenshot? What is the code intended to do? – craftworkgames Jan 31 '14 at 06:30

1 Answers1

0

How I solve sortings:
1. Creating IDisplay interface and adding it to every class which handles displaying Draw(SpriteBatch batch)
2. Creating a DisplayManager which has Add,Remove,Draw methods and as many layers(List of IDisplay objects) as you want. Personally, the mid layer is the layer where I sort my stuff. So I can put stuff behind sorted objects, and before sorted objects.
3. In the Main class which handles the drawing, call the DisplayManager's Draw function, which will run through the layers(Lists) and IDisplay items and call the Draw function on every item.

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
DisplayManager.Draw(spriteBatch);
spriteBatch.End();

And then in the DisplayManager:

public static void Draw(SpriteBatch batch){
   for (i = 0; i < bottom.Count; i++)
   {
       bottom[i].Draw(batch);
   }
   //Gets the model from the IDisplay item, and then it's Y position, and orders
   //the sort layer(list) by that.
   sort = sort.OrderBy(o => o.getModel.Position.Y).ToList();
   for (i = 0; i < sort.Count; i++)
   {
       sort[i].Draw(batch);
   }
   for (i = 0; i < top.Count; i++)
   {
       top[i].Draw(batch);
   }
}
Zhafur
  • 1,626
  • 1
  • 13
  • 31