} = false; // Self explanatory boolean public static List <RoomInfo> GenerateNewLevel(LevelGenerationData _data) { if (IsGeneratingLevel) // If some script requested a new level whilst one was already being made { // Return to avoid errors | todo - Wait for the current level to finish instead of returning Debug.LogError("Someone tried to generate a level whilst one was already being generated!"); Debug.LogError("Returning null!"); return(null); } IsGeneratingLevel = true; CurrentLevelData = _data; LevelCrawler.ResetCrawledPositions(); // Make every position available CurrentLevel.Add(new RoomInfo("Starting Room", Vector2.zero, Vector2.zero, RoomType.StartingRoom)); // Add the starting room for (int i = 0; i < CurrentLevelData.numberOfCrawlers; i++) // Iterate over every crawler { int crawls; if (CurrentLevelData.minimumCrawls >= CurrentLevelData.maximumCrawls) // If the minimum is higher than the maximum, { crawls = CurrentLevelData.minimumCrawls; // Just assign the minimum } else // Else assign a random value between the min and max { crawls = Random.Range(CurrentLevelData.minimumCrawls, CurrentLevelData.maximumCrawls + 1); // The +1 is here because the second number is exclusive } LevelCrawler crawler; // Create the crawler if (i == 0) // If this is the first crawler, choose a random position adjacent to the starting room { crawler = new LevelCrawler(Vector2.zero); // Instantiate the crawler GenerateRoom(crawler, 0, 0); // Generate First Room } else // Instantiate the crawler at a random position that has been already visited { crawler = new LevelCrawler(CurrentLevel[Random.Range(0, CurrentLevel.Count)].Coordinates); if (GenerateRoom(crawler, i, 0)) // Generate the first room of this crawler { break; // True means that a error happened, so break out } } for (int j = 1; j < crawls; j++) // Iterate over every crawl of crawler 'i' { if (GenerateRoom(crawler, i, j)) { break; // True means that a error happened, so break out } } } foreach (var room in CurrentLevel) { room.UpdateNeighbours(); // After the crawlers are finished, update the Neighbours list (dictionary) of every room } IsGeneratingLevel = false; return(CurrentLevel); }
static bool GenerateRoom(LevelCrawler _crawler, int _crawlerID, int _crawlerCrawl) { if (_crawler.NewPosition() == Vector2.zero) // Moves the crawler to a new not-occupied position & If there is no free position, return; { Debug.LogWarning($"Crawler {_crawlerID} got himeself stuck, returning."); return(true); // True for 'a error happened' | todo - help the crawlers get unstuck } var newRoom = new RoomInfo($"Room {_crawlerID}-{_crawlerCrawl}", _crawler.Position, CalculateBigRoom(_crawler), RoomType.RegularRoom); CurrentLevel.Add(newRoom); // Add the new room to the List of current rooms if (newRoom.IsBigRoom) { CrawlBigRoom(newRoom, _crawler); // This method adds the subRooms to the crawled positions } return(false); // False for 'no errors occured' }