Ejemplo n.º 1
0
    public static IEnumerable <RectPoint> GenerateMazeWalls <TCell>(RectGrid <TCell> grid)
    {
        var walls = grid.CloneStructure <bool>();        //true indicates passable

        foreach (var point in walls)
        {
            walls[point] = point.GetColor3() == 0;

            //Debug.Log(point);
        }

        var wallList = new PointList <RectPoint>();

        var newMaizePoint = grid.Where(p => p.GetColor3() == 0).RandomItem();
        var inMaze        = new PointList <RectPoint> {
            newMaizePoint
        };

        var edges = grid.GetNeighbors(newMaizePoint);

        wallList.AddRange(edges);

        while (wallList.Any())
        {
            var randomWall = wallList.RandomItem();
            var faces      = GetEdgeFaces(randomWall).Where(grid.Contains);

            //At least one of the two faces must be in the maze
            if (faces.Any(point => !inMaze.Contains(point)))
            {
                newMaizePoint = faces.First(point => !inMaze.Contains(point));
                inMaze.Add(newMaizePoint);
                walls[randomWall] = true;

                yield return(randomWall);

                // Add all edges that are not passages
                edges = grid.GetNeighbors(newMaizePoint).Where(edge => !(walls[edge]));
                wallList.AddRange(edges);
            }
            else
            {
                wallList.Remove(randomWall);
            }
        }
    }