Question

Within my game, I have to manage a lot of objects. Every object has Depth value that will be used when calling its draw function.

Every object is created with a layer-system:

Layer1: obj1 - obj2 - obj3 - obj4
Layer2: obj1 - obj2 - obj3 - obj4
Layer3: obj1 - obj2 - obj3 - obj4
Layer4: obj1 - obj2 - obj3 - obj4
ecc.

A Layer is just a list of objects.

My problem is how to manage every Depth of every layer.

From a higher level, Layer1 is on top of Layer2,Layer3,Layer4; Layer2 is bottom of Layer1 but on top of Layer3,Layer4. So, considering that the Depth value in the draw call is a value between 0 and 1, I just assigned to every level a value starting from 0, to 1, step 0.01. In this way I will have max 100 layers.

Now I need to define an order of Depth also within layers, so for every object: Layer1.obj1 is on top of obj2, obj3, obj4, but also on top of Layer2.obj1, Layer2.obj2 ecc.

How can I achieve this?

Was it helpful?

Solution

enum LayerState : byte
{
    One,
    Two,
    Three,
    Four
}
void DrawLayer(LayerState state)
{
    switch (state)
    {
        case One:
          foreach (object obj in Layer1)
            SpriteBatch.Draw(obj);
        break;
        case Two:
          foreach (object obj in Layer2)
            SpriteBatch.Draw(obj);
        break;
        case Three:
          foreach (object obj in Layer3)
            SpriteBatch.Draw(obj);
        break;
        case Four:
          foreach (object obj in Layer4)
            SpriteBatch.Draw(obj);
        break;
     }
}

public override void Draw()
{
   SpriteBatch.Begin();
   DrawLayer(LayerState.Two) // play with states for result
   SpriteBatch.End();
   SpriteBatch.Begin();
   DrawLayer(LayerState.One) // play with states for result
   SpriteBatch.End();
   SpriteBatch.Begin();
   DrawLayer(LayerState.Three) // play with states for result
   SpriteBatch.End();
   SpriteBatch.Begin();
   DrawLayer(LayerState.Four) // play with states for result
   SpriteBatch.End();
}

I'm sorry if it doesn't work. Just suggest. I think void DrawLayer() will manage Layers depth depends of LayerState and sprite batches manage layers' objects.

OTHER TIPS

You could further subdivide your 0.01 increments on your depth.

Alternatively you could simply use the draw order to decide what is drawn on top of other things?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top