当前位置: 首页>>代码示例>>C#>>正文


C# PathNode.Equals方法代码示例

本文整理汇总了C#中PathNode.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# PathNode.Equals方法的具体用法?C# PathNode.Equals怎么用?C# PathNode.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PathNode的用法示例。


在下文中一共展示了PathNode.Equals方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindPath

    // A* Pathfinding
    public static List<PathNode> FindPath(PathNode start, PathNode end)
    {
        // TODO: diagonal cost not working right??}{???
        const float STRAIGHT_COST = 1.0f;
        const float DIAG_COST = 1.41421356237f;

        List<PathNode> open = new List<PathNode>();
        List<PathNode> closed = new List<PathNode>();

        // Return immediately if we're already at the end
        // or the cell we're on is blocked
        if (start.Equals(end))
        {
            List<PathNode> nodes = new List<PathNode>();
            nodes.Add(start);
            return nodes;
        }

        // Add starting cell to open list
        start.Parent = null;
        start.GScore = 0;
        start.OverallScore = 0;
        open.Add(start);

        // Set end parent to null so we know afterwards if we found it
        end.Parent = null;

        // Loop while open list has cells
        while (open.Count > 0)
        {
            // Find node in open list with lowest cost
            PathNode parent = open[0];

            foreach (PathNode node in open)
            {
                if (node.OverallScore < parent.OverallScore)
                    parent = node;
            }

            // Remove this node from the open list and add it to the closed list
            open.Remove(parent);
            closed.Add(parent);

            // Check if we've found the end
            if (parent == end)
                break;

            // Check adjacent nodes
            foreach (PathNode node in parent.AdjacentNodes)
            {
                // Ignore blocked nodes
                if (!node.Accessible)
                    continue;

                if (closed.Contains(node))
                    continue;

                // Calculate cost to get to this node from current node
                float xdiff = Mathf.Abs(node.Position.x - start.Position.x);
                float ydiff = Mathf.Abs(node.Position.y - start.Position.y);
                float manhattenDistance = xdiff + ydiff;

                float G = parent.GScore;

                if (manhattenDistance == 1.0f)
                    G += STRAIGHT_COST;
                else
                    G += DIAG_COST;

                // Calculate heuristic distance from this cell to the end
                xdiff = Mathf.Abs(node.Position.x - end.Position.x);
                ydiff = Mathf.Abs(node.Position.x - end.Position.x);
                manhattenDistance = xdiff + ydiff;

                float H = manhattenDistance * STRAIGHT_COST;

                // Calculate score
                float F = G + H;

                // If this node is already on the open list
                if (open.Contains(node))
                {
                    // If this path is better, replace it
                    if (G < node.GScore)
                    {
                        node.GScore = G;
                        node.OverallScore = F;
                        node.Parent = parent;
                    }
                }
                else
                {
                    node.GScore = G;
                    node.OverallScore = F;
                    node.Parent = parent;

                    // Add this node to the open list
                    open.Add(node);
                }
//.........这里部分代码省略.........
开发者ID:Catchouli-old,项目名称:SuperShooterGuy3D,代码行数:101,代码来源:PathFinding.cs


注:本文中的PathNode.Equals方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。