public void GenerateRandomDataMaze(string nameMaze, int numberColumns, int numberRows) { // Initialize grid and list active tiles TileData[,] grid = new TileData[numberColumns, numberRows]; // Generate a temporary list of tiles List <TileData> lActiveTiles = new List <TileData>(); int numberRoom = 0; // Gets first tile in random coord IVector2 firstCoord = new IVector2(Random.Range(0, numberColumns), Random.Range(0, numberRows)); TileData firstTile = new TileData(firstCoord); //firstTile.Room = numberRoom; grid[firstCoord.Column, firstCoord.Row] = firstTile; lActiveTiles.Add(firstTile); // Generate the rest of the tiles until the list is empty while (lActiveTiles.Count > 0) { // Gets random index to select next tile int cIndex = Random.Range(0, lActiveTiles.Count - 1); TileData cTile = lActiveTiles[cIndex]; // Check if the current tile doesn't have all of the edges already defined (it already has boxed by 4 edges) if (!cTile.IsBoxed()) { // Get a random direction from the current tile MazeDirection randomDir = cTile.GetRandomUnboxedDirection(); // Gets the coords for the random direction + current tile (this way we can get the new tile) IVector2 newCoords = cTile.Coords + randomDir.ToIntVector2(); // Check if these coords are within the maze boundaries bool coordsInBoundaries = ((newCoords.Column >= 0) && (newCoords.Column < numberColumns) && (newCoords.Row >= 0) && (newCoords.Row < numberRows)); if (coordsInBoundaries) { // Gets the tile for this coords TileData nextTile = grid[newCoords.Column, newCoords.Row]; // This tile doesn't exist yet, create the cell and a passage between the current tile and this new one if (nextTile == null) { nextTile = new TileData(newCoords); // Add tile to the grid grid[newCoords.Column, newCoords.Row] = nextTile; // Add passage // Sets a passage for the random edge for the current tile cTile.SetEdge(randomDir, MazeCellEdge.TypeEdgeEnum.PASSAGE); // Sets the opposite for this random direction or edge for the next tile as a Door // For instance> if random dir for current tile was right, next tile has left as edge nextTile.SetEdge(randomDir.GetOpposite(), MazeCellEdge.TypeEdgeEnum.PASSAGE); // Both tiles are in the same room //nextTile.Room = numberRoom; // Add this nextTile to active tiles lActiveTiles.Add(nextTile); } else { // Tile exits create a wall // Sets a wall for the random edge for the current tile cTile.SetEdge(randomDir, MazeCellEdge.TypeEdgeEnum.WALL); } } else { // coords not in boundary, create a wall for the current cell // Sets a wall for the random edge for the current tile cTile.SetEdge(randomDir, MazeCellEdge.TypeEdgeEnum.WALL); } } else { // Remove the tile from the active tiles lActiveTiles.RemoveAt(cIndex); } } // Save in JSON Debug.Log("[MazeGenerator] Saving in JSON file "); MazeJSONData mazeData = new MazeJSONData(); mazeData.Columns = numberColumns; mazeData.Rows = numberRows; mazeData.NumberRooms = numberRoom + 1; mazeData.Name = nameMaze; //mazeData.StartLocation = new IVector2(); //mazeData.EndLocation = new IVector2(); for (int iCol = 0; iCol < numberColumns; ++iCol) { for (int iRow = 0; iRow < numberRows; ++iRow) { mazeData.AddTileData(grid[iCol, iRow]); /*Debug.Log("Tile(" + iCol + "x" + iRow + ") - Coord ( " + grid[iCol, iRow].Coords.Column + "," + grid[iCol, iRow].Coords.Row + ")"); * for (int iEdge = 0; iEdge < grid[iCol, iRow].Edges.Length; iEdge++) * { * Debug.Log("Edge " + ((MazeDirection)iEdge).ToString() + ":" + grid[iCol, iRow].Edges[iEdge].ToString()); * }*/ } } Debug.Log("Saving " + mazeData.Name + " to JSON..."); MazeJSONTool.SaveMazeOnFile(mazeData); if (OnEndGenerateJSONMaze != null) { OnEndGenerateJSONMaze(""); } }
public void GenerateDataMazeByPath(string nameMaze, int numberColumns, int numberRows, List <IVector2> PathCoords) { // Initialize grid and list active tiles TileData[,] grid = new TileData[numberColumns, numberRows]; // Set all tiles to invisible for (int iCol = 0; iCol < numberColumns; ++iCol) { for (int iRow = 0; iRow < numberRows; ++iRow) { IVector2 currentCoords = new IVector2(iCol, iRow); TileData cTile = new TileData(currentCoords); cTile.TileType = MazeCell.ETypeCell.INVISIBLE; // Check if this tile is in coords for (int iTile = 0; iTile < PathCoords.Count; iTile++) { if (currentCoords == PathCoords[iTile]) { cTile.TileType = MazeCell.ETypeCell.NONE; // Add walls for (int iEdge = 0; iEdge < MazeDirections.NumberDirections; iEdge++) { cTile.SetEdge((MazeDirection)iEdge, MazeCellEdge.TypeEdgeEnum.WALL); } break; } } //Add tile grid[iCol, iRow] = cTile; } } // Convertion between direction and a 2D Vector IVector2[] m_directions = { // UP new IVector2(0, 1), // RIGHT new IVector2(1, 0), // DOWN new IVector2(0, -1), // LEFT new IVector2(-1, 0) }; // Follow the path and open walls // Check if this tile is in coords for (int iTile = 0; iTile < (PathCoords.Count - 1); iTile++) { IVector2 currentCoords = PathCoords[iTile]; // Next tile int nextTile = iTile + 1; // Check if next tile is the last one, then next tile would be the first one if (nextTile >= PathCoords.Count) { break; //nextTile = 0; } IVector2 nextCoords = PathCoords[nextTile]; // Check coords // Same column (move up or down) MazeDirection moveDirection = MazeDirection.UP; bool findDir = false; if (currentCoords.Column == nextCoords.Column) { // Check if up or down if (currentCoords.Row < nextCoords.Row) { moveDirection = MazeDirection.UP; findDir = true; } else { moveDirection = MazeDirection.DOWN; findDir = true; } } else if (currentCoords.Row == nextCoords.Row) // Same row, move left or right { if (currentCoords.Column < nextCoords.Column) { moveDirection = MazeDirection.RIGHT; findDir = true; } else { moveDirection = MazeDirection.LEFT; findDir = true; } } if (findDir) { // Set new edge TileData cTile = grid[currentCoords.Column, currentCoords.Row]; cTile.SetEdge(moveDirection, MazeCellEdge.TypeEdgeEnum.PASSAGE); grid[currentCoords.Column, currentCoords.Row] = cTile; // Set next tile TileData nTile = grid[nextCoords.Column, nextCoords.Row]; nTile.SetEdge(moveDirection.GetOpposite(), MazeCellEdge.TypeEdgeEnum.PASSAGE); grid[nextCoords.Column, nextCoords.Row] = nTile; } } for (int iCol = 0; iCol < numberColumns; ++iCol) { for (int iRow = 0; iRow < numberRows; ++iRow) { Debug.LogFormat("Tile {0}x{1}", iCol, iRow); // Add walls for (int iEdge = 0; iEdge < MazeDirections.NumberDirections; iEdge++) { Debug.LogFormat("Edge {0} : {1}", (MazeDirection)iEdge, grid[iCol, iRow].Edges[iEdge].ToString()); } } } // Save in JSON Debug.Log("[MazeGenerator] Saving in JSON file "); MazeJSONData mazeData = new MazeJSONData(); mazeData.Columns = numberColumns; mazeData.Rows = numberRows; mazeData.NumberRooms = 0; mazeData.Name = nameMaze; //mazeData.StartLocation = new IVector2(); //mazeData.EndLocation = new IVector2(); for (int iCol = 0; iCol < numberColumns; ++iCol) { for (int iRow = 0; iRow < numberRows; ++iRow) { mazeData.AddTileData(grid[iCol, iRow]); /*Debug.Log("Tile(" + iCol + "x" + iRow + ") - Coord ( " + grid[iCol, iRow].Coords.Column + "," + grid[iCol, iRow].Coords.Row + ")"); * for (int iEdge = 0; iEdge < grid[iCol, iRow].Edges.Length; iEdge++) * { * Debug.Log("Edge " + ((MazeDirection)iEdge).ToString() + ":" + grid[iCol, iRow].Edges[iEdge].ToString()); * }*/ } } Debug.Log("Saving " + mazeData.Name + " to JSON..."); MazeJSONTool.SaveMazeOnFile(mazeData); if (OnEndGenerateJSONMaze != null) { OnEndGenerateJSONMaze(""); } }