Beispiel #1
0
    public Cell MakeConnection(Cell currentCell)
    {
        List <int> randomizedCellDirections = CellDirections.GetRandomizedCellDirections; // e.g [2, 1, 0, 3]

        for (int i = 0; i < randomizedCellDirections.Count; i++)
        {
            // The random direction from the current cell
            CellDirection direction = (CellDirection)randomizedCellDirections[i];

            // The neighbor cell location from the direction
            CellLocation nextLocation = currentCell.Location + direction.ToRelativeCellLocation();

            if (CanPlaceCell(nextLocation))
            {
                CellDirection fromDirection = direction.GetOpposite();
                Cell          nextCell      = PlaceCell(nextLocation, fromDirection);
                currentCell.AddConnection(direction); // Direction that connects it to the newly generated cell
                currentCell.name += "_" + direction;

                return(nextCell);
            }
        }

        return(null);
    }
    public IEnumerator GenerateBTA()
    {
        DateTime startTime = DateTime.Now;

        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                // print(String.Format("x: {0}, z: {1}", x, z));
                CellLocation current_location = new CellLocation(x, z);
                Cell         currentCell      = PlaceCell(current_location);

                if (x == 0 && z == 0)
                {
                    currentCell.Material = entryCellMaterial;
                    entryCell            = currentCell;
                }
                else if (x == height - 1 && z == width - 1)
                {
                    currentCell.Material = exitCellMaterial;
                    CellDirection exitCellDirection = GetDirectionThatLeadstoOutOfBound(currentCell);
                    currentCell.AddConnection(exitCellDirection);
                    exitCell = currentCell;
                }

                List <int> validDirections = new List <int>();
                if (x > 0)
                {
                    validDirections.Add(2);
                }
                if (z > 0)
                {
                    validDirections.Add(1);
                }

                if (validDirections.Count > 0)
                {
                    validDirections.Shuffle();
                    CellDirection selectedDirection = (CellDirection)validDirections[0];
                    CellLocation  neighbourLocation = currentCell.Location + selectedDirection.ToRelativeCellLocation();
                    // print(String.Format("nx: {0}, nz: {1}", neighbourLocation.x, neighbourLocation.z));
                    CellDirection fromDirection = selectedDirection.GetOpposite();
                    currentCell.AddConnection(selectedDirection);
                    cells[neighbourLocation.x, neighbourLocation.z].AddConnection(fromDirection);
                }

                currentLength++;
                cellDistances[currentCell.Location.x, currentCell.Location.z] = currentLength;
                activeCells.Add(currentCell);
                yield return(new WaitForSeconds(delay));
            }
        }

        CreateCellWalls();
        DateTime endTime   = DateTime.Now;
        String   totalTime = endTime.Subtract(startTime).ToString();

        print("Total time taken: " + totalTime);
        print("Generate complete");
    }