Пример #1
0
        public Vector2Int[] MarkDeadEnds()
        {
            m_modifiedPoints.Clear();

            int maxX = Grid.GetLength(0);
            int maxY = Grid.GetLength(1);

            for (int x = 0; x < maxX; ++x)
            {
                for (int y = 0; y < maxY; ++y)
                {
                    PathCell cell = Grid[x, y] as PathCell;

                    if (cell == null)
                    {
                        continue;
                    }

                    int validPaths = 4;

                    foreach (var direction in new Vector2Int[] { Vector2Int.up, Vector2Int.right, Vector2Int.down, Vector2Int.left })
                    {
                        int cX, cY;
                        cX = x + direction.x;
                        cY = y + direction.y;

                        if (0 <= cX && cX < maxX &&
                            0 <= cY && cY < maxY)
                        {
                            WallCell     wallCheck = Grid[cX, cY] as WallCell;
                            EdgeWallCell edgeCheck = Grid[cX, cY] as EdgeWallCell;

                            if (wallCheck != null || edgeCheck != null)
                            {
                                --validPaths;
                            }
                        }
                    }

                    if (validPaths == 1)
                    {
                        m_modifiedPoints.Add(new Vector2Int(x, y));
                        Grid[x, y] = new DeadEndCell(x, y);
                        m_deadEndsList.Add(Grid[x, y] as DeadEndCell);
                    }
                }
            }

            return(m_modifiedPoints.ToArray());
        }
Пример #2
0
        public Maze(Vector2Int aSize)
        {
            m_deadEndsList   = new List <DeadEndCell>();
            m_modifiedPoints = new List <Vector2Int>();

            m_size = aSize;
            int width  = aSize.x * 2 + 1;
            int height = aSize.y * 2 + 1;

            m_grid = new GridCell[width, height];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    if (x == 0 ||
                        x == width - 1 ||
                        y == 0 ||
                        y == height - 1)
                    {
                        if (x % 2 == 0 && y % 2 == 0)
                        {
                            Grid[x, y] = new EdgeCornerCell(x, y);
                        }
                        else
                        {
                            Grid[x, y] = new EdgeWallCell(x, y);
                        }
                    }
                    else if (x % 2 == 1 &&
                             y % 2 == 1)
                    {
                        Grid[x, y] = new PathCell(x, y);
                    }
                    else if (x % 2 == 0 &&
                             y % 2 == 0)
                    {
                        Grid[x, y] = new CornerCell(x, y);
                    }
                    else
                    {
                        m_grid[x, y] = new WallCell(x, y);
                    }
                }
            }
        }