I'm trying to create random level platforms like this:
public Level(int platfromsInLevel, Texture2D sprite, int screenWidth)
{
rnd = new Random();
this.screenWidth = screenWidth;
for (int i = 0; i < platfromsInLevel; i++)
{
if (i == 0)
{
Platform platform = new Platform((new Vector2(0, 0)), (new Vector2(0, 0)), sprite);
Platformlist.Add(platform);
}
else
{
int CurrentX = (int)Platformlist[i - 1].getPos().X + Platformlist[i - 1].getTex().Width;
int CurrentY = (int)Platformlist[i - 1].getPos().Y + Platformlist[i - 1].getTex().Height;
int xRand = rnd.Next(CurrentX + 100,(CurrentX + 100) + 150);
int yRand = rnd.Next(CurrentY + 200, (CurrentY + 200) + 200);
if (xRand <= screenWidth || xRand >= 0)
{
Platform platform = new Platform((new Vector2(0, 0)), (new Vector2(xRand, yRand)), sprite);
Platformlist.Add(platform);
}
}
}
}
Can anyone help with the random logic? I only want them to appear on the screen that is visible on the X axis, but the y axis is infinite it can go as high as needed. The width of the screen is stored in screenWidth. I also don't think my random logic is taking into account the whole screen width, it only adds positive values. Finally I've tried to add some logic that says a platform can only be a certain height and distance away from another one whilst taking into account the previous platforms position, but its not really working.
Anyone got any ideas to tidy this up a little bit?