private List <WorldTile> GetAdjacentRootTiles(int col, int row, WorldTileType tileType) { WorldTile rootTile = GetRootTile(col, row); List <WorldTile> rootTiles = rootTile.GetTileGroup(); // Debug.Log(" Looking for " + tileType + " adjacent to " + rootTile + " : "); // for ( int i = 0; i < rootTiles.Count; i++ ) // Debug.Log(" --> " + rootTiles[i]); List <WorldTile> adjacentRootTiles = new List <WorldTile>(); for (int i = 0; i < rootTiles.Count; i++) { WorldTile tile1 = TryGetRootTile(rootTiles[i].Col + 1, rootTiles[i].Row, tileType); if (tile1 != null && !rootTiles.Contains(tile1) && !adjacentRootTiles.Contains(tile1)) { adjacentRootTiles.Add(tile1); } // else // Debug.Log(" --- right tile is not added as adjacent"); WorldTile tile2 = TryGetRootTile(rootTiles[i].Col - 1, rootTiles[i].Row, tileType); if (tile2 != null && !rootTiles.Contains(tile2) && !adjacentRootTiles.Contains(tile2)) { adjacentRootTiles.Add(tile2); } // else // Debug.Log(" --- left tile is not added as adjacent : tile2=" + ( tile2 != null ).ToString() + ", rootTiles.Contains( tile2 )=" + rootTiles.Contains( tile2 ).ToString() + ", adjacentTiles.Contains( tile2 )=" + adjacentTiles.Contains( tile2 ) ); WorldTile tile3 = TryGetRootTile(rootTiles[i].Col, rootTiles[i].Row + 1, tileType); if (tile3 != null && !rootTiles.Contains(tile3) && !adjacentRootTiles.Contains(tile3)) { adjacentRootTiles.Add(tile3); } // else // Debug.Log(" --- up tile is not added as adjacent"); WorldTile tile4 = TryGetRootTile(rootTiles[i].Col, rootTiles[i].Row - 1, tileType); if (tile4 != null && !rootTiles.Contains(tile4) && !adjacentRootTiles.Contains(tile4)) { adjacentRootTiles.Add(tile4); } // else // Debug.Log(" --- down tile is not added as adjacent"); } return(adjacentRootTiles); }
private IEnumerator GenerateWorld_Prim(int randomSeed) { if (randomSeed != 0) { UnityEngine.Random.InitState(randomSeed); } // decide which room to start in WorldTile startRoom = GetPrimStartRoom(); // startRoom.SetColor( Color.green ); _listRooms.Clear(); _listRooms.Add(startRoom); // start with all walls adjacent to the random room List <WorldTile> adjacentRootWalls = GetAdjacentRootTiles(startRoom.Col, startRoom.Row, WorldTileType.Wall); _listWalls.AddRange(adjacentRootWalls); // color the initial set just for debug fun // for ( int i = 0; i < adjacentRootWalls.Count; i++ ) // { // adjacentRootWalls[i].SetColor( Color.yellow ); // // List<WorldTile> adjacentChildWalls = adjacentRootWalls[i].GetChildrenTiles(); // if ( adjacentChildWalls != null ) // { // for ( int n = 0; n < adjacentChildWalls.Count; n++ ) // adjacentChildWalls[n].SetColor( new Color( 1f, 1f, 0.5f ) ); // } // } // keep searching as long as we have walls in the queue int step = 0; while (_listWalls.Count > 0) { // decide which wall to process next WorldTile nextWall = GetPrimNextWall(); List <WorldTile> randomWallGroup = nextWall.GetTileGroup(); // find all rooms next to the wall List <WorldTile> adjacentRooms = GetAdjacentRootTiles(nextWall.Col, nextWall.Row, WorldTileType.Room); // check if we found more than one room if (adjacentRooms.Count >= 2) { // check if any of those rooms aren't yet part of the main path List <WorldTile> unvisitedRooms = new List <WorldTile>(); for (int i = 0; i < adjacentRooms.Count; i++) { if (!_listRooms.Contains(adjacentRooms[i])) { unvisitedRooms.Add(adjacentRooms[i]); } } // check if we have exactly one adjacent room that isn't part of the main path if (unvisitedRooms.Count == 1) { // open the wall for (int i = 0; i < randomWallGroup.Count; i++) { randomWallGroup[i].SetTileType(WorldTileType.WallOpen); } // add the room to the main path _listRooms.Add(unvisitedRooms[0]); // color the processed room for debugging // List<Tile> unvisitedGroup = unvisitedRooms[0].GetTileGroup(); // for ( int n = 0; n < unvisitedGroup.Count; n++ ) // { // float color = 1f - ( (float)step * 0.001f ); // unvisitedGroup[n].SetColor( new Color( 0f, color, 0f ) ); // } // and any newly discovered walls to the queue adjacentRootWalls.Clear(); adjacentRootWalls = GetAdjacentRootTiles(unvisitedRooms[0].Col, unvisitedRooms[0].Row, WorldTileType.Wall); for (int i = 0; i < adjacentRootWalls.Count; i++) { if (!_listWalls.Contains(adjacentRootWalls[i])) { _listWalls.Add(adjacentRootWalls[i]); } } } } // if ( nextWall.TileType == WorldTileType.Wall ) // { // for ( int i = 0; i < randomWallGroup.Count; i++ ) // randomWallGroup[i].SetColor( WorldTile.GetTileColor( WorldTileType.Pillar ) ); // } // we are done processing this wall, remove it from the queue _listWalls.Remove(nextWall); // color the walls we removed // for ( int i = 0; i < randomWallGroup.Count; i++ ) // { // if ( randomWallGroup[i].TileType == WorldTileType.Wall ) // randomWallGroup[i].SetColor( WorldTile.GetTileColor(WorldTileType.Pillar)); // } // frame limiting step++; int stepRate = Mathf.FloorToInt(_generationRate * (2 + ((float)_numCols / 16))); if (step % stepRate == 0) { yield return(new WaitForEndOfFrame()); } } }