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
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

+13 -76
View File
@@ -63,16 +63,6 @@ namespace LunaLightXMG
random = new Random(42); random = new Random(42);
} }
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
{
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
{
resizing = true;
CalculateRenderDestination();
resizing = false;
}
}
protected override void Initialize() protected override void Initialize()
{ {
// Set initial window size // Set initial window size
@@ -87,71 +77,8 @@ namespace LunaLightXMG
// Play music // Play music
PlayBackgroundMusic(sinkBeats); PlayBackgroundMusic(sinkBeats);
// Test initialize platforms -- this should be abstracted to a class // Generate random platforms - test this idea out...
// Also, I'm assuming using a tool like tiled will prevent having to platforms = PlatformGenerator.CreatePlatforms(14);
// manually input platforms like below...
platforms = new Platform[]
{
// platforms
new Platform(new Vector2(48, 128), 16, 16),
new Platform(new Vector2(64, 128), 16, 16),
new Platform(new Vector2(96, 96), 16, 16),
new Platform(new Vector2(112, 96), 16, 16),
new Platform(new Vector2(192, 128), 16, 16),
new Platform(new Vector2(208, 128), 16, 16),
new Platform(new Vector2(240, 96), 16, 16),
new Platform(new Vector2(256, 96), 16, 16),
new Platform(new Vector2(192, 64), 16, 16),
new Platform(new Vector2(208, 64), 16, 16),
new Platform(new Vector2(144, 32), 16, 16),
new Platform(new Vector2(160, 32), 16, 16),
new Platform(new Vector2(288, 128), 16, 16),
new Platform(new Vector2(16, 96), 16, 16),
// bottom
new Platform(new Vector2(16, 160), 16, 16),
new Platform(new Vector2(32, 160), 16, 16),
new Platform(new Vector2(48, 160), 16, 16),
new Platform(new Vector2(64, 160), 16, 16),
new Platform(new Vector2(80, 160), 16, 16),
new Platform(new Vector2(96, 160), 16, 16),
new Platform(new Vector2(112, 160), 16, 16),
new Platform(new Vector2(128, 160), 16, 16),
new Platform(new Vector2(144, 160), 16, 16),
new Platform(new Vector2(160, 160), 16, 16),
new Platform(new Vector2(176, 160), 16, 16),
new Platform(new Vector2(192, 160), 16, 16),
new Platform(new Vector2(208, 160), 16, 16),
new Platform(new Vector2(224, 160), 16, 16),
new Platform(new Vector2(240, 160), 16, 16),
new Platform(new Vector2(256, 160), 16, 16),
new Platform(new Vector2(272, 160), 16, 16),
new Platform(new Vector2(288, 160), 16, 16),
// left side
new Platform(new Vector2(0, 0), 16, 16),
new Platform(new Vector2(0, 16), 16, 16),
new Platform(new Vector2(0, 32), 16, 16),
new Platform(new Vector2(0, 48), 16, 16),
new Platform(new Vector2(0, 64), 16, 16),
new Platform(new Vector2(0, 80), 16, 16),
new Platform(new Vector2(0, 96), 16, 16),
new Platform(new Vector2(0, 112), 16, 16),
new Platform(new Vector2(0, 128), 16, 16),
new Platform(new Vector2(0, 144), 16, 16),
// right side
new Platform(new Vector2(304, 0), 16, 16),
new Platform(new Vector2(304, 16), 16, 16),
new Platform(new Vector2(304, 32), 16, 16),
new Platform(new Vector2(304, 48), 16, 16),
new Platform(new Vector2(304, 64), 16, 16),
new Platform(new Vector2(304, 80), 16, 16),
new Platform(new Vector2(304, 96), 16, 16),
new Platform(new Vector2(304, 112), 16, 16),
new Platform(new Vector2(304, 128), 16, 16),
new Platform(new Vector2(304, 144), 16, 16)
};
} }
@@ -169,7 +96,7 @@ namespace LunaLightXMG
// Spawn 10 enemies at the start of the level // Spawn 10 enemies at the start of the level
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
Vector2 spawnPosition = new Vector2(16 + i*3,16); Vector2 spawnPosition = new Vector2(16 + i*3,-16);
Enemy enemy = new Enemy(spawnPosition, GetRandomValueBetween1And3()); Enemy enemy = new Enemy(spawnPosition, GetRandomValueBetween1And3());
enemy.LoadContent(Content); enemy.LoadContent(Content);
enemies.Add(enemy); enemies.Add(enemy);
@@ -257,6 +184,16 @@ namespace LunaLightXMG
} }
} }
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
{
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
{
resizing = true;
CalculateRenderDestination();
resizing = false;
}
}
private void CalculateRenderDestination() private void CalculateRenderDestination()
{ {
Point size = GraphicsDevice.Viewport.Bounds.Size; Point size = GraphicsDevice.Viewport.Bounds.Size;
+110
View File
@@ -1,5 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;
namespace LunaLightXMG namespace LunaLightXMG
{ {
@@ -19,5 +21,113 @@ namespace LunaLightXMG
spriteBatch.Draw(texture, Position, Color.White); 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;
}
}
} }