Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1.  public static IEnumerable<NodeElement> Neighbors(NodeElement id, Seeker seeker)
  2.     {
  3.         Direction curDir;
  4.         if (seeker._start == id)
  5.         {
  6.             //if this is the first node, get the general direction from it to the goal of the path
  7.             curDir = new Direction(Mathf.Clamp(seeker.LastDestination.x - id.x, -1, 1), Mathf.Clamp(seeker.LastDestination.y - id.y, -1, 1));
  8.         }
  9.         else
  10.         {
  11.             //otherwise, get the direction from the seeker's last node to this node
  12.             curDir = new Direction(Mathf.Clamp(-seeker.camefrom[id].x + id.x, -1, 1), Mathf.Clamp(-seeker.camefrom[id].y + id.y, -1, 1));
  13.         }
  14.         //if the direction is horizontal, add forward, and if the sides are unpassable, add their forward nodes too
  15.         //if the direction is vertical, do the same but with y and x reversed
  16.         //if the direction is diagonal, add the forward, left, right node
  17.         if (Mathf.Abs(curDir.x) == 1 && curDir.y == 0)
  18.         {
  19.             //horizontal
  20.             NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y);
  21.             NodeElement right = GetNodeAt(id.x, id.y + 1);
  22.             NodeElement left = GetNodeAt(id.x, id.y - 1);
  23.             if (middleForward != null)
  24.             {
  25.                 if (middleForward.Passable())
  26.                 {
  27.                     yield return middleForward;
  28.                 }
  29.             }
  30.             if (!right.Passable())
  31.             {
  32.                 NodeElement rightForward = GetNodeAt(id.x + curDir.x, id.y + 1);
  33.                 if (rightForward != null)
  34.                 {
  35.                     if (rightForward.Passable())
  36.                     {
  37.                         yield return rightForward;
  38.                     }
  39.                 }
  40.             }
  41.             if (!left.Passable())
  42.             {
  43.                 NodeElement leftForward = GetNodeAt(id.x + curDir.x, id.y - 1);
  44.                 if (leftForward != null)
  45.                 {
  46.                     if (leftForward.Passable())
  47.                     {
  48.                         yield return leftForward;
  49.                     }
  50.                 }
  51.             }
  52.  
  53.         }
  54.         else
  55.             if (Mathf.Abs(curDir.y) == 1 && curDir.x == 0)
  56.             {
  57.                 //vertical
  58.                 NodeElement middleForward = GetNodeAt(id.x, id.y + curDir.y);
  59.                 NodeElement right = GetNodeAt(id.x + 1, id.y);
  60.                 NodeElement left = GetNodeAt(id.x - 1, id.y);
  61.                 if (middleForward != null)
  62.                 {
  63.                     if (middleForward.Passable())
  64.                     {
  65.                         yield return middleForward;
  66.                     }
  67.                 } if (right != null)
  68.                     if (!right.Passable())
  69.                     {
  70.                         NodeElement rightForward = GetNodeAt(id.x + 1, id.y + curDir.y);
  71.                         if (rightForward != null)
  72.                         {
  73.                             if (rightForward.Passable())
  74.                             {
  75.                                 yield return rightForward;
  76.                             }
  77.                         }
  78.                     }
  79.                 if (left != null)
  80.                     if (!left.Passable())
  81.                     {
  82.                         NodeElement leftForward = GetNodeAt(id.x - 1, id.y + curDir.y);
  83.                         if (leftForward != null)
  84.                         {
  85.                             if (leftForward.Passable())
  86.                             {
  87.                                 yield return leftForward;
  88.                             }
  89.                         }
  90.                     }
  91.  
  92.             }
  93.             else
  94.                 if (Mathf.Abs(curDir.x) == Mathf.Abs(curDir.y))
  95.                 {
  96.                     //diagonal
  97.                     NodeElement middleForward = GetNodeAt(id.x + curDir.x, id.y + curDir.y);
  98.                     if (middleForward != null)
  99.                     {
  100.                         NodeElement right = GetNodeAt(middleForward.x - curDir.x, middleForward.y);
  101.                         NodeElement left = GetNodeAt(middleForward.x, middleForward.y - curDir.y);
  102.                         if (middleForward.Passable())
  103.                         {
  104.                             yield return middleForward;
  105.                         }
  106.                         if (right != null)
  107.                         {
  108.                             if (right.Passable())
  109.                             {
  110.                                 yield return right;
  111.                             }
  112.                         }
  113.                         if (left != null)
  114.                         {
  115.                             if (left.Passable())
  116.                             {
  117.                                 yield return left;
  118.                             }
  119.  
  120.                         }
  121.                     }
  122.  
  123.                 }
  124.  
  125.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement