private bool spawnFailed = true; //if true when generation is 'finished', the level will be generated again //main method to generate the map void Start() { while (spawnFailed) //while loop used until a generation meets all criteria (main one being that every room is actually connected!) { try{ spawnFailed = false; //will be set to true in fatal circumstances (no direct line between rooms, coridoors unable to generate etc) SubLevel rootLevel = new SubLevel(levelWidth / 2f, levelHeight / 2f, levelWidth, levelHeight, minRoomSize, true, 0); SplitLevel(rootLevel); //recursive method to split the map into appropriately sized sections rootLevel.CreateRoom(); //recursive method to create rooms in the sections rootLevel.FindPairs(); //recursive method to find pairs of rooms rooms = new List <Room> (); groups = new List <RoomGroup> (); coridoorControllers = new List <CoridoorController> (); GetRooms(rootLevel); //returns all rooms SpawnRooms(); //spawns in rooms (including generating stats) GroupRooms(); //groups rooms together SpawnCoridoorsWithinGroups(); //spawns coridoors within each group SpawnCoridoorsBetweenGroups(); //connects each group with a coridoor } catch { spawnFailed = true; } if (spawnFailed) //Resetting, before trying again { for (int i = 0; i < roomList.childCount; i++) { Destroy(roomList.GetChild(i).gameObject); } for (int i = 0; i < coridoorList.childCount; i++) { Destroy(coridoorList.GetChild(i).gameObject); } while (roomList.childCount > 0) { roomList.GetChild(0).parent = null; } while (coridoorList.childCount > 0) { coridoorList.GetChild(0).parent = null; } tileController.ResetTiles(levelWidth, levelHeight); } } BuildCoridoors(); //builds the physical aspect of the coridoors; BuildRooms(); //builds the physical aspect of the rooms BuildNavMesh(); //builds a navmesh, used for AI movement }
//Recursive Method to find a SubLevels organic pair public void FindPairs() { if (child1 != null) { child1.FindPairs(); } if (child2 != null) { child2.FindPairs(); } if (IsLeaf()) { if (room.getPair() == null) { room.setPair(FindPair()); room.getPair().setPair(room); } } }