From a184a78189cfaa5d643fff0f0e1c14e27353b2bc Mon Sep 17 00:00:00 2001 From: "J. M. Ellis" Date: Mon, 3 Jun 2024 21:05:20 -0700 Subject: [PATCH] Added comments to CreateRandomPlatforms(). --- Platform.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Platform.cs b/Platform.cs index 4d47222..09fe54f 100644 --- a/Platform.cs +++ b/Platform.cs @@ -101,6 +101,7 @@ namespace LunaLightXMG public static List CreateRandomPlatforms(int numberOfPlatforms) { List platforms = new List(); + // 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)); + } } } }