Ejemplo n.º 1
0
        /// <summary>
        /// Returns a ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority
        /// This method is useful when there are multiple paths that would all work and we want to have some additional logic to pick one of the best paths
        /// The FindPath( int x, int y ) method in the GoalMap class uses this method and then chooses the first path.
        /// </summary>
        /// <param name="x">X location of the beginning of the path, starting with 0 as the farthest left</param>
        /// <param name="y">Y location of the beginning of the path, starting with 0 as the top</param>
        /// <returns>A ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority. Returns null if no path is found.</returns>
        public ReadOnlyCollection <Path> TryFindPaths(int x, int y)
        {
            if (_goals.Count < 1)
            {
                return(null);
            }

            if (!_map.IsWalkable(x, y))
            {
                return(null);
            }

            if (!_goals.Any(g => _map.IsWalkable(g.X, g.Y)))
            {
                return(null);
            }

            ComputeCellWeightsIfNeeded();
            var pathFinder = new GoalMapPathFinder(this);
            ReadOnlyCollection <Path> paths = pathFinder.FindPaths(x, y);

            if (paths.Count <= 1 && paths[0].Length <= 1)
            {
                return(null);
            }

            return(paths);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority
        /// This method is useful when there are multiple paths that would all work and we want to have some additional logic to pick one of the best paths
        /// The FindPath( int x, int y ) method in the GoalMap class uses this method and then chooses the first path.
        /// </summary>
        /// <exception cref="PathNotFoundException">Thrown when there is not a path from the Source x,y to any Goal or a Goal is not set</exception>
        /// <param name="x">X location of the beginning of the path, starting with 0 as the farthest left</param>
        /// <param name="y">Y location of the beginning of the path, starting with 0 as the top</param>
        /// <returns>A ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority</returns>
        public ReadOnlyCollection <Path> FindPaths(int x, int y)
        {
            if (_goals.Count < 1)
            {
                throw new PathNotFoundException("A goal must be set to find a path");
            }

            if (!_map.IsWalkable(x, y))
            {
                throw new PathNotFoundException($"Source ({x}, {y}) must be walkable to find a path");
            }

            if (!_goals.Any(g => _map.IsWalkable(g.X, g.Y)))
            {
                throw new PathNotFoundException("A goal must be walkable to find a path");
            }

            ComputeCellWeightsIfNeeded();
            var pathFinder = new GoalMapPathFinder(this);
            ReadOnlyCollection <Path> paths = pathFinder.FindPaths(x, y);

            if (paths.Count <= 1 && paths[0].Length <= 1)
            {
                throw new PathNotFoundException($"A path from Source ({x}, {y}) to any goal was not found");
            }

            return(paths);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a List of ordered Lists of Points representing all of the shortest paths from the specified location to all defined Goals
        /// </summary>
        /// <param name="x">X location of the beginning of the path, starting with 0 as the farthest left</param>
        /// <param name="y">Y location of the beginning of the path, starting with 0 as the top</param>
        /// <returns>A List of ordered Lists of Points representing all of the shortest paths from the specified location to all defined Goals</returns>
        public List <List <Point> > FindAllPathsToAllGoals(int x, int y)
        {
            ComputeCellWeightsIfNeeded();
            var pathFinder = new GoalMapPathFinder(this);

            return(pathFinder.FindPaths(x, y));
        }
Ejemplo n.º 4
0
        public ReadOnlyCollection <Path> FindPaths(Vector2Int position)
        {
            if (goals.Count < 1)
            {
                Debug.LogError("A goal must be set to find a path");
                return(null);
            }

            if (!map.IsWalkable(position))
            {
                Debug.LogError($"Source ({position.x}, {position.y}) must be walkable to find a path");
                return(null);
            }

            if (!goals.Any(g => map.IsWalkable(g.Position)))
            {
                Debug.LogError("A goal must be walkable to find a path");
                return(null);
            }

            ComputeCellWeightsIfNeeded();
            var pathFinder = new GoalMapPathFinder(this);
            ReadOnlyCollection <Path> paths = pathFinder.FindPaths(position);

            if (paths.Count <= 1 && paths[0].Length <= 1)
            {
                Debug.LogError($"A path from Source ({position.x}, {position.y}) to any goal was not found");
                return(null);
            }

            return(paths);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority
        /// This method is useful when there are multiple paths that would all work and we want to have some additional logic to pick one of the best paths
        /// The FindPath( int x, int y ) method in the GoalMap class uses this method and then chooses the first path.
        /// </summary>
        /// <exception cref="PathNotFoundException">Thrown when there is not a path from the Source x,y to any Goal or a Goal is not set</exception>
        /// <param name="x">X location of the beginning of the path, starting with 0 as the farthest left</param>
        /// <param name="y">Y location of the beginning of the path, starting with 0 as the top</param>
        /// <returns>A ReadOnlyCollection of Paths representing all of the shortest paths from the specified location to the Goal or Goals determined to have the highest priority</returns>
        public ReadOnlyCollection<Path> FindPaths( int x, int y )
        {
            if ( _goals.Count < 1 )
             {
            throw new PathNotFoundException( "A goal must be set to find a path" );
             }

             if ( !_map.IsWalkable( x, y ) )
             {
            throw new PathNotFoundException( string.Format( "Source ({0}, {1}) must be walkable to find a path", x, y ) );
             }

             if ( !_goals.Any( g => _map.IsWalkable( g.X, g.Y ) ) )
             {
            throw new PathNotFoundException( "A goal must be walkable to find a path" );
             }

             ComputeCellWeightsIfNeeded();
             var pathFinder = new GoalMapPathFinder( this );
             var paths = pathFinder.FindPaths( x, y );

             if ( paths.Count <= 1 && paths[0].Length <= 1 )
             {
            throw new PathNotFoundException( string.Format( "A path from Source ({0}, {1}) to any goal was not found", x, y ) );
             }

             return paths;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns a List of ordered Lists of Points representing all of the shortest paths from the specified location to all defined Goals
 /// </summary>
 /// <param name="x">X location of the beginning of the path, starting with 0 as the farthest left</param>
 /// <param name="y">Y location of the beginning of the path, starting with 0 as the top</param>
 /// <returns>A List of ordered Lists of Points representing all of the shortest paths from the specified location to all defined Goals</returns>
 public List<List<Point>> FindAllPathsToAllGoals( int x, int y )
 {
     ComputeCellWeightsIfNeeded();
      var pathFinder = new GoalMapPathFinder( this );
      return pathFinder.FindPaths( x, y );
 }