コード例 #1
0
        /// <summary>
        /// Returns any nodes that are adjacent to <paramref name="fromNode"/> and may be considered to form the next step in the path
        /// </summary>
        /// <param name="fromNode">The node from which to return the next possible nodes in the path</param>
        /// <returns>A list of next possible nodes in the path</returns>
        private List <Node> GetAdjacentWalkableNodes(Node fromNode)
        {
            var walkableNodes = new List <Node>();
            var nextLocations = GetAdjacentLocations(fromNode.Location);

            foreach (var location in nextLocations)
            {
                var x = location.X;
                var y = location.Y;

                // Stay within the grid's boundaries
                if (x < 0 || x >= _map.GetLength(0) || y < 0 || y >= _map.GetLength(1))
                {
                    continue;
                }

                var node = _map[x, y];
                // Ignore non-walkable nodes
                if (!node.IsWalkable)
                {
                    continue;
                }

                // Ignore already-closed nodes
                if (node.State == NodeState.Closed)
                {
                    continue;
                }

                // Already-open nodes are only added to the list if their G-value is lower going via this route.
                if (node.State == NodeState.Open)
                {
                    var traversalCost = Node.GetTraversalCost(node.Location, node.ParentNode.Location);
                    var gTemp         = fromNode.G + traversalCost;
                    if (gTemp < node.G)
                    {
                        node.ParentNode = fromNode;
                        walkableNodes.Add(node);
                    }
                }
                else
                {
                    // If it's untested, set the parent and flag it as 'Open' for consideration
                    node.ParentNode = fromNode;
                    node.State      = NodeState.Open;
                    _testedPoints.Add(node.Location);
                    walkableNodes.Add(node);
                }
            }

            return(walkableNodes);
        }
コード例 #2
0
        /// <summary>
        /// Returns any nodes that are adjacent to <paramref name="fromNode"/> and may be considered to form the next step in the path
        /// </summary>
        /// <param name="fromNode">The node from which to return the next possible nodes in the path</param>
        /// <returns>A list of next possible nodes in the path</returns>
        private List <Node> GetAdjacentWalkableNodes(Node fromNode)
        {
            List <Node>         walkableNodes = new List <Node>();
            IEnumerable <Point> nextLocations = GetAdjacentLocations(fromNode.Location);

            foreach (var location in nextLocations)
            {
                int x = location.X;
                int y = location.Y;

                // Stay within the grid's boundaries
                if (x < 0 || x >= this.width || y < 0 || y >= this.height)
                {
                    continue;
                }

                Node node = this.nodes[x, y];
                // Ignore non-walkable nodes
                if (!node.IsWalkable)
                {
                    continue;
                }

                // Ignore already-closed nodes
                if (node.State == NodeState.Closed)
                {
                    continue;
                }

                // Already-open nodes are only added to the list if their G-value is lower going via this route.
                if (node.State == NodeState.Open)
                {
                    float traversalCost = Node.GetTraversalCost(node.Location, node.ParentNode.Location);
                    float gTemp         = fromNode.G + traversalCost;
                    if (gTemp < node.G)
                    {
                        node.ParentNode = fromNode;
                        walkableNodes.Add(node);
                    }
                }
                else
                {
                    // If it's untested, set the parent and flag it as 'Open' for consideration
                    node.ParentNode = fromNode;
                    node.State      = NodeState.Open;
                    walkableNodes.Add(node);
                }
            }

            return(walkableNodes);
        }