Added comments to CreateRandomPlatforms().

This commit is contained in:
J. M. Ellis
2024-06-03 21:05:20 -07:00
parent 7fdfc20d57
commit a184a78189
+11 -2
View File
@@ -101,6 +101,7 @@ namespace LunaLightXMG
public static List<Platform> CreateRandomPlatforms(int numberOfPlatforms)
{
List<Platform> platforms = new List<Platform>();
// Static platforms
platforms.Add(new Platform(new Vector2(16, 132), 16, 16));
platforms.Add(new Platform(new Vector2(128, 132), 16, 16));
platforms.Add(new Platform(new Vector2(176, 132), 16, 16));
@@ -112,16 +113,19 @@ namespace LunaLightXMG
int x = 0;
int y = 0;
// Create platforms at random x,y coordinate until a valid position is found.
while (!validPosition)
{
x = random.Next(1, 18) * 16; // Random x multiple of 16 between 16 and 288
y = random.Next(3, 9) * 16; // Random y multiple of 16 between 48 and 132
// Check if position is already in the platforms list
validPosition = !platforms.Exists(p => p.Position.X == x && p.Position.Y == y);
// If it's already taken, look at the space to the right.
if (!validPosition)
{
x += 16;
// If all spaces are taken in that row, move to the above row.
if (x > 288)
{
x = 16;
@@ -129,12 +133,17 @@ namespace LunaLightXMG
}
}
}
// Add valid platform to the platforms list.
platforms.Add(new Platform(new Vector2(x, y + 4), 16, 16));
// If we aren't at the end of the row, add two more platforms.
if (x + 16 < 288)
{
for (int j = 1; j < 3; j++)
{
platforms.Add(new Platform(new Vector2(x + (j*16), y + 4), 16, 16));
int nextX = j * 16;
if (!platforms.Exists(p => p.Position.X == nextX && p.Position.Y == y)) {
platforms.Add(new Platform(new Vector2(x + (j * 16), y + 4), 16, 16));
}
}
}
}