Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ConnectClosestRooms(List<Room> allRooms) {
- List<Connection> connections = new List<Connection> ();
- List<int> visitedRooms = new List<int> ();
- List<int> unvisitedRooms = new List<int> ();
- visitedRooms.Add (0);
- for (int i = 1; i < allRooms.Count; i++) {
- unvisitedRooms.Add (i);
- }
- // search until all rooms have been visited
- while (unvisitedRooms.Count > 0) {
- // Find best connection between visited and unvisited rooms
- Connection bestConnection = new Connection ();
- bestConnection.distance = int.MaxValue;
- for (int i = 0; i < visitedRooms.Count; i++) {
- for (int j = 0; j < unvisitedRooms.Count; j++) {
- int roomA = visitedRooms [i];
- int roomB = unvisitedRooms [j];
- for (int tileIndexA = 0; tileIndexA < allRooms [roomA].edgeTiles.Count; tileIndexA++) {
- for (int tileIndexB = 0; tileIndexB < allRooms [roomB].edgeTiles.Count; tileIndexB++) {
- int dX = allRooms [roomA].edgeTiles [tileIndexA].tileX - allRooms [roomB].edgeTiles [tileIndexB].tileX;
- int dY = allRooms [roomA].edgeTiles [tileIndexA].tileY - allRooms [roomB].edgeTiles [tileIndexB].tileY;
- int distanceBetweenRooms = dX * dX + dY * dY;
- if (distanceBetweenRooms < bestConnection.distance) {
- bestConnection = new Connection (roomA, tileIndexA, roomB, tileIndexB, distanceBetweenRooms);
- }
- }
- }
- }
- }
- connections.Add (bestConnection);
- visitedRooms.Add (bestConnection.roomIndexB);
- unvisitedRooms.Remove (bestConnection.roomIndexB);
- }
- // Create passages
- foreach (Connection connection in connections) {
- Coord tileA = allRooms [connection.roomIndexA].edgeTiles [connection.tileA];
- Coord tileB = allRooms [connection.roomIndexB].edgeTiles [connection.tileB];
- CreatePassage (allRooms [connection.roomIndexA], allRooms [connection.roomIndexB], tileA, tileB);
- }
- }
- struct Connection {
- public int roomIndexA;
- public int tileA;
- public int roomIndexB;
- public int tileB;
- public int distance;
- public Connection (int roomIndexA, int tileA, int roomIndexB, int tileB, int distance)
- {
- this.roomIndexA = roomIndexA;
- this.tileA = tileA;
- this.roomIndexB = roomIndexB;
- this.tileB = tileB;
- this.distance = distance;
- }
- }
Add Comment
Please, Sign In to add comment