Esempio n. 1
0
    public MazeGeneratorCell[,] GenerateMaze()
    {
        MazeGeneratorCell[,] maze = new MazeGeneratorCell [Width, Height];
        for (int w = 0; w < maze.GetLength(0); w++)
        {
            for (int l = 0; l < maze.GetLength(1); l++)
            {
                maze [w, l] = new MazeGeneratorCell(w, l);
            }
        }

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            maze[x, Height - 1].WallLeft = false;
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            maze[Width - 1, y].WallBottom = false;
        }



        RemoveWallsWithBackTracker(maze);
        PlaceMazeExit(maze);
        PlaceMazeDoors(maze);

        return(maze);
    }
Esempio n. 2
0
    private Vector2Int PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[0, 0];

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, Height - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[0, y];
            }
        }

        return(new Vector2Int(furthest.X, furthest.Y));
    }
Esempio n. 3
0
    public MazeGeneratorCell[,] GenerateMaze()
    {
        MazeGeneratorCell[,] maze = new MazeGeneratorCell[Width, Height];
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            for (int y = 0; y < maze.GetLength(1); y++)
            {
                maze[x, y] = new MazeGeneratorCell {
                    X = x, Y = y
                };
            }
        }

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            maze[x, Height - 1].WallLeft = false;
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            maze[Width - 1, y].WallBottom = false;
        }

        RemoveWallWithBacktracking(maze);

        PlaceMazeExit(maze);
        return(maze);
    }
Esempio n. 4
0
 private void RemoveWall(MazeGeneratorCell a, MazeGeneratorCell b)
 {
     if (a.X == b.X)
     {
         if (a.Y > b.Y)
         {
             a.WallBottom = false;
         }
         else
         {
             b.WallBottom = false;
         }
     }
     else
     {
         if (a.X > b.X)
         {
             a.WallLeft = false;
         }
         else
         {
             b.WallLeft = false;
         }
     }
 }
Esempio n. 5
0
    public Maze GenerateMaze()
    {
        MazeGeneratorCell[,] cells = new MazeGeneratorCell[Width, Height];

        for (int x = 0; x < cells.GetLength(0); x++)
        {
            for (int y = 0; y < cells.GetLength(1); y++)
            {
                cells[x, y] = new MazeGeneratorCell {
                    X = x, Y = y
                };
            }
        }

        for (int x = 0; x < cells.GetLength(0); x++)
        {
            cells[x, Height - 1].WallLeft = false;
        }

        for (int y = 0; y < cells.GetLength(1); y++)
        {
            cells[Width - 1, y].WallBottom = false;
        }

        RemoveWallsWithBacktracker(cells);

        Maze maze = new Maze();

        maze.cells          = cells;
        maze.finishPosition = PlaceMazeExit(cells);

        return(maze);
    }
    public Maze GenerateMaze(int w, int h)
    {
        MazeGeneratorCell[,] cells = new MazeGeneratorCell[w, h];

        for (int x = 0; x < cells.GetLength(0); x++)
        {
            for (int y = 0; y < cells.GetLength(1); y++)
            {
                cells[x, y] = new MazeGeneratorCell {
                    X = x, Y = y
                };
            }
        }

        //удаление боковых стен
        for (int x = 0; x < cells.GetLength(0); x++)
        {
            cells[x, h - 1].WallLeft = false;
        }
        //удаление боковых стен
        for (int y = 0; y < cells.GetLength(1); y++)
        {
            cells[w - 1, y].WallBottom = false;
        }

        RemoveWallsWithBacktracker(cells, w, h);

        Maze maze = new Maze();

        maze.cells          = cells;
        maze.finishPosition = PlaceMazeExit(cells, w, h);

        return(maze);
    }
Esempio n. 7
0
    private void PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[0, 0];

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Width - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze [x, Width - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze [x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze [Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze [0, y];
            }
        }

        furthest.MazeExit = true;
    }
Esempio n. 8
0
 private void RemoveWall(MazeGeneratorCell current, MazeGeneratorCell chozen)
 {
     if (current.X == chozen.X)
     {
         if (current.Y > chozen.Y)
         {
             current.WallBottom = false;
         }
         else
         {
             chozen.WallBottom = false;
         }
     }
     else
     {
         if (current.X > chozen.X)
         {
             current.WallLeft = false;
         }
         else
         {
             chozen.WallLeft = false;
         }
     }
 }
Esempio n. 9
0
    public MazeGeneratorCell[,] GenerateMaze()
    {
        MazeGeneratorCell[,] maze = new MazeGeneratorCell[Width, Height];

        // просто заполняем пустыми объектами
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            for (int y = 0; y < maze.GetLength(1); y++)
            {
                maze[x, y] = new MazeGeneratorCell {
                    X = x, Y = y
                };
            }
        }

        // выключение внешних стен
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            maze[x, Height - 1].WallLeft = false;
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            maze[Width - 1, y].WallBottom = false;
        }

        RemoveWallsWithBacktracker(maze);

        PlaceMazeExit(maze);

        return(maze);
    }
Esempio n. 10
0
    public MazeGeneratorCell[,] GenerateMaze()
    {
        MazeGeneratorCell[,] maze = new MazeGeneratorCell[Width, Height];
        for (int i = 0; i < maze.GetLength(0); i++)
        {
            for (int j = 0; j < maze.GetLength(1); j++)
            {
                maze[i, j] = new MazeGeneratorCell {
                    X = i, Y = j
                };
            }
        }
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            maze[x, Height - 1].LeftWall = false;
            maze[x, Height - 1].floor    = false;
        }
        for (int y = 0; y < maze.GetLength(1); y++)
        {
            maze[Width - 1, y].BottomWall = false;
            maze[Width - 1, y].floor      = false;
        }



        RemoveWallWithBacktracker(maze);
        SetPlaceExit(maze);

        return(maze);
    }
Esempio n. 11
0
 private void RemoveWall(MazeGeneratorCell a, MazeGeneratorCell b)
 {
     if (a.X == b.X)
     {
         if (a.Y > b.Y)
         {
             a.BottomWall = false;
         }
         else
         {
             b.BottomWall = false;
         }
     }
     else
     {
         if (a.X > b.X)
         {
             a.LeftWall = false;
         }
         else
         {
             b.LeftWall = false;
         }
     }
 }
Esempio n. 12
0
    private void PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[StartPointX, StartPointY];

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, Height - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[0, y];
            }
        }

        if (furthest.X == 0)
        {
            //furthest.WallLeft = false;
            furthest.Exit = true;
            Debug.Log("hi 1");
        }
        else if (furthest.Y == 0)
        {
            //furthest.WallBottom = false;
            furthest.Exit = true;
            Debug.Log("hi 2");
        }
        else if (furthest.X == Width - 2)
        {
            //maze[furthest.X + 1, furthest.Y].WallLeft = false;
            maze[furthest.X, furthest.Y].Exit = true;
            Debug.Log("hi 3");
        }
        else if (furthest.Y == Height - 2)
        {
            //maze[furthest.X, furthest.Y + 1].WallBottom = false;
            maze[furthest.X, furthest.Y].Exit = true;
            Debug.Log("hi 4");
        }
    }
Esempio n. 13
0
    private List <MazeGeneratorCell> GetPath(MazeGeneratorCell currentPoint, Dictionary <MazeGeneratorCell, DijkstraData> track)
    {
        var result = new List <MazeGeneratorCell>();
        int cost   = track[currentPoint].Price;

        while (currentPoint.X != -1 && currentPoint.Y != -1)
        {
            result.Add(currentPoint);
            currentPoint = track[currentPoint].Previous;
        }
        result.Reverse();
        return(result);
    }
Esempio n. 14
0
    private void RemoveWallsWithBacktracker(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[StartPointX, StartPointY];

        current.Start             = true;
        current.Visited           = true;
        current.DistanceFromStart = 0;

        Stack <MazeGeneratorCell> stack = new Stack <MazeGeneratorCell>();

        do
        {
            List <MazeGeneratorCell> unvisitedNeighbours = new List <MazeGeneratorCell>();

            int x = current.X;
            int y = current.Y;

            if (x > 0 && !maze[x - 1, y].Visited)
            {
                unvisitedNeighbours.Add(maze[x - 1, y]);
            }
            if (y > 0 && !maze[x, y - 1].Visited)
            {
                unvisitedNeighbours.Add(maze[x, y - 1]);
            }
            if (x < Width - 2 && !maze[x + 1, y].Visited)
            {
                unvisitedNeighbours.Add(maze[x + 1, y]);
            }
            if (y < Height - 2 && !maze[x, y + 1].Visited)
            {
                unvisitedNeighbours.Add(maze[x, y + 1]);
            }

            if (unvisitedNeighbours.Count > 0)
            {
                MazeGeneratorCell chosen = unvisitedNeighbours[UnityEngine.Random.Range(0, unvisitedNeighbours.Count)];
                RemoveWall(current, chosen);

                chosen.Visited = true;
                stack.Push(chosen);
                current = chosen;
                chosen.DistanceFromStart = stack.Count;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }
Esempio n. 15
0
    public IEnumerator moveObject(MazeGeneratorCell current, MazeGeneratorCell next)
    {
        float totalMovementTime   = 2f; //the amount of time you want the movement to take
        float currentMovementTime = 0f; //The amount of time that has passed
        var   Destination         = new Vector2(next.X * MazeGenerator.Shift, next.Y * MazeGenerator.Shift);
        var   Origin = new Vector2(current.X * MazeGenerator.Shift, current.Y * MazeGenerator.Shift);

        while (Vector2.Distance(transform.localPosition, Destination) > 0)
        {
            currentMovementTime    += Time.deltaTime;
            transform.localPosition = Vector3.Lerp(Origin, Destination, currentMovementTime / totalMovementTime);
            yield return(null);
        }
    }
Esempio n. 16
0
    //This method removes walls from MazeGeneratorCell array via "Backtracker algorithm"
    private void WallsBackTracker(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[0, 0];

        current.isVisited         = true;
        current.DistanceFromStart = 0;

        Stack <MazeGeneratorCell> stack = new Stack <MazeGeneratorCell>();

        do
        {
            List <MazeGeneratorCell> unvisitedCells = new List <MazeGeneratorCell>();

            int x = current.X;
            int y = current.Y;

            //Сheck neighboring cells
            if (x > 0 && !maze[x - 1, y].isVisited)
            {
                unvisitedCells.Add(maze[x - 1, y]);
            }
            if (y > 0 && !maze[x, y - 1].isVisited)
            {
                unvisitedCells.Add(maze[x, y - 1]);
            }
            if (x < Width - 2 && !maze[x + 1, y].isVisited)
            {
                unvisitedCells.Add(maze[x + 1, y]);
            }
            if (y < Height - 2 && !maze[x, y + 1].isVisited)
            {
                unvisitedCells.Add(maze[x, y + 1]);
            }

            if (unvisitedCells.Count > 0)
            {
                MazeGeneratorCell choosen = unvisitedCells[UnityEngine.Random.Range(0, unvisitedCells.Count)];
                RemoveWall(current, choosen);

                choosen.isVisited = true;
                stack.Push(choosen);
                choosen.DistanceFromStart = current.DistanceFromStart + 1;
                current = choosen;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }
Esempio n. 17
0
    //Placing exit from maze
    private Vector2Int PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[0, 0];

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, Height - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[0, y];
            }
        }

        if (furthest.X == 0)
        {
            furthest.WallLeft = false;
        }
        else if (furthest.Y == 0)
        {
            furthest.WallBottom = false;
        }
        else if (furthest.X == Width - 2)
        {
            maze[furthest.X + 1, furthest.Y].WallLeft = false;
        }
        else if (furthest.Y == Height - 2)
        {
            maze[furthest.X, furthest.Y + 1].WallBottom = false;
        }
        FinishFlag = GameObject.Find("finish");

        FinishFlag.transform.position = new Vector2(furthest.X + 0.5f, furthest.Y + 0.5f);
        return(new Vector2Int(furthest.X, furthest.Y));
    }
Esempio n. 18
0
    private MazeGeneratorCell GetOpeningPoint(HashSet <MazeGeneratorCell> canBeVisited, Dictionary <MazeGeneratorCell, DijkstraData> track)
    {
        var toOpen    = new MazeGeneratorCell(-1, -1);
        var bestPrice = double.PositiveInfinity;

        foreach (var point in canBeVisited)
        {
            if (track.ContainsKey(point) && track[point].Price < bestPrice)
            {
                bestPrice = track[point].Price;
                toOpen    = point;
            }
        }
        return(toOpen);
    }
Esempio n. 19
0
    private Vector2Int PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[0, 0];


        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, Height - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[0, y];
            }
        }

        if (furthest.X == 0)
        {
            furthest.WallLeft = false;
        }
        else if (furthest.Y == 0)
        {
            furthest.WallBottom = false;
        }
        else if (furthest.X == Width - 2)
        {
            maze[furthest.X + 1, furthest.Y].WallLeft = false;
        }
        else if (furthest.Y == Height - 2)
        {
            maze[furthest.X, furthest.Y + 1].WallBottom = false;
        }

        return(new Vector2Int(furthest.X, furthest.Y));
    }
Esempio n. 20
0
    private void SetPlaceExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell Exit = maze[0, 0];

        for (int i = 0; i < maze.GetLength(0); i++)
        {
            for (int j = 0; j < maze.GetLength(1); j++)
            {
                if (Exit.DistanceFromStart < maze[i, j].DistanceFromStart)
                {
                    Exit = maze[i, j];
                }
            }
        }
        Exit.Exit = true;
    }
Esempio n. 21
0
    private void RemoveWallsWithBacktracker(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[0, 0];

        current.Visited = true;

        Stack <MazeGeneratorCell> stack = new Stack <MazeGeneratorCell>();

        do
        {
            List <MazeGeneratorCell> unvisited = new List <MazeGeneratorCell>();

            int x = current.X;
            int y = current.Y;

            if (x > 0 && !maze[x - 1, y].Visited)
            {
                unvisited.Add(maze[x - 1, y]);
            }
            if (y > 0 && !maze[x, y - 1].Visited)
            {
                unvisited.Add(maze[x, y - 1]);
            }
            if (x < (Width - 2) && !maze[x + 1, y].Visited)
            {
                unvisited.Add(maze[x + 1, y]);
            }
            if (y < (Height - 2) && !maze[x, y + 1].Visited)
            {
                unvisited.Add(maze[x, y + 1]);
            }

            if (unvisited.Count > 0)
            {
                MazeGeneratorCell chosen = unvisited[Random.Range(0, unvisited.Count)];
                RemoveWall(current, chosen);

                chosen.Visited = true;
                stack.Push(chosen);
                current = chosen;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }
Esempio n. 22
0
    //в данной функции описывается алгоритм генерации лабиринта, начиная с начальной точки мы двигаемся в случайном направлении которое еще не посещали и сносим стены
    //пока не зайдем в тупик, далее мы возвращаемся обратно по созданной нами дороге до места где можно строить лабиринт дальше, цикл заканчивается когда мы
    //возвращаемся в начальную точку
    public void RemoveWallWithBacktracker(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[0, 0];

        current.visited           = true;
        current.DistanceFromStart = 0;

        Stack <MazeGeneratorCell> stack = new Stack <MazeGeneratorCell>();

        do
        {
            List <MazeGeneratorCell> UnvisitedCell = new List <MazeGeneratorCell>();
            int x = current.X;
            int y = current.Y;

            if (x > 0 && !maze[x - 1, y].visited)
            {
                UnvisitedCell.Add(maze[x - 1, y]);
            }
            if (y > 0 && !maze[x, y - 1].visited)
            {
                UnvisitedCell.Add(maze[x, y - 1]);
            }
            if (x < Width - 2 && !maze[x + 1, y].visited)
            {
                UnvisitedCell.Add(maze[x + 1, y]);
            }
            if (y < Height - 2 && !maze[x, y + 1].visited)
            {
                UnvisitedCell.Add(maze[x, y + 1]);
            }

            if (UnvisitedCell.Count > 0)
            {
                MazeGeneratorCell choosen = UnvisitedCell[Random.Range(0, UnvisitedCell.Count)];
                RemoveWall(current, choosen);
                choosen.visited = true;
                stack.Push(choosen);
                choosen.DistanceFromStart = stack.Count;
                current = choosen;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }
Esempio n. 23
0
    // поиск самой далекой ячейки от старта
    private void PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell furthest = maze[0, 0];

        // поиск
        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, Height - 2];
            }
            if (maze[x, 0].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[Width - 2, y];
            }
            if (maze[0, y].DistanceFromStart > furthest.DistanceFromStart)
            {
                furthest = maze[0, y];
            }
        }

        // делаем выход
        if (furthest.X == 0)
        {
            furthest.WallLeft = false;
        }
        else if (furthest.Y == 0)
        {
            furthest.WallBottom = false;
        }
        else if (furthest.X == Width - 2)
        {
            maze[furthest.X + 1, furthest.Y].WallLeft = false;
        }
        else if (furthest.Y == Height - 2)
        {
            maze[furthest.X, furthest.Y + 1].WallBottom = false;
        }
    }
Esempio n. 24
0
    private void RemoveWallWithBacktracking(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell current = maze[0, 0];

        current.Visited = true;
        current.Delta   = 0;
        Stack <MazeGeneratorCell> stack = new Stack <MazeGeneratorCell>();

        do
        {
            int x = current.X;
            int y = current.Y;
            List <MazeGeneratorCell> unvisitedNeighbours = new List <MazeGeneratorCell>();
            if (x > 0 && !maze[x - 1, y].Visited)
            {
                unvisitedNeighbours.Add(maze[x - 1, y]);
            }
            if (x < Width - 2 && !maze[x + 1, y].Visited)
            {
                unvisitedNeighbours.Add(maze[x + 1, y]);
            }
            if (y > 0 && !maze[x, y - 1].Visited)
            {
                unvisitedNeighbours.Add(maze[x, y - 1]);
            }
            if (y < Height - 2 && !maze[x, y + 1].Visited)
            {
                unvisitedNeighbours.Add(maze[x, y + 1]);
            }


            if (unvisitedNeighbours.Count > 0)
            {
                MazeGeneratorCell chosen = unvisitedNeighbours[Random.Range(0, unvisitedNeighbours.Count)];
                RemoveWall(current, chosen);

                chosen.Visited = true;
                stack.Push(chosen);
                current      = chosen;
                chosen.Delta = stack.Count;
            }
            else
            {
                current = stack.Pop();
            }
        } while (stack.Count > 0);
    }
Esempio n. 25
0
    private void PlaceMazeExit(MazeGeneratorCell[,] maze)
    {
        MazeGeneratorCell futher = maze[0, 0];

        for (int x = 0; x < maze.GetLength(0); x++)
        {
            if (maze[x, Height - 2].Delta > futher.Delta)
            {
                futher = maze[x, Height - 2];
            }
            if (maze[x, 0].Delta > futher.Delta)
            {
                futher = maze[x, 0];
            }
        }

        for (int y = 0; y < maze.GetLength(1); y++)
        {
            if (maze[Width - 2, y].Delta > futher.Delta)
            {
                futher = maze[Width - 2, y];
            }
            if (maze[0, y].Delta > futher.Delta)
            {
                futher = maze[0, y];
            }
        }

        if (futher.X == 0)
        {
            futher.WallLeft = false;
        }
        else if (futher.Y == 0)
        {
            futher.WallBottom = false;
        }
        else if (futher.X == Width - 2)
        {
            maze[futher.X + 1, futher.Y].WallLeft = false;
        }
        else if (futher.Y == Height - 2)
        {
            maze[futher.X, futher.Y + 1].WallBottom = false;
        }
    }
Esempio n. 26
0
    private MazeGeneratorCell GetCellCheckWalls(MazeGeneratorCell current, int x, int y)
    {
        var possibleCell = new MazeGeneratorCell(current.X + x, current.Y + y);

        if (InsideMaze(possibleCell))
        {
            var nextCell = MazeSpawner.Maze[current.X + x, current.Y + y];
            if (x < 0 && !current.WallLeft || x > 0 && !nextCell.WallLeft)
            {
                return(nextCell);
            }
            if (y < 0 && !current.WallBottom || y > 0 && !nextCell.WallBottom)
            {
                return(nextCell);
            }
        }
        return(null);
    }
Esempio n. 27
0
    private string CreatePortal(string Type, MazeGeneratorCell current, MazeGeneratorCell chozen)
    {
        if (current.X == chozen.X)
        {
            if (current.Y > chozen.Y)
            {
                current.BarrierBottom    = true;
                current.BarrierBottomObj = Type;
                return(Type);
            }
            else
            {
                chozen.BarrierBottom    = true;
                chozen.BarrierBottomObj = Type;
                return(Type);
            }
        }
        else
        {
            if (current.X > chozen.X)
            {
                current.WallLeft       = true;
                current.BarrierLeftObj = Type;
                return(Type);
            }
            else
            {
                chozen.BarrierLeft    = true;
                chozen.BarrierLeftObj = Type;
                return(Type);
            }
        }


        // if (current.X == chozen.X){
        //     if (current.Y > chozen.Y) current.WallBottom = false;
        //     else chozen.WallBottom = false;
        // }
        // else{
        //     if (current.X > chozen.X) current.WallLeft = false;
        //     else chozen.WallLeft = false;
        // }
    }
Esempio n. 28
0
    public void DrawPath()
    {
        Maze           maze      = MazeSpawner.maze;
        int            x         = maze.finishPos.x;
        int            y         = maze.finishPos.y;
        List <Vector3> positions = new List <Vector3>();

        while ((x != 0 || y != 0) && positions.Count < 10000)
        {
            positions.Add(new Vector3(x * MazeSpawner.CellSize.x, y * MazeSpawner.CellSize.y, y * MazeSpawner.CellSize.z));

            MazeGeneratorCell currentCell = maze.cells[x, y];

            if (x > 0 &&
                !currentCell.WallLeft &&
                maze.cells[x - 1, y].DistanceFromStart < currentCell.DistanceFromStart)
            {
                x--;
            }
            else if (y > 0 &&
                     !currentCell.WallBottom &&
                     maze.cells[x, y - 1].DistanceFromStart < currentCell.DistanceFromStart)
            {
                y--;
            }
            else if (x < maze.cells.GetLength(0) - 1 &&
                     !maze.cells[x + 1, y].WallLeft &&
                     maze.cells[x + 1, y].DistanceFromStart < currentCell.DistanceFromStart)
            {
                x++;
            }
            else if (y < maze.cells.GetLength(1) - 1 &&
                     !maze.cells[x, y + 1].WallBottom &&
                     maze.cells[x, y + 1].DistanceFromStart < currentCell.DistanceFromStart)
            {
                y++;
            }
        }

        positions.Add(Vector3.zero);
        componentLineRenderer.positionCount = positions.Count;
        componentLineRenderer.SetPositions(positions.ToArray());
    }
Esempio n. 29
0
    public IEnumerable <List <MazeGeneratorCell> > GetPathsByDijkstra(MazeGeneratorCell start,
                                                                      IEnumerable <MazeGeneratorCell> targets)
    {
        var visited      = new HashSet <MazeGeneratorCell>();
        var canBeVisited = new HashSet <MazeGeneratorCell>();
        var track        = new Dictionary <MazeGeneratorCell, DijkstraData>();

        canBeVisited.Add(start);
        track.Add(start, new DijkstraData {
            Price = 0, Previous = new MazeGeneratorCell(-1, -1)
        });

        while (true)
        {
            var toOpen = GetOpeningPoint(canBeVisited, track);
            if (toOpen == new MazeGeneratorCell(-1, -1))
            {
                yield break;
            }
            if (targets.Contains(toOpen))
            {
                yield return(GetPath(toOpen, track));
            }
            foreach (var point in GetNextPoints(toOpen))
            {
                int currentPrice = track[toOpen].Price + 1;
                if (!track.ContainsKey(point) || track[point].Price > currentPrice)
                {
                    track.Add(point, new DijkstraData {
                        Previous = toOpen, Price = currentPrice
                    });
                }
                if (!visited.Contains(point))
                {
                    canBeVisited.Add(point);
                }
            }
            canBeVisited.Remove(toOpen);
            visited.Add(toOpen);
        }
    }
Esempio n. 30
0
 private IEnumerable <MazeGeneratorCell> GetNextPoints(MazeGeneratorCell current)
 {
     for (int x = -1; x <= 1; x++)
     {
         for (int y = -1; y <= 1; y++)
         {
             if (x == 0 && y == 0)
             {
                 continue;
             }
             if (x * y == 0)
             {
                 var nextCell = GetCellCheckWalls(current, x, y);
                 if (nextCell != null)
                 {
                     yield return(nextCell);
                 }
             }
         }
     }
 }