Implemented bi-directional linking between rooms
This commit is contained in:
parent
e0c65bd66e
commit
eb7c43a4f1
2 changed files with 30 additions and 47 deletions
|
|
@ -46,14 +46,6 @@ namespace DungeonGenerator
|
|||
return dungeonMap;
|
||||
}
|
||||
|
||||
enum RoomSide
|
||||
{
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
|
||||
private void AddNormalRoomsAroundMonsterRooms(DungeonMap dungeon)
|
||||
{
|
||||
HashSet<Point> unoccupiedPoints = dungeon.GetUnoccupiedPoints();
|
||||
|
|
@ -130,6 +122,7 @@ namespace DungeonGenerator
|
|||
Room newRoom = CreateAdjacentRoom(RoomType.Normal, unoccupiedPointsOnSide, side);
|
||||
if (newRoom != null)
|
||||
{
|
||||
monsterRoom.AddAdjacentRoom(newRoom, side);
|
||||
newRooms.Add(newRoom);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,17 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
namespace DungeonGenerator
|
||||
{
|
||||
public enum RoomSide
|
||||
{
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
public enum RoomType
|
||||
{
|
||||
Normal,
|
||||
|
|
@ -22,10 +30,7 @@ namespace DungeonGenerator
|
|||
PositionOfTopLeft = positionOfTopLeft;
|
||||
}
|
||||
|
||||
private List<Room> _topAdjacentRooms = new List<Room>();
|
||||
private List<Room> _bottomAdjacentRooms = new List<Room>();
|
||||
private List<Room> _leftAdjacentRooms = new List<Room>();
|
||||
private List<Room> _rightAdjacentRooms = new List<Room>();
|
||||
private readonly Dictionary<RoomSide, List<Room>> _adjacentRooms = new();
|
||||
|
||||
public RoomType TypeOfRoom { get; set; }
|
||||
public int Height { get; set; }
|
||||
|
|
@ -58,44 +63,29 @@ namespace DungeonGenerator
|
|||
return new Point(PositionOfTopLeft.X + Width / 2, PositionOfTopLeft.Y + Height / 2);
|
||||
}
|
||||
|
||||
public void AddTopAdjacentRoom(Room room)
|
||||
public void AddAdjacentRoom(Room room, RoomSide side)
|
||||
{
|
||||
_topAdjacentRooms.Add(room);
|
||||
if (!_adjacentRooms.ContainsKey(side))
|
||||
{
|
||||
_adjacentRooms[side] = new List<Room>();
|
||||
}
|
||||
|
||||
public List<Room> GetTopAdjaceRooms()
|
||||
// Prevent duplicates
|
||||
if (!_adjacentRooms[side].Contains(room))
|
||||
{
|
||||
return _topAdjacentRooms;
|
||||
_adjacentRooms[side].Add(room);
|
||||
room.AddAdjacentRoom(this, GetOppositeSide(side)); // Ensure bidirectional linkage
|
||||
}
|
||||
}
|
||||
|
||||
public void AddBottomAdjacentRoom(Room room)
|
||||
// Helper method to get the opposite side
|
||||
private RoomSide GetOppositeSide(RoomSide side) => side switch
|
||||
{
|
||||
_bottomAdjacentRooms.Add(room);
|
||||
}
|
||||
|
||||
public List<Room> GetBottomAdjaceRooms()
|
||||
{
|
||||
return _bottomAdjacentRooms;
|
||||
}
|
||||
|
||||
public void AddLeftAdjacentRoom(Room room)
|
||||
{
|
||||
_leftAdjacentRooms.Add(room);
|
||||
}
|
||||
|
||||
public List<Room> GetLeftAdjaceRooms()
|
||||
{
|
||||
return _leftAdjacentRooms;
|
||||
}
|
||||
|
||||
public void AddRightAdjacentRoom(Room room)
|
||||
{
|
||||
_rightAdjacentRooms.Add(room);
|
||||
}
|
||||
|
||||
public List<Room> GetRightAdjaceRooms()
|
||||
{
|
||||
return _rightAdjacentRooms;
|
||||
}
|
||||
RoomSide.Top => RoomSide.Bottom,
|
||||
RoomSide.Bottom => RoomSide.Top,
|
||||
RoomSide.Left => RoomSide.Right,
|
||||
RoomSide.Right => RoomSide.Left,
|
||||
_ => throw new ArgumentException("Invalid RoomSide", nameof(side))
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue