Added platform generator class to platform.cs.

This commit is contained in:
J. M. Ellis
2024-06-02 17:08:27 -07:00
parent 0e8ac75cef
commit 059a3f9534
3 changed files with 123 additions and 76 deletions
+110
View File
@@ -1,5 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;
namespace LunaLightXMG
{
@@ -19,5 +21,113 @@ namespace LunaLightXMG
spriteBatch.Draw(texture, Position, Color.White);
}
}
public class PlatformGenerator
{
private static Random random = new Random();
public static Platform[] CreatePlatforms(int numberOfPlatforms)
{
// Bottom wall (static)
List<Platform> bottomPlatforms = new List<Platform>
{
new Platform(new Vector2(0, 164), 16, 16),
new Platform(new Vector2(16, 164), 16, 16),
new Platform(new Vector2(32, 164), 16, 16),
new Platform(new Vector2(48, 164), 16, 16),
new Platform(new Vector2(64, 164), 16, 16),
new Platform(new Vector2(80, 164), 16, 16),
new Platform(new Vector2(96, 164), 16, 16),
new Platform(new Vector2(112, 164), 16, 16),
new Platform(new Vector2(128, 164), 16, 16),
new Platform(new Vector2(144, 164), 16, 16),
new Platform(new Vector2(160, 164), 16, 16),
new Platform(new Vector2(176, 164), 16, 16),
new Platform(new Vector2(192, 164), 16, 16),
new Platform(new Vector2(208, 164), 16, 16),
new Platform(new Vector2(224, 164), 16, 16),
new Platform(new Vector2(240, 164), 16, 16),
new Platform(new Vector2(256, 164), 16, 16),
new Platform(new Vector2(272, 164), 16, 16),
new Platform(new Vector2(288, 164), 16, 16),
new Platform(new Vector2(304, 164), 16, 16)
};
// Left side wall (static)
List<Platform> leftSidePlatforms = new List<Platform>
{
new Platform(new Vector2(0, -12), 16, 16),
new Platform(new Vector2(0, 4), 16, 16),
new Platform(new Vector2(0, 20), 16, 16),
new Platform(new Vector2(0, 36), 16, 16),
new Platform(new Vector2(0, 52), 16, 16),
new Platform(new Vector2(0, 68), 16, 16),
new Platform(new Vector2(0, 84), 16, 16),
new Platform(new Vector2(0, 100), 16, 16),
new Platform(new Vector2(0, 116), 16, 16),
new Platform(new Vector2(0, 132), 16, 16),
new Platform(new Vector2(0, 148), 16, 16)
};
// Right side wall (static)
List<Platform> rightSidePlatforms = new List<Platform>
{
new Platform(new Vector2(304, -12), 16, 16),
new Platform(new Vector2(304, 4), 16, 16),
new Platform(new Vector2(304, 20), 16, 16),
new Platform(new Vector2(304, 36), 16, 16),
new Platform(new Vector2(304, 52), 16, 16),
new Platform(new Vector2(304, 68), 16, 16),
new Platform(new Vector2(304, 84), 16, 16),
new Platform(new Vector2(304, 100), 16, 16),
new Platform(new Vector2(304, 116), 16, 16),
new Platform(new Vector2(304, 132), 16, 16),
new Platform(new Vector2(304, 148), 16, 16)
};
// Generate random platforms
List<Platform> randomizedPlatforms = CreateRandomPlatforms(numberOfPlatforms);
// Combine all platforms
List<Platform> allPlatforms = new List<Platform>();
allPlatforms.AddRange(randomizedPlatforms);
allPlatforms.AddRange(bottomPlatforms);
allPlatforms.AddRange(leftSidePlatforms);
allPlatforms.AddRange(rightSidePlatforms);
return allPlatforms.ToArray();
}
public static List<Platform> CreateRandomPlatforms(int numberOfPlatforms)
{
List<Platform> platforms = new List<Platform>();
for (int i = 0; i < numberOfPlatforms; i++)
{
bool validPosition = false;
int x = 0;
int y = 0;
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
validPosition = !platforms.Exists(p => p.Position.X == x && p.Position.Y == y);
if (!validPosition)
{
x += 16;
if (x > 288)
{
x = 16;
y += 16;
}
}
}
platforms.Add(new Platform(new Vector2(x, y + 4), 16, 16));
}
return platforms;
}
}
}