コード例 #1
0
        /// <summary>
        ///   Pathfinding logic to get this actor to the specified position.
        /// </summary>
        /// <param name = "newPosition">The new position to move to.</param>
        private void MoveToPosition(MovementDestination <Point> newPosition)
        {
            PathFinder <Point> pathFinder = Global.PathFinder;

            if (newPosition.PointToMoveTo == _position && newPosition.MovementDestinationType == MovementDestinationType.SinglePoint)
            {
                Directions.Clear();
            }
            else if (newPosition.MovementDestinationType == MovementDestinationType.MultiPoint && newPosition.PointsAcceptable.Contains(_position))
            {
                Directions.Clear();
            }
            else
            {
                PathfindRequest <Point> newrequest = null;
                switch (newPosition.MovementDestinationType)
                {
                case MovementDestinationType.SinglePoint:
                    newrequest = new PathfindRequest <Point>(_position, newPosition.PointToMoveTo, this);
                    break;

                case MovementDestinationType.MultiPoint:
                    newrequest = new PathfindRequest <Point>(_position, this, newPosition.PointsAcceptable);
                    break;

                default:
                    throw new Exception("Can not move to an unaccessable position.");
                }
                pathFinder.NewSearch(newrequest);
                if (pathFinder.SearchStep(999) == SearchState.SearchStateSucceeded)
                {
                    PathfindAnswer theAnswer = pathFinder.FinalResult();
                    Directions = theAnswer.Directions;
                }
                else
                {
                    throw new Exception("Unable to path to that position.");
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Start a new search.
 /// </summary>
 /// <param name="request">Contains all the necessary information to perform the search by.</param>
 public void NewSearch(PathfindRequest <T> request)
 {
     ClearCollections();
     _startNode            = new Node <T>(request.start);
     _startNode.IsRootNode = true;
     _singlePointSolution  = request.SinglePointSolution;
     if (_singlePointSolution)
     {
         _goalNode = new Node <T>(request.end);
     }
     else
     {
         _goalNode = new Node <T>(request.possibleSolutions[0]);
     }
     _startNode.H = GoalDistanceEstimate(request.start, request.end);
     _startNode.F = _startNode.H + _startNode.G;
     // Add the start node into the open list to begin the search
     PushHeap(_startNode);
     _stepCount   = 0;
     _searchState = SearchState.SearchStateSearching;
     _traceManager.WriteLine("New Request - " + request, "path");
     _possibleSolutions = request.possibleSolutions;
 }