Exemplo n.º 1
0
    /// <summary>
    /// Takes a Point3 in data coordinates.
    /// </summary>
    public Pathfinding(MazeStructure maze, bool[, ,] walls, Point3 center)
    {
        // initialize member variables
        this.center = center;
        this.maze   = maze;
        //Debug.Log(center);
        data = new int[walls.GetLength(0), walls.GetLength(1), walls.GetLength(2)];
        data.Initialize();

        // create visited matrix
        bool[, ,] visited = new bool[walls.GetLength(0), walls.GetLength(1), walls.GetLength(2)];
        visited.Initialize();

        // initialize vars
        Queue <VisitPoint> toVisit = new Queue <VisitPoint>(walls.GetLength(0) * walls.GetLength(1));

        toVisit.Enqueue(new VisitPoint(center, 0));

        // visit every possible cell
        while (toVisit.Count > 0)
        {
            // visit the given cell
            int    value = toVisit.Peek().value;
            Point3 pos   = toVisit.Dequeue().pos;
            data[pos.x, pos.y, pos.z]    = value;
            visited[pos.x, pos.y, pos.z] = true;

            // add appropriate neighbors to toVisit
            Point3[] neighbors = Point3.Scramble(pos.neighbors(2));
            foreach (Point3 newPos in neighbors)
            {
                if (maze.ValidMove(pos, newPos) && (!visited[newPos.x, newPos.y, newPos.z]))
                {
                    toVisit.Enqueue(new VisitPoint(newPos, value + 1));
                }
            }
        }
    }