Refactored dungeon generation with new DungeonMap class.

This commit is contained in:
Max 2025-02-06 09:40:47 +01:00
parent 8ed53b6384
commit 2cd9df5652
5 changed files with 147 additions and 62 deletions

View file

@ -18,79 +18,31 @@ namespace DungeonGenerator
private int _xLength = 40;
private int _yLength = 28;
private List<Room> _monsterRooms = new List<Room>();
private List<Room> _entranceRooms;
private Room _bossRoom;
public void GenerateDungeon(int length, float monsterRoomRatio)
public DungeonMap GenerateDungeon(int length, float monsterRoomRatio)
{
_xLength = 40;
_yLength = 28;
Random random = new Random();
List<Point> pointsToExclude = new List<Point>();
DungeonMap dungeonMap = new DungeonMap(_xLength, _yLength);
_entranceRooms = GenerateEntranceRooms(_xLength, _yLength, random.Next(1,4));
pointsToExclude.AddRange(_entranceRooms.SelectMany(room => room.GetPointsInRoom()).ToList());
_bossRoom = GenerateOnlyBossRoom(_xLength, _yLength, WIDTH_OF_BOSS, HEIGHT_OF_BOSS);
pointsToExclude.AddRange(_bossRoom.GetPointsInRoom());
dungeonMap.AddRooms(GenerateEntranceRooms(_xLength, _yLength, random.Next(1,4)));
dungeonMap.AddRoom(GenerateOnlyBossRoom(_xLength, _yLength, WIDTH_OF_BOSS, HEIGHT_OF_BOSS));
EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, pointsToExclude); //TODO calculate L and W from length
EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, dungeonMap.GetUnoccupiedPoints()); //TODO calculate L and W from length
int numberOfMonsterRooms = 7; // TODO: Calculate from ratio
for (var i = 0; i < numberOfMonsterRooms; i ++)
{
_monsterRooms.Add(disperser.GenerateAndPlaceRoom(
dungeonMap.AddRoom(disperser.GenerateAndPlaceRoom(
SIDE_LENGTH_OF_MONSTER,
SIDE_LENGTH_OF_MONSTER,
RoomType.Monster));
}
}
public void PrintMap()
{
List<Point> monsterRoomPoints = _monsterRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
List<Point> entranceRoomPoints = _entranceRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
char[,] mapMatrix = new Char[_xLength, _yLength];
for (int x = 0; x < _xLength; x++)
{
for (int y = 0; y < _yLength; y++)
{
if (entranceRoomPoints.Contains(new Point(x, y)))
{
mapMatrix[x, y] = '*';
}
else if (monsterRoomPoints.Contains(new Point(x,y)))
{
mapMatrix[x, y] = 'X';
}
else if (_bossRoom.GetPointsInRoom().Contains(new Point(x, y)))
{
mapMatrix[x, y] = 'B';
}
else
{
mapMatrix[x, y] = '-';
}
}
}
StringBuilder sb = new StringBuilder();
for (int j = 0; j < mapMatrix.GetLength(1); j++)
{
for (int i = 0; i < mapMatrix.GetLength(0); i++)
{
sb.Append(mapMatrix[i, j] + " ");
}
sb.Append(Environment.NewLine);
}
Debug.Log($"{DateTime.Now}{Environment.NewLine}" +
$"X Length: {_xLength}{Environment.NewLine}" +
$"Y Length: {_yLength}{Environment.NewLine}" +
$"Monster Rooms: {_monsterRooms.Count}{Environment.NewLine}" +
sb.ToString());
return dungeonMap;
}
private Room GenerateOnlyBossRoom(int xLengthOfDungeon, int yLengthOfDungeon, int width, int heigth)