diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs index 9ed90bd..7ec5a36 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs @@ -24,7 +24,8 @@ namespace DungeonGenerator _yLength = 28; Random random = new Random(); - _entrancePoints = GenerateEntrancePoints(_xLength, _yLength, random.Next(1,4)); + List entranceRooms = GenerateEntranceRooms(_xLength, _yLength, random.Next(1,4)); + _entrancePoints = entranceRooms.SelectMany(room => room.GetPointsInRoom()).ToList(); EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, _entrancePoints); //TODO calculate L and W from length int numberOfMonsterRooms = 7; // TODO: Calculate from ratio @@ -41,6 +42,7 @@ namespace DungeonGenerator public void PrintMap() { + List monsterRoomPoints = _monsterRooms.SelectMany(room => room.GetPointsInRoom()).ToList(); char[,] mapMatrix = new Char[_xLength, _yLength]; for (int x = 0; x < _xLength; x++) { @@ -50,13 +52,13 @@ namespace DungeonGenerator { mapMatrix[x, y] = '*'; } - else if (_monsterRooms.Any(room => room.PositionOfTopLeft.Equals(new Point(x,y)))) + else if (monsterRoomPoints.Contains(new Point(x,y))) { mapMatrix[x, y] = 'X'; } else { - mapMatrix[x, y] = 'O'; + mapMatrix[x, y] = '-'; } } } @@ -86,15 +88,15 @@ namespace DungeonGenerator Left } - private List GenerateEntrancePoints(int xLengthOfDungeon, int yLengthOfDungeon, int numberOfEntranceLines) + private List GenerateEntranceRooms(int xLengthOfDungeon, int yLengthOfDungeon, int numberOfEntranceLines) { if (numberOfEntranceLines > 4) { throw new Exception("Number of entrance lines cannot be greater than 4"); } - const int entranceSpaces = 12; - List entrancePoints = new List(); + const int numEntranceRooms = 12; + List entranceRooms = new List(); Random random = new Random(); Dictionary sidesToEnterOn = new() { @@ -118,30 +120,31 @@ namespace DungeonGenerator } } - int pointsPerLine = entranceSpaces / numberOfEntranceLines; + int roomsPerLine = numEntranceRooms / numberOfEntranceLines; + int bottom = yLengthOfDungeon - SIDE_LENGTH_OF_NORMAL; + int oneRoomFromBottom = bottom - SIDE_LENGTH_OF_NORMAL; + int right = xLengthOfDungeon - SIDE_LENGTH_OF_NORMAL; // Generate entrance lines on each side switch (sidesToEnterOn[Side.Top]) { case 2: // Add half of points for this entrance line to the top left side - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(i,0)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(i,0))); } // Add the rest of the points for this entrance line to the top right side. - int pointsRemaining = pointsPerLine; - for (var i = 0; i < pointsRemaining; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point((xLengthOfDungeon - 1) - i,0)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right - i,0))); } break; case 1: - int midpoint = xLengthOfDungeon / 2; - int startOfLine = midpoint - (pointsPerLine / 2); - for (var i = 0; i < pointsPerLine; i++) + int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL); + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(startOfLine + i, 0)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL,new Point(startOfLine + i, 0))); } break; } @@ -150,23 +153,22 @@ namespace DungeonGenerator { case 2: // Add points for this entrance line to the top right side - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(xLengthOfDungeon - 1, i + 1)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, i + SIDE_LENGTH_OF_NORMAL))); } // Add the rest of the points for this entrance line to the bottom right side. - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(xLengthOfDungeon - 1, (yLengthOfDungeon - 2) - i)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, oneRoomFromBottom - i))); } break; case 1: - int midpoint = yLengthOfDungeon / 2; - int startOfLine = midpoint - (pointsPerLine / 2); - for (int i = 0; i < pointsPerLine; i++) + int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL); + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(xLengthOfDungeon - 1, startOfLine + i)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, startOfLine + i))); } break; } @@ -175,23 +177,22 @@ namespace DungeonGenerator { case 2: // Add half of points for this entrance line to the bottom left side - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(i, yLengthOfDungeon - 1)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(i, bottom))); } // Add the rest of the points for this entrance line to the bottom right side. - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point((xLengthOfDungeon - 1) - i, yLengthOfDungeon - 1)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point((xLengthOfDungeon - 1) - i, bottom))); } break; case 1: - int midpoint = xLengthOfDungeon / 2; - int startOfLine = midpoint - (pointsPerLine / 2); - for (var i = 0; i < pointsPerLine; i++) + int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL); + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(startOfLine + i, yLengthOfDungeon - 1)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(startOfLine + i, bottom))); } break; } @@ -200,28 +201,33 @@ namespace DungeonGenerator { case 2: // Add half of points for this entrance line to the top left side - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(0, i + 1)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, i + SIDE_LENGTH_OF_NORMAL))); } // Add the rest of the points for this entrance line to the bottom left side. - for (var i = 0; i < pointsPerLine; i++) + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(0, (yLengthOfDungeon - 1) - i)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, oneRoomFromBottom - i))); } break; case 1: - int midpoint = yLengthOfDungeon / 2; - int startOfLine = midpoint - (pointsPerLine / 2); - for (int i = 0; i < pointsPerLine; i++) + int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL); + for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL) { - entrancePoints.Add(new Point(0, startOfLine + i)); + entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, startOfLine + i))); } break; } - return entrancePoints; + return entranceRooms; + } + + private int GetStartOfCenteredLine(int length, int numberOfRooms, int sizeOfRoom) + { + int midpoint = length / 2; + return midpoint - (numberOfRooms * sizeOfRoom / 2); } } } \ No newline at end of file diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs index d528706..26b4afe 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs @@ -37,7 +37,7 @@ namespace DungeonGenerator { int numCandidates = 50; // Increasing improves results but greatly effects performance. Random rnd = new Random(); - int randomIndex = rnd.Next(_availablePoints.Count); + int randomIndex = rnd.Next(0, _availablePoints.Count); Point bestCandidate = new Point(Int32.MaxValue, Int32.MaxValue); int bestDistance = 0; for (var i = 0; i < numCandidates; i++) diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs index e61944d..248e6e0 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs @@ -1,8 +1,11 @@ +using System.Collections.Generic; + namespace DungeonGenerator { public enum RoomType { Normal, + Entrance, Monster, } @@ -20,5 +23,19 @@ namespace DungeonGenerator public int Height { get; set; } public int Width { get; set; } public Point PositionOfTopLeft { get; set; } + + public List GetPointsInRoom() + { + List points = new List(); + for (int i = 0; i < Width; i++) + { + for (int j = 0; j < Height; j++) + { + points.Add(new Point(PositionOfTopLeft.X + i, PositionOfTopLeft.Y + j)); + } + } + + return points; + } } } \ No newline at end of file