diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs index d867224..e959c75 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonGenerator.cs @@ -29,14 +29,13 @@ namespace DungeonGenerator EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, _entrancePoints); //TODO calculate L and W from length int numberOfMonsterRooms = 7; // TODO: Calculate from ratio + for (var i = 0; i < numberOfMonsterRooms; i ++) { - Point newSpot = disperser.GetPoint(SIDE_LENGTH_OF_MONSTER, SIDE_LENGTH_OF_MONSTER); - _monsterRooms.Add(new Room( - RoomType.Monster, + _monsterRooms.Add(disperser.GenerateAndPlaceRoom( + SIDE_LENGTH_OF_MONSTER, SIDE_LENGTH_OF_MONSTER, - SIDE_LENGTH_OF_MONSTER, - newSpot)); + RoomType.Monster)); } } diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs index 9dfa514..ee3c741 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/EvenDisperser.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using UnityEngine.UIElements; using Random = System.Random; @@ -7,17 +8,21 @@ namespace DungeonGenerator { public class EvenDisperser { - private List _samples = new List(){new Point(1,1)}; - private List _availablePoints; + private List _samples = new List(){new Room( + RoomType.Normal, + 1, + 1, + new Point(1000, 1000))}; + private HashSet _availablePoints; public EvenDisperser(int xLength, int yLength, List excludedPoints = null) { _availablePoints = GenerateAvailablePoints(xLength, yLength, excludedPoints ?? new List()); } - private List GenerateAvailablePoints(int xLength, int yLength, List excludedPoints) + private HashSet GenerateAvailablePoints(int xLength, int yLength, List excludedPoints) { - List availablePoints = new List(); + HashSet availablePoints = new HashSet(); for (var x = 0; x < xLength; x++) { for (var y = 0; y < yLength; y++) @@ -32,21 +37,21 @@ namespace DungeonGenerator return availablePoints; } - // Can place point so that the x lengt hand y length go out of bounds to the right and down. - public Point GetPoint(int xLength, int yLength) + public Room GenerateAndPlaceRoom(int xLength, int yLength, RoomType roomType) { int numCandidates = 100; // Increasing improves results but greatly effects performance. Random rnd = new Random(); - int randomIndex = rnd.Next(0, _availablePoints.Count); - Point bestCandidate = new Point(Int32.MaxValue, Int32.MaxValue); + Room bestCandidate = new Room(roomType, xLength, yLength, new Point(Int32.MaxValue, Int32.MaxValue)); int bestDistance = 0; for (var i = 0; i < numCandidates; i++) { - var candidate = _availablePoints[randomIndex]; - var distance = candidate.ManhattanDistance(FindClosest(_samples, candidate)); - if (distance > bestDistance) + var candidate = new Room( + roomType, xLength, yLength, _availablePoints.ToList()[rnd.Next(0, _availablePoints.Count)]); + var distance = candidate.GetDistanceToRoom(FindClosestRoom(_samples, candidate)); + if (distance > bestDistance + && candidate.GetPointsInRoom().All(room => _availablePoints.Contains(room))) { - RemoveArea(candidate, xLength, yLength, _availablePoints); + _availablePoints.ExceptWith(candidate.GetPointsInRoom()); bestCandidate = candidate; bestDistance = distance; } @@ -56,12 +61,12 @@ namespace DungeonGenerator return bestCandidate; } - private Point FindClosest(List options, Point reference) + private Room FindClosestRoom(List options, Room reference) { - Point closest = options[0]; + Room closest = options[0]; foreach (var option in options) { - if (reference.ManhattanDistance(option) < reference.ManhattanDistance(closest)) + if (reference.GetDistanceToRoom(option) < reference.GetDistanceToRoom(closest)) { closest = option; } @@ -69,16 +74,5 @@ namespace DungeonGenerator return closest; } - - private void RemoveArea(Point topLeft, int xLength, int yLength, List availablePoints) - { - for (var x = topLeft.X; x < topLeft.X + xLength; x++) - { - for (var y = topLeft.Y; y < topLeft.Y + yLength; y++) - { - availablePoints.Remove(new Point(x, y)); - } - } - } } } \ No newline at end of file diff --git a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs index 248e6e0..69513ae 100644 --- a/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs +++ b/PuzzleGameProject/Assets/Scripts/DungeonGenerator/Room.cs @@ -37,5 +37,17 @@ namespace DungeonGenerator return points; } + + public int GetDistanceToRoom(Room other) + { + Point centerOfRoom = GetCenterOfRoom(); + Point centerOfOther = other.GetCenterOfRoom(); + return centerOfRoom.ManhattanDistance(centerOfOther); + } + + public Point GetCenterOfRoom() + { + return new Point(PositionOfTopLeft.X + Width / 2, PositionOfTopLeft.Y + Height / 2); + } } } \ No newline at end of file