Hi all.
I am making a driving game where the object is to run over zombies who are walking in the road, in front of you. The view is a perspective one and you are driving forward so lots of stuff on the road and around it coming down the screen and being scaled up in size.
I have two C# lists at the moment; one containing zombies and the other debris which appears in the road. I wanted to get the render order right so that when the zombie is higher up, than a debris object it would be rendered behind the debris and vise versa. If it was just two separate objects, this would be easier - I would use the Y pos value to determine which to draw to the screen first. However, I need to compare every zombie on the screen to every piece of debris.
So I constructed the following nests of loops and if statements to try and get this correct render order.
if (debrisList.Count != 0 && zombies.Count != 0) // If both types of sprite are on screen
{
foreach (Zombie zom in zombies)
{
foreach (Debris deb in debrisList)
{
if (zom.ReturnPosition().Y >= deb.ReturnPos().Y)
{
deb.Draw(this.spriteBatch);
zom.Draw(this.spriteBatch);
}
}
foreach(Debris deb in debrisList)
{
if (zom.ReturnPosition().Y <= deb.ReturnPos().Y)
{
zom.Draw(this.spriteBatch);
deb.Draw(this.spriteBatch);
}
}
}
}
else
{
foreach (Zombie zom in zombies)
{
zom.Draw(this.spriteBatch);
}
foreach (Debris deb in debrisList)
{
deb.Draw(this.spriteBatch);
}
}
What the code is supposed to do (in my mind) is first of all check if both lists are not empty. If one is empty then it draws to screen as normal and no consideration is given to the order. If both lists are not empty what should be going on is that the zombies list is iterated through and for each zombie, I compare its Y position to the Y position of each debris item and render accordingly.
However, the zombie is always rendered on top of the debris.
Hopefully I have explained what I am trying to do and hopefully someone can help me. This has been driving me crazy for a few hours now!
Thanks