// random /* * private void DoNextGenerationStep(List<MazeCell> activeCells) * { * int currentIndex = activeCells.Count - 1; * MazeCell currentCell = activeCells[currentIndex]; * MazeDirection direction = MazeDirections.RandomValue; * CellVector coordinates = currentCell.coordinates + direction.ToCellVector(); * if (ContainsCoordinates(coordinates)) * { * MazeCell neighbor = this.GetCellAt(coordinates); * if (neighbor == null) * { * neighbor = this.CreateCell(coordinates); * this.CreatePassage(currentCell, neighbor, direction); * activeCells.Add(neighbor); * } * else * { * this.CreateWall(currentCell, neighbor, direction); * activeCells.RemoveAt(currentIndex); * } * } * else * { * this.CreateWall(currentCell, null, direction); * activeCells.RemoveAt(currentIndex); * } * } */ // with connection check private void DoNextGenerationStep(List <MazeCell> activeCells) { //int currentIndex = activeCells.Count / 2; int currentIndex = activeCells.Count - 1; MazeCell currentCell = activeCells[currentIndex]; if (currentCell.IsFullyInitialized) { activeCells.RemoveAt(currentIndex); return; } MazeDirection direction = currentCell.RandomUninitializedDirection; CellVector coordinates = currentCell.coordinates + direction.ToCellVector(); if (ContainsCoordinates(coordinates)) { MazeCell neighbor = this.GetCellAt(coordinates); if (neighbor == null) { neighbor = this.CreateCell(coordinates); this.CreatePassage(currentCell, neighbor, direction); activeCells.Add(neighbor); } else { this.CreateWall(currentCell, neighbor, direction); // No longer remove the cell here. } } else { this.CreateWall(currentCell, null, direction); // No longer remove the cell here. } }
private void DoFirstGenerationStep(List <MazeCell> activeCells) { // TODO: create a new cell at the center of the maze CellVector middleCoordinates = new CellVector(0, 0); activeCells.Add(CreateCell(middleCoordinates)); }
public bool ContainsCoordinates(CellVector coordinate) { // TODO: implement this check if (coordinate.x == 0 && coordinate.z == 0) { return(false); } return(true); }
private MazeCell CreateCell(CellVector coordinates) { MazeCell newCell = Instantiate(this.CellPrefab) as MazeCell; cells[coordinates.x, coordinates.z] = newCell; newCell.name = "Maze Cell " + coordinates.x + ", " + coordinates.z; newCell.coordinates = coordinates; newCell.transform.parent = this.transform; newCell.transform.localPosition = new Vector3(coordinates.x - this.size.x * 0.5f + 0.5f, 0f, coordinates.z - this.size.z * 0.5f + 0.5f); return(newCell); }
private MazeCell CreateCell(CellVector coordinates) { MazeCell newCell = Instantiate(this.CellPrefab) as MazeCell; cells[coordinates.x, coordinates.z] = newCell; newCell.name = "Maze Cell " + coordinates.x + ", " + coordinates.z; newCell.coordinates = coordinates; // we create a hierarchy where each cell is a child of the gameobject with the GrowingTreeMaze script newCell.transform.parent = this.transform; newCell.transform.localPosition = new Vector3(coordinates.x - this.size.x * 0.5f + 0.5f, 0f, coordinates.z - this.size.z * 0.5f + 0.5f); return(newCell); }
public IEnumerator Generate() { // TODO: add a delay to pause after each cell generation WaitForSeconds delay = new WaitForSeconds(CreationStepDelay); this.cells = new MazeCell[this.size.x, this.size.z]; CellVector coordinates = this.RandomCoordinates; // TODO: implement the ContainsCoordinates check while (this.ContainsCoordinates(coordinates) && this.GetCellAt(coordinates) == null) { this.CreateCell(coordinates); coordinates += MazeDirections.RandomValue.ToCellVector(); yield return(delay); } yield return(null); }
private void DoNextGenerationStep(List <MazeCell> activeCells) { // TODO: implement the maze generation // 1st: get the index of the cell you want to work with, lets take the last one in the list int currentIndex = activeCells.Count - 1; MazeCell currentCell = activeCells[currentIndex]; // if the cell has already all walls and connections, we do not want to change it anymore if (currentCell.IsFullyInitialized) { // TODO: the cell should be removed from the list activeCells.RemoveAt(currentIndex); return; } // TODO: we need to find the next edge (north, east, south, or west), we want to connect. Change this to a random direction, // there is a convenient method for this defined in MazeCell MazeDirection direction = currentCell.RandomUninitializedDirection; //MazeDirection direction = MazeDirection.NORTH; // TODO: determine the new coordinates, have a look into the MazeDirections class on how to obtain CellVector from a direction CellVector coordinates = currentCell.coordinates + direction.ToCellVector(); // TODO: if the coordinates are valid, we might add a new cell, otherwise we create a wall (see functions below) if (ContainsCoordinates(coordinates)) { // TODO: fetch the cell, if its null, create a cell, add a passage (see functions below), and add the cell to the list; create a wall otherwise MazeCell neighbor = this.GetCellAt(coordinates); if (neighbor == null) { neighbor = CreateCell(coordinates); CreatePassage(currentCell, neighbor, direction); activeCells.Add(neighbor); } else { CreateWall(currentCell, neighbor, direction); } } else { // TODO: create a wall CreateWall(currentCell, null, direction); } }
public bool ContainsCoordinates(CellVector coordinate) { return(coordinate.x >= 0 && coordinate.x < this.size.x && coordinate.z >= 0 && coordinate.z < this.size.z); }
private MazeCell GetCellAt(CellVector coordinates) { return(this.cells[coordinates.x, coordinates.z]); }