Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.13 KB | None | 0 0
  1. public class Node
  2.     {
  3.         public int gridX;
  4.         public int gridY;
  5.         public Vector3Int location;
  6.         public List<NodeLink> links;
  7.  
  8.         public class NodeLink
  9.         {
  10.             public int weight;
  11.             public Node parent;
  12.             public Node child;
  13.         }
  14.  
  15.         #region Link Functions
  16.         // Create a new link, connecting this Node to the Node passed in the parameter
  17.         public void AddLink(Node _child)
  18.         {
  19.             if(_child != null && _child != this)
  20.             {
  21.                 links.Add(new NodeLink
  22.                 {
  23.                     parent = this,
  24.                     child = _child,
  25.                     weight = _child.MoveCost()
  26.                 });
  27.  
  28.                 if (!_child.links.Exists(a => a.parent == _child && a.child == this))
  29.                 {
  30.                     _child.AddLink(this);
  31.                 }
  32.             }
  33.         }
  34.  
  35.         public void SetLinks()
  36.         {
  37.             foreach(Node neighbor in GetNeighbors(NeighborCheckMethod.Standard))
  38.             {
  39.                 AddLink(neighbor);
  40.             }
  41.         }
  42.  
  43.         public void WipeLinks()
  44.         {
  45.             links = new List<NodeLink>();
  46.         }
  47.  
  48.         public void RefreshLinks()
  49.         {
  50.             WipeLinks();
  51.             SetLinks();
  52.         }
  53.         #endregion
  54.  
  55.         #region Node Functions
  56.         /*--Can be gone through but cannot be stopped on--*/
  57.         public bool Walkable()
  58.         {
  59.             if (battleGrid.NodeGetUnitTile(location) != null)
  60.             {
  61.                 /*--if blocker is true, walkable must be false and vice versa--*/
  62.                 return !battleGrid.NodeGetUnitTile(location).blocker;
  63.             }
  64.             else
  65.                 return true;
  66.         }
  67.        
  68.         /*--Can be stopped on--*/
  69.         public bool Travelable() {
  70.             /*--Checks if units are in the same space--*/
  71.             if (battleGrid.NodeGetUnitTile(location) != null)
  72.             {
  73.                 return false;
  74.             }
  75.             else
  76.             {
  77.                 return true;
  78.             }
  79.         }
  80.  
  81.         public int MoveCost()
  82.         {
  83.             int baseMC = battleGrid.NodeGetBaseTile(location).baseMoveCost;
  84.             int mod = 0;
  85.             /*--needed for ever--*/
  86.             if (battleGrid.NodeGetModTile(location) != null)
  87.             {
  88.                 switch (battleGrid.NodeGetModTile(location).modType)
  89.                 {
  90.                     case Constants.TileModType.None:
  91.                         break;
  92.                     case Constants.TileModType.Blood:
  93.                         Debug.Log("Got a blood tile and you need to set a move modifier for it");
  94.                         break;
  95.                 }
  96.                 return baseMC + mod;
  97.             }
  98.             else
  99.                 return baseMC;
  100.         }
  101.  
  102.         public Unit OccupyingUnit()
  103.         {
  104.             //Debug.Log("OccupyingUnit being called from " + gridX  + "" + gridY);
  105.  
  106.             if (!Travelable())
  107.             {
  108.                 //Debug.Log("Got this far");
  109.                 foreach (GameObject entity in battleGrid.unitCollector.allUnits)
  110.                 {
  111.                     if (entity.GetComponent<Unit>().currTile.node == this)
  112.                     {
  113.                         //Debug.Log(entity.gameObject.name);
  114.                         return entity.GetComponent<Unit>();
  115.                     }
  116.                 }
  117.                 return null;
  118.             }
  119.             else
  120.                 return null;
  121.         }
  122.  
  123.         public List<Node> GetNeighbors(NeighborCheckMethod checkMethod)
  124.         {
  125.             List<Node> neighbors = new List<Node>();
  126.  
  127.             switch (checkMethod)
  128.             {
  129.                 case NeighborCheckMethod.CardinalsFirst:
  130.                     for (int y = gridY - 1; y <= gridY + 1; y++)
  131.                     {
  132.                         if (y <= gridY && y >= gridY)
  133.                         {
  134.                             continue;
  135.                         }
  136.                         if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
  137.                         {
  138.                             //Debug.Log("Neighbor outside map bounds through X value");
  139.                         }
  140.                         else
  141.                         {
  142.                             neighbors.Add(nodeGrid[gridX, y]);
  143.                         }
  144.                     }
  145.                     for (int x = gridX - 1; x <= gridX + 1; x++)
  146.                     {
  147.                         if (x <= gridX && x >= gridX)
  148.                         {
  149.                             continue;
  150.                         }
  151.                         if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
  152.                         {
  153.                             //Debug.Log("Neighbor outside map bounds through X value");
  154.                         }
  155.                         else
  156.                         {
  157.                             neighbors.Add(nodeGrid[x, gridY]);
  158.                         }
  159.                     }
  160.                     for (int x = gridX - 1; x <= gridX + 1; x++)
  161.                     {
  162.                         for (int y = gridY - 1; y <= gridY + 1; y++)
  163.                         {
  164.                             if (x <= gridX && x >= gridX)
  165.                             {
  166.                                 if (y <= gridY && y >= gridY)
  167.                                 {
  168.                                     continue;
  169.                                 }
  170.                             }
  171.                             if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
  172.                             {
  173.                                 //Debug.Log("Neighbor outside map bounds through X value");
  174.                             }
  175.                             else if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
  176.                             {
  177.                                 //Debug.Log("Neighbor outside map bounds through Y value");
  178.                             }
  179.                             else if (!neighbors.Contains(nodeGrid[x, y]))
  180.                             {
  181.                                 neighbors.Add(nodeGrid[x, y]);
  182.                             }
  183.                         }
  184.                     }
  185.                     break;
  186.                 case NeighborCheckMethod.Standard:
  187.                     /*--Checks all adjacent tiles by going through all of the nodes within 1 tile in the X and Y dimensions--*/
  188.  
  189.                     for (int x = gridX - 1; x <= gridX + 1; x++)
  190.                     {
  191.                         for (int y = gridY - 1; y <= gridY + 1; y++)
  192.                         {
  193.                             if (x <= gridX && x >= gridX)
  194.                             {
  195.                                 if (y <= gridY && y >= gridY)
  196.                                 {
  197.                                     continue;
  198.                                 }
  199.                             }
  200.                             if (x < 0 || x > battleGrid.overallMapBounds.x - 1)
  201.                             {
  202.                                 //Debug.Log("Neighbor outside map bounds through X value");
  203.                             }
  204.                             else if (y < 0 || y > battleGrid.overallMapBounds.y - 1)
  205.                             {
  206.                                 //Debug.Log("Neighbor outside map bounds through Y value");
  207.                             }
  208.                             else
  209.                             {
  210.                                 neighbors.Add(nodeGrid[x, y]);
  211.                             }
  212.                         }
  213.                     }
  214.  
  215.                     break;
  216.             }
  217.  
  218.             //foreach(Node node in neighbors)
  219.             //{
  220.             //    Debug.Log(location + " " + node.location);
  221.             //}
  222.             return neighbors;
  223.         }
  224.  
  225.  
  226.  
  227.  
  228.         #endregion
  229.  
  230.         ///*--Used Solely in DStar--*/
  231.         //public int pathCost;
  232.         //public Node pathParent;
  233.        
  234.         /*--Constructor--*/
  235.         public Node(int _gridX, int _gridY)
  236.         {
  237.             gridX = _gridX;
  238.             gridY = _gridY;
  239.             location = new Vector3Int(gridX, gridY, 0);
  240.         }
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement