private void ConnectOverlapSpawns(Room parent, RoomSpawn parentSpawn, HashSet <Room> collidedRooms) { Vector3 pos1 = parent.transform.position + parentSpawn.position; parent.CloseSpawn(parentSpawn, null); foreach (Room room in collidedRooms) { if (room == parent) { continue; } Vector3 pos2 = room.transform.position; bool connected = false; foreach (RoomSpawn spawn in room.spawns) { if (pos2 + spawn.position == pos1) { room.CloseSpawn(spawn, parent); parent.CloseSpawn(parentSpawn, room); parent.AddConnection(room); room.AddConnection(parent); break; } } if (connected) { break; } } }
private Room GetRndRoom(Dir dir, Room parent, RoomSpawn parentSpawn) { Room newRoom = null; List <Room> validRooms = new List <Room>(sortedRooms[dir]); HashSet <Room> collidedRooms = new HashSet <Room>(); while (validRooms.Count > 0) { int roomI = rng.Next(validRooms.Count); Room curr = validRooms[roomI]; validRooms.RemoveAt(roomI); Vector3 pos = parent.transform.position + parentSpawn.position; List <RoomSpawn> validSpawns = new List <RoomSpawn>(curr.sortedSpawns[dir]); bool succeded = false; RoomSpawn childSpawn = null; int spawnI = rng.Next(validSpawns.Count); int spawnIStart = spawnI; while (true) { childSpawn = validSpawns[spawnI]; Vector3 pos2 = pos - childSpawn.position; List <Room> hitRooms = RoomCollisionCheck(pos2, curr.bounds); foreach (Room hitRoom in hitRooms) { collidedRooms.Add(hitRoom); } if (hitRooms.Count == 0) { succeded = true; pos = pos2; break; } spawnI = spawnI == validSpawns.Count - 1 ? 0 : spawnI + 1; if (spawnI == spawnIStart) { break; } } if (succeded) { newRoom = Instantiate(curr, transform); newRoom.Init(); newRoom.transform.position = pos; newRoom.CloseSpawn(newRoom.sortedSpawns[dir][spawnI], parent); parent.CloseSpawn(parentSpawn, newRoom); break; } } if (newRoom == null) { ConnectOverlapSpawns(parent, parentSpawn, collidedRooms); } return(newRoom); }
public void Generate(int newSeed) { Clear(); Room start = Instantiate(startRoom, transform); start.Init(); seed = newSeed; rng = new System.Random(newSeed); int targetNumRooms = rng.Next(minRooms, maxRooms); List <Room> openRooms = new List <Room>(); spawnedRooms.Add(start); openRooms.Add(start); while (openRooms.Count > 0 && spawnedRooms.Count < targetNumRooms) { Room rndRoom = openRooms[rng.Next(openRooms.Count)]; if (rndRoom.openSpawns.Count == 0) { openRooms.Remove(rndRoom); continue; } RoomSpawn rndSpawn = rndRoom.openSpawns[rng.Next(rndRoom.openSpawns.Count)]; Dir dir = Utils.FlipDir(Utils.Vector3ToDir(rndSpawn.position)); Room newRoom = GetRndRoom(dir, rndRoom, rndSpawn); if (newRoom != null) { rndRoom.AddConnection(newRoom); newRoom.AddConnection(rndRoom); spawnedRooms.Add(newRoom); if (newRoom.openSpawns.Count > 0) { openRooms.Add(newRoom); } } } CalculateScores(); }
public void CloseSpawn(RoomSpawn spawn, Room connection) { spawn.Connect(connection); openSpawns.Remove(spawn); }