示例#1
0
    public void Generate()
    {
        for (int i = 0; i < size.x; i++)
        {
            for (int j = 0; j < size.z; j++)
            {
                UpdateVisitation(new IntVector2(i, j));
            }
        }

        List <Cell.Wall> walls = _foreCell.GetWalls(false).ToList();

        if (walls.Count == 0)
        {
            bool newForeCellFound = false;

            for (int i = 0; i < size.x; i++)
            {
                for (int j = 0; j < size.z; j++)
                {
                    Cell cell = _cells[i, j];

                    if (!cell.visited
                        &&
                        cell.GetWalls(true)
                        .Count(w => size.Contains(IntVector2.Zero, cell.coordinate + w.direction.GetUnitVector())) != 0)
                    {
                        SetForeCell(cell);

                        newForeCellFound = true;

                        break;
                    }
                }

                if (newForeCellFound)
                {
                    break;
                }
            }

            if (!newForeCellFound)
            {
                return;
            }

            _foreCell.visited = true;

            Cell.Wall[] visitedWalls = _foreCell.GetWalls(true).Where(w =>
                                                                      size.Contains(IntVector2.Zero, _foreCell.coordinate + w.direction.GetUnitVector())).ToArray();

            Cell.Wall visitedWall = visitedWalls[Random.Range(0, visitedWalls.Length)];

            IntVector2 visitedCellCoordinate = _foreCell.coordinate + visitedWall.direction.GetUnitVector();

            if (size.Contains(IntVector2.Zero, visitedCellCoordinate))
            {
                _foreCell.DisableWall(visitedWall.direction);

                _cells[visitedCellCoordinate.x, visitedCellCoordinate.z].DisableWall(visitedWall.direction.GetOpposite());
            }

            Generate();

            return;
        }

        Cell.Wall wall = walls[Random.Range(0, walls.Count)];

        Direction direction = wall.direction;

        IntVector2 coordinate = _foreCell.coordinate + direction.GetUnitVector();

        if (!size.Contains(IntVector2.Zero, coordinate))
        {
            wall.visited = true;

            Generate();

            return;
        }

        Cell adjacentCell = _cells[coordinate.x, coordinate.z];

        _foreCell.DisableWall(direction);

        SetForeCell(adjacentCell);

        _foreCell.DisableWall(direction.GetOpposite());

        Generate();
    }