public void AddExit(SimpleDungeonRoom from, SimpleDungeonRoom to, Direction direction) { from.exits.Add(direction); switch (direction) { case Direction.East: to.exits.Add(Direction.West); break; case Direction.West: to.exits.Add(Direction.East); break; case Direction.South: to.exits.Add(Direction.North); break; case Direction.North: to.exits.Add(Direction.South); break; default: break; } }
private static Vector2 GetNeighborLocation(SimpleDungeonRoom startingRoom, Direction direction) { Vector2 neighborLocation = startingRoom.location; switch (direction) { case Direction.East: neighborLocation.x++; break; case Direction.West: neighborLocation.x--; break; case Direction.North: neighborLocation.y++; break; case Direction.South: neighborLocation.y--; break; default: break; } return neighborLocation; }
public List<SimpleDungeonRoom> GenerateDungeonLayout() { List<SimpleDungeonRoom> dungeonRooms = new List<SimpleDungeonRoom>(); SimpleDungeonRoom startingRoom = new SimpleDungeonRoom(); startingRoom.location = Vector2.zero; dungeonRooms.Add(startingRoom); //for each remaining room, add a room on to an existing room for (int i = 1; i < numRooms; i++) { SimpleDungeonRoom newRoom = new SimpleDungeonRoom(); bool roomPlaced = false; //just brute force it for now while (!roomPlaced) { //pick a random starting room int randomRoomIndex = UnityEngine.Random.Range(0, dungeonRooms.Count); SimpleDungeonRoom connectedRoom = dungeonRooms[randomRoomIndex]; //pick a random direction Direction randomDirection = GetRandomDirection(); Vector2 newRoomLocation = GetNeighborLocation(connectedRoom, randomDirection); SimpleDungeonRoom existingRoom = GetRoom(newRoomLocation, dungeonRooms); if (existingRoom == null) { //we found an empty spot, put the room here newRoom.location = newRoomLocation; AddExit(connectedRoom, newRoom, randomDirection); dungeonRooms.Add(newRoom); roomPlaced = true; } } } return dungeonRooms; }