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
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue