Implemented loading dungeon map json into Unity.
This commit is contained in:
parent
0ff1e92fb9
commit
ec466ee6cd
82 changed files with 252177 additions and 1068 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -42,10 +42,53 @@ namespace DungeonMapGenerator
|
|||
AddNormalRoomsAroundMonsterRooms(dungeonMap);
|
||||
AddNormalRoomsAroundBossRoom(dungeonMap);
|
||||
AddConnectionRooms(dungeonMap);
|
||||
ConnectAllAdjacentRooms(dungeonMap);
|
||||
|
||||
return dungeonMap;
|
||||
}
|
||||
|
||||
private void ConnectAllAdjacentRooms(DungeonMap dungeon)
|
||||
{
|
||||
Dictionary<Point, int> pointRoomMapping = dungeon.GetPointRoomIdMapping();
|
||||
foreach (var room in dungeon.GetAllRooms())
|
||||
{
|
||||
foreach ((Point p, RoomSide side) in room.GetAdjacentPoints())
|
||||
{
|
||||
if (pointRoomMapping.ContainsKey(p))
|
||||
{
|
||||
int idForRoomPointIsIn = pointRoomMapping[p];
|
||||
room.AddAdjacentRoomId(idForRoomPointIsIn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Point> GetAdjacentPoints(Room room)
|
||||
{
|
||||
var adjacentPoints = new List<Point>();
|
||||
|
||||
int x = room.PositionOfTopLeft.X;
|
||||
int y = room.PositionOfTopLeft.Y;
|
||||
int width = room.Width;
|
||||
int height = room.Height;
|
||||
|
||||
// Add points to the left and right of the room
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
adjacentPoints.Add(new Point(x - 1, y + i)); // Left side
|
||||
adjacentPoints.Add(new Point(x + width, y + i)); // Right side
|
||||
}
|
||||
|
||||
// Add points above and below the room
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
adjacentPoints.Add(new Point(x + i, y - 1)); // Top side
|
||||
adjacentPoints.Add(new Point(x + i, y + height)); // Bottom side
|
||||
}
|
||||
|
||||
return adjacentPoints;
|
||||
}
|
||||
|
||||
private void AddConnectionRooms(DungeonMap dungeon)
|
||||
{
|
||||
// For each room adjacent to a monster and boos room connect it with the closest thing (entrance room, or monster adjacent room)
|
||||
|
|
@ -139,7 +182,7 @@ namespace DungeonMapGenerator
|
|||
Room newRoom = CreateAdjacentRoom(RoomType.Normal, unoccupiedPointsOnSide, side, dungeon.GetOccupiedPoints());
|
||||
if (newRoom != null)
|
||||
{
|
||||
room.AddAdjacentRoom(newRoom, side);
|
||||
room.AddAdjacentRoomBySide(newRoom, side);
|
||||
dungeon.AddRoom(newRoom);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@ namespace DungeonMapGenerator
|
|||
public class DungeonMap
|
||||
{
|
||||
[JsonProperty("Width")]
|
||||
private int _width;
|
||||
public int Width;
|
||||
[JsonProperty("Height")]
|
||||
private int _height;
|
||||
public int Height;
|
||||
[JsonProperty("MonsterRooms")]
|
||||
private List<Room> _monsterRooms = new List<Room>();
|
||||
[JsonProperty("EntranceRooms")]
|
||||
|
|
@ -22,18 +22,42 @@ namespace DungeonMapGenerator
|
|||
private Room _bossRoom;
|
||||
private HashSet<Point> _unoccupiedPoints = new HashSet<Point>();
|
||||
private HashSet<Point> _occupiedPoints = new HashSet<Point>();
|
||||
|
||||
public DungeonMap(){ }
|
||||
|
||||
public DungeonMap(int width, int height)
|
||||
{
|
||||
this._width = width;
|
||||
_height = height;
|
||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, this._width)
|
||||
.SelectMany(x => Enumerable.Range(0, _height)
|
||||
this.Width = width;
|
||||
Height = height;
|
||||
_unoccupiedPoints.AddRange(Enumerable.Range(0, this.Width)
|
||||
.SelectMany(x => Enumerable.Range(0, Height)
|
||||
.Select(y => new Point(x,y))));
|
||||
}
|
||||
|
||||
public Dictionary<Point, int> GetPointRoomIdMapping()
|
||||
{
|
||||
var pointRoomMap = new Dictionary<Point, int>();
|
||||
|
||||
foreach (var room in GetAllRooms())
|
||||
{
|
||||
foreach (var point in room.GetPointsInRoom())
|
||||
{
|
||||
pointRoomMap[point] = room.Id;
|
||||
}
|
||||
}
|
||||
|
||||
return pointRoomMap;
|
||||
}
|
||||
|
||||
public List<Room> GetAllRooms()
|
||||
{
|
||||
List<Room> allRooms = new List<Room>();
|
||||
allRooms.AddRange(_monsterRooms);
|
||||
allRooms.AddRange(_normalRooms);
|
||||
allRooms.AddRange(_entranceRooms);
|
||||
allRooms.Add(_bossRoom);
|
||||
return allRooms;
|
||||
}
|
||||
|
||||
public void AddRoom(Room room)
|
||||
{
|
||||
switch (room.TypeOfRoom)
|
||||
|
|
@ -114,10 +138,10 @@ namespace DungeonMapGenerator
|
|||
List<Point> monsterRoomPoints = _monsterRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
|
||||
List<Point> entranceRoomPoints = _entranceRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
|
||||
List<Point> normalRoomPoints = _normalRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
|
||||
char[,] mapMatrix = new Char[_width, _height];
|
||||
for (int x = 0; x < _width; x++)
|
||||
char[,] mapMatrix = new Char[Width, Height];
|
||||
for (int x = 0; x < Width; x++)
|
||||
{
|
||||
for (int y = 0; y < _height; y++)
|
||||
for (int y = 0; y < Height; y++)
|
||||
{
|
||||
Point point = new Point(x, y);
|
||||
if (entranceRoomPoints.Contains(point))
|
||||
|
|
@ -154,8 +178,8 @@ namespace DungeonMapGenerator
|
|||
}
|
||||
|
||||
return $"{DateTime.Now}{Environment.NewLine}" +
|
||||
$"X Length: {_width}{Environment.NewLine}" +
|
||||
$"Y Length: {_height}{Environment.NewLine}" +
|
||||
$"X Length: {Width}{Environment.NewLine}" +
|
||||
$"Y Length: {Height}{Environment.NewLine}" +
|
||||
$"Monster Rooms: {_monsterRooms.Count}{Environment.NewLine}" +
|
||||
sb.ToString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,20 +24,41 @@ namespace DungeonMapGenerator
|
|||
|
||||
public class Room
|
||||
{
|
||||
private static int _nextId = 1;
|
||||
|
||||
public RoomType TypeOfRoom { get; set; }
|
||||
public int Height { get; set; }
|
||||
public int Width { get; set; }
|
||||
public Point PositionOfTopLeft { get; set; }
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonProperty("AdjacentRoomIds")]
|
||||
private HashSet<int> _adjacentRoomIds = new HashSet<int>();
|
||||
private readonly Dictionary<RoomSide, List<Room>> _adjacentRoomsBySide = new Dictionary<RoomSide, List<Room>>();
|
||||
|
||||
public Room()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Room(RoomType roomType, int width, int height, Point positionOfTopLeft)
|
||||
{
|
||||
Id = _nextId++;
|
||||
TypeOfRoom = roomType;
|
||||
Width = width;
|
||||
Height = height;
|
||||
PositionOfTopLeft = positionOfTopLeft;
|
||||
}
|
||||
|
||||
private readonly Dictionary<RoomSide, List<Room>> _adjacentRooms = new Dictionary<RoomSide, List<Room>>();
|
||||
public void AddAdjacentRoomId(int id)
|
||||
{
|
||||
_adjacentRoomIds.Add(id);
|
||||
}
|
||||
|
||||
public RoomType TypeOfRoom { get; set; }
|
||||
public int Height { get; set; }
|
||||
public int Width { get; set; }
|
||||
public Point PositionOfTopLeft { get; set; }
|
||||
public HashSet<int> GetAdjacentRoomIds()
|
||||
{
|
||||
return new HashSet<int>(_adjacentRoomIds);
|
||||
}
|
||||
|
||||
public List<Point> GetPointsInRoom()
|
||||
{
|
||||
|
|
@ -70,29 +91,55 @@ namespace DungeonMapGenerator
|
|||
return new Point(PositionOfTopLeft.X + Width / 2, PositionOfTopLeft.Y + Height / 2);
|
||||
}
|
||||
|
||||
public void AddAdjacentRoom(Room room, RoomSide side)
|
||||
public void AddAdjacentRoomBySide(Room room, RoomSide side)
|
||||
{
|
||||
if (!_adjacentRooms.ContainsKey(side))
|
||||
if (!_adjacentRoomsBySide.ContainsKey(side))
|
||||
{
|
||||
_adjacentRooms[side] = new List<Room>();
|
||||
_adjacentRoomsBySide[side] = new List<Room>();
|
||||
}
|
||||
|
||||
// Prevent duplicates
|
||||
if (!_adjacentRooms[side].Contains(room))
|
||||
if (!_adjacentRoomsBySide[side].Contains(room))
|
||||
{
|
||||
_adjacentRooms[side].Add(room);
|
||||
room.AddAdjacentRoom(this, GetOppositeSide(side)); // Ensure bidirectional linkage
|
||||
_adjacentRoomsBySide[side].Add(room);
|
||||
room.AddAdjacentRoomBySide(this, GetOppositeSide(side)); // Ensure bidirectional linkage
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Room> GetAdjacentRooms()
|
||||
{
|
||||
return _adjacentRooms.Values.SelectMany(roomList => roomList);
|
||||
return _adjacentRoomsBySide.Values.SelectMany(roomList => roomList);
|
||||
}
|
||||
|
||||
public Dictionary<RoomSide, List<Room>> GetAdjacentRoomsDict()
|
||||
{
|
||||
return new Dictionary<RoomSide, List<Room>>(_adjacentRooms);
|
||||
return new Dictionary<RoomSide, List<Room>>(_adjacentRoomsBySide);
|
||||
}
|
||||
|
||||
public List<(Point, RoomSide)> GetAdjacentPoints()
|
||||
{
|
||||
var adjacentPoints = new List<(Point, RoomSide)>();
|
||||
|
||||
int x = PositionOfTopLeft.X;
|
||||
int y = PositionOfTopLeft.Y;
|
||||
int width = Width;
|
||||
int height = Height;
|
||||
|
||||
// Add points to the left and right of the room
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
adjacentPoints.Add((new Point(x - 1, y + i), RoomSide.Left));
|
||||
adjacentPoints.Add((new Point(x + width, y + i), RoomSide.Right));
|
||||
}
|
||||
|
||||
// Add points above and below the room
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
adjacentPoints.Add((new Point(x + i, y - 1), RoomSide.Top));
|
||||
adjacentPoints.Add((new Point(x + i, y + height), RoomSide.Bottom));
|
||||
}
|
||||
|
||||
return adjacentPoints;
|
||||
}
|
||||
|
||||
// Helper method to get the opposite side
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue