AStarPathfinder.FindPath C# (CSharp) Méthode

FindPath() public méthode

public FindPath ( Cell, startCell, Cell, goalCell, List map, bool targetCellMustBeFree ) : void
startCell Cell,
goalCell Cell,
map List
targetCellMustBeFree bool
Résultat void
    public void FindPath(Cell startCell, Cell goalCell, List<List<Cell>> map, bool targetCellMustBeFree)
    {
        _map = map.Select(a => a.ToArray()).ToArray(); ;
        _mapWidth = _map.GetLength(0);
        _mapHeight = _map.GetLength(1);

        _start = new Node((int)startCell.gridPosition.x, (int)startCell.gridPosition.y, 0, 0, 0, null, startCell);
        end = new Node((int)goalCell.gridPosition.x, (int)goalCell.gridPosition.y, 0, 0, 0, null, goalCell);
        _openList.Add(_start);
        bool keepSearching = true;
        bool pathExists = true;

        while ((keepSearching) && (pathExists))
        {
            Node currentNode = ExtractBestNodeFromOpenList();
            if (currentNode == null)
            {
                pathExists = false;
                break;
            }
            _closeList.Add(currentNode);
            if (NodeIsGoal(currentNode))
                keepSearching = false;
            else
            {
                if (targetCellMustBeFree)
                    FindValidFourNeighbours(currentNode);
                else
                    FindValidFourNeighboursIgnoreTargetCell(currentNode);

                foreach (Node neighbour in _neighbours)
                {
                    if (FindInCloseList(neighbour) != null)
                        continue;
                    Node inOpenList = FindInOpenList(neighbour);
                    if (inOpenList == null)
                    {
                        _openList.Add(neighbour);
                    }
                    else
                    {
                        if (neighbour.G < inOpenList.G)
                        {
                            inOpenList.G = neighbour.G;
                            inOpenList.MoveCost = inOpenList.G + inOpenList.H;
                            inOpenList.Parent = currentNode;
                        }
                    }
                }
            }
        }

        if (pathExists)
        {
            Node n = FindInCloseList(end);
            while (n != null)
            {
                FinalPath.Add(n);
                n = n.Parent;
            }
        }
    }

Usage Example

Exemple #1
0
        public void MoveToNode(IGraphNode node, int movementLimit = -1)
        {
            // Find the shortest route to the destination node, and start moving towards it.
            var pathfinder = new AStarPathfinder();

            path = pathfinder.FindPath(GetTravelingNode(), node);

            if (OnStartMove != null)
            {
                OnStartMove.Invoke();
            }

            if (movementLimit > -1 && path.nodes.Count > movementLimit)
            {
                path.nodes.RemoveRange(movementLimit, path.nodes.Count - movementLimit);
            }


            if (path != null)
            {
                pathEnumerator = path.nodes.GetEnumerator();
                pathEnumerator.MoveNext();
                SetTargetNode(pathEnumerator.Current, false);
            }
        }
All Usage Examples Of AStarPathfinder::FindPath