Refactored dungeon generation with new DungeonMap class.
This commit is contained in:
parent
8ed53b6384
commit
2cd9df5652
5 changed files with 147 additions and 62 deletions
|
|
@ -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());
|
||||
dungeonMap.AddRooms(GenerateEntranceRooms(_xLength, _yLength, random.Next(1,4)));
|
||||
dungeonMap.AddRoom(GenerateOnlyBossRoom(_xLength, _yLength, WIDTH_OF_BOSS, HEIGHT_OF_BOSS));
|
||||
|
||||
_bossRoom = GenerateOnlyBossRoom(_xLength, _yLength, WIDTH_OF_BOSS, HEIGHT_OF_BOSS);
|
||||
pointsToExclude.AddRange(_bossRoom.GetPointsInRoom());
|
||||
|
||||
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)
|
||||
|
|
|
|||
130
PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonMap.cs
Normal file
130
PuzzleGameProject/Assets/Scripts/DungeonGenerator/DungeonMap.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DungeonGenerator
|
||||
{
|
||||
public class DungeonMap
|
||||
{
|
||||
private int _width;
|
||||
private int _height;
|
||||
private List<Room> _monsterRooms = new List<Room>();
|
||||
private List<Room> _entranceRooms = new List<Room>();
|
||||
private Room _bossRoom;
|
||||
private HashSet<Point> _unoccupiedPoints = new HashSet<Point>();
|
||||
|
||||
public DungeonMap(int width, int height)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, _width)
|
||||
.SelectMany(x => Enumerable.Range(0, _height)
|
||||
.Select(y => new Point(x,y))));
|
||||
}
|
||||
|
||||
public void AddRoom(Room room)
|
||||
{
|
||||
switch (room.TypeOfRoom)
|
||||
{
|
||||
case RoomType.Monster:
|
||||
_monsterRooms.Add(room);
|
||||
break;
|
||||
case RoomType.Entrance:
|
||||
_entranceRooms.Add(room);
|
||||
break;
|
||||
case RoomType.Boss:
|
||||
_bossRoom = room;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_unoccupiedPoints.ExceptWith(room.GetPointsInRoom());
|
||||
}
|
||||
|
||||
public void AddRooms(List<Room> rooms)
|
||||
{
|
||||
switch (rooms[0].TypeOfRoom)
|
||||
{
|
||||
case RoomType.Monster:
|
||||
_monsterRooms.AddRange(rooms);
|
||||
break;
|
||||
case RoomType.Entrance:
|
||||
_entranceRooms.AddRange(rooms);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_unoccupiedPoints.ExceptWith(rooms.SelectMany(room => room.GetPointsInRoom()));
|
||||
}
|
||||
|
||||
public List<Room> GetMonsterRooms()
|
||||
{
|
||||
return _monsterRooms;
|
||||
}
|
||||
|
||||
public Room GetBossRoom()
|
||||
{
|
||||
return _bossRoom;
|
||||
}
|
||||
|
||||
public List<Room> GetEntranceRooms()
|
||||
{
|
||||
return _entranceRooms;
|
||||
}
|
||||
|
||||
public HashSet<Point> GetUnoccupiedPoints()
|
||||
{
|
||||
return new HashSet<Point>(_unoccupiedPoints);
|
||||
}
|
||||
|
||||
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[_width, _height];
|
||||
for (int x = 0; x < _width; x++)
|
||||
{
|
||||
for (int y = 0; y < _height; 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: {_width}{Environment.NewLine}" +
|
||||
$"Y Length: {_height}{Environment.NewLine}" +
|
||||
$"Monster Rooms: {_monsterRooms.Count}{Environment.NewLine}" +
|
||||
sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b8c880c12ad649f8a395e6367a9c8838
|
||||
timeCreated: 1738774051
|
||||
|
|
@ -15,9 +15,9 @@ namespace DungeonGenerator
|
|||
new Point(1000, 1000))};
|
||||
private HashSet<Point> _availablePoints;
|
||||
|
||||
public EvenDisperser(int xLength, int yLength, List<Point> excludedPoints = null)
|
||||
public EvenDisperser(int xLength, int yLength, HashSet<Point> availablePoints)
|
||||
{
|
||||
_availablePoints = GenerateAvailablePoints(xLength, yLength, excludedPoints ?? new List<Point>());
|
||||
_availablePoints = availablePoints;
|
||||
}
|
||||
|
||||
private HashSet<Point> GenerateAvailablePoints(int xLength, int yLength, List<Point> excludedPoints)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using DungeonGenerator;
|
||||
using UnityEngine;
|
||||
|
||||
public class TestRun : MonoBehaviour
|
||||
|
|
@ -6,8 +7,7 @@ public class TestRun : MonoBehaviour
|
|||
void Start()
|
||||
{
|
||||
DungeonGenerator.DungeonGenerator gen = new DungeonGenerator.DungeonGenerator();
|
||||
gen.GenerateDungeon(0 , 0.5f);
|
||||
gen.PrintMap();
|
||||
DungeonMap dungeon = gen.GenerateDungeon(0 , 0.5f);
|
||||
dungeon.PrintMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue