⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.cs

📁 A .NET Path Finder Library Path Finder Library is a .NET library that currently contains one type c
💻 CS
字号:
using System;
using System.Collections.Generic;


namespace HenizeSoftware
{
  namespace PathFinding
  {
    class Node : IComparable<Node>, IEquatable<Node>
    {
      enum NodeType : byte { Above, Below, ToRight, ToLeft }
      
      int gCost;
      int hCost;
      ushort x;
      ushort y;
      Node parentNode;
      Node goalNode;
      Map ownerMap;

      public int GCost
      {
        get { return gCost; }
        set { gCost = value; }
      }

      public int HCost
      {
        get { return hCost; }
        set { hCost = value; }
      }

      public ushort X
      {
        get { return x; }
        set { x = value;  }
      }

      public ushort Y
      {
        get { return y; }
        set { y = value; }
      }

      public Node ParentNode
      {
        get { return parentNode; }
        set { parentNode = value;  }
      }

      public int TotalCost
      {
        get { return hCost + gCost; }
      }

      public Node(Node parentNode, Node goalNode, Map ownerMap, int x, int y)
      {
        this.parentNode = parentNode;
        this.goalNode = goalNode;
        this.x = (ushort)x;
        this.y = (ushort)y;
        this.ownerMap = ownerMap;
        CalculateCost();
      }

      void CalculateCost()
      {
        this.gCost = 10;

        if (parentNode != null)
        {
          //if (IsDiagonalTo(parentNode)) //not really needed but may become usefull
          //  this.gCost = 14;
        }
        else
        {
          this.gCost = 0;
        }
        
        this.gCost = (parentNode != null) ? parentNode.gCost + this.gCost : this.gCost;
        this.hCost = (goalNode != null) ?
          Math.Abs(goalNode.x - this.x) + Math.Abs(goalNode.y - this.y) : 0;
        
      }

      public bool IsDiagonalTo(Node other) //not really needed but may become usefull
      {
        return ((other.x < this.x && other.y < this.y) || //upper left
                (other.x > this.x && other.y < this.y) || //upper right
                (other.x < this.x && other.y > this.y) || //lower left
                (other.x > this.x && other.y > this.y));  //lower right
      }

      public bool IsAjacentTo(Node other)
      {
        if (other.x == this.x && other.y == this.y)
          return false;

        for (int ix = -1; ix <= 1; ix++)
        {
          for (int iy = -1; iy <= 1; iy++)
          {
            if (ix == 0 && iy == 0)
              continue;
            if (other.x == this.x + ix && other.y == this.y + iy)
              return true;
          }
        }

        return false;
      }

      public Node[] GetSuccessors()
      {
        Stack<Node> successors = new Stack<Node>();

        for (int ix = -1; ix <= 1; ix++)
        {
          for (int iy = -1; iy <= 1; iy++)
          {
            if (ownerMap[(ushort)(x + iy), (ushort)(y + ix)] != true)
            {
              Node n = new Node(this, this.goalNode, ownerMap, x + iy, y + ix);
              if (!n.Equals(this.parentNode) && !n.Equals(this))
              {
                successors.Push(n);
              }
            }
          }
        }
     
       return successors.ToArray();
      }

      public override bool Equals(object obj)
      {
        Node n = obj as Node;
        return n!=null && this.Equals(n);
      }
            
      public int CompareTo(Node other)
      {
        return this.TotalCost - other.TotalCost;
      }

      public bool Equals(Node other)
      {
        return (other != null && this.x == other.x && this.y == other.y);
      }

      #region "Operator Overloads"

      public static bool operator <(Node a, Node b)
      {
        return a.CompareTo(b) < 0;
      }
      
      public static bool operator <=(Node a, Node b)
      {
        return a.CompareTo(b) <= 0;
      }
      
      public static bool operator >(Node a, Node b)
      {
        return a.CompareTo(b) > 0;
      }
      
      public static bool operator >=(Node a, Node b)
      {
        return a.CompareTo(b) >= 0;
      }
      #endregion

    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -