Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. public Node GetPath(Vector2 goal, Vector2 start)
  2. {
  3. List<Node> OpenNodes = new List<Node>();//список с не просмотренными узлами
  4. List<Node> ClosedNodes = new List<Node>();//список с не просмотренными узлами
  5. goal = new Vector2(Mathf.RoundToInt(goal.x), Mathf.RoundToInt(goal.y));
  6. OpenNodes.Add(new Node
  7. {
  8. pos = start,
  9. parent = null,
  10. DistStart = 0,
  11. DistOnGoal = GetDistOnGoal(Vector2.zero, goal)
  12. });
  13. while (OpenNodes.Count > 0)
  14. {
  15. var FirstNode = OpenNodes.OrderBy(node => node.FullDist).First();
  16. ClosedNodes.Add(FirstNode);
  17. OpenNodes.Remove(FirstNode);
  18. if (FirstNode.pos == goal)
  19. {
  20. return FirstNode;
  21. }
  22. var nigh = NighBours(FirstNode, goal);
  23. foreach (Node n in nigh)
  24. {
  25. if (ClosedNodes.Count(node => node.pos == n.pos) > 0)
  26. {
  27. continue;
  28. }
  29. var opennod = OpenNodes.FirstOrDefault(node => node.pos == n.pos);
  30. if (opennod == null)
  31. {
  32. OpenNodes.Add(n);
  33. }
  34. else
  35. {
  36. if (opennod.DistStart > n.DistStart)
  37. {
  38. opennod.DistStart = n.DistStart;
  39. opennod.parent = FirstNode;
  40.  
  41. }
  42. }
  43. }
  44. }
  45. return null;
  46. }
  47. public float GetDistOnGoal(Vector2 n, Vector2 goal)
  48. {//дистанция до цели
  49. return Mathf.Abs((n-goal).magnitude);
  50. }
  51.  
  52.  
  53. public List<Node> NighBours(Node n, Vector2 goal)//соседи
  54. {
  55. var Point = new Vector2[4];
  56. var Nodes = new List<Node>();
  57. Point[0] = new Vector2(n.pos.x - 1, n.pos.y);
  58. Point[1] = new Vector2(n.pos.x + 1, n.pos.y);
  59. Point[2] = new Vector2(n.pos.x, n.pos.y + 1);
  60. Point[3] = new Vector2(n.pos.x, n.pos.y - 1);
  61. for (var i = 0; i < Point.Length; i++)
  62. {
  63. if ((Point[i].x > 0 && Point[i].x < map.GetLength(0) - 1)||( Point[i].y > 0 && Point[i].y < map.GetLength(1) - 1))
  64. {
  65. if (map[(int)Point[i].x, (int)Point[i].y] == 0)
  66. {
  67. Nodes.Add(
  68. new Node
  69. {
  70. pos = Point[i],
  71. parent = n,
  72. DistStart = n.DistStart + 1,
  73. DistOnGoal = GetDistOnGoal(Point[i], goal),
  74. });
  75. }
  76.  
  77. }
  78. }
  79. return Nodes;
  80. }
  81.  
  82. public void Move(float speed)
  83. {
  84. if (Points.Count > 0)
  85. {
  86. rb.velocity = (Points[0].pos * size - new Vector2(transform.position.x, transform.position.y)).normalized * speed;
  87. if (Vector2.Distance(transform.position, Points[0].pos * size) < 0.1f)
  88. {
  89. Points.RemoveAt(0);
  90. }
  91. }
  92. }
  93. public void UpPos(object n)
  94. {
  95. Parametrs p = (Parametrs)n;
  96.  
  97. Vector2 _transform = p._transform;
  98. Vector2 pl = p.pl;
  99.  
  100. if (goal != null)
  101. {
  102. Points = new List<Node>();
  103. Node point = goal;
  104. while (point != null)
  105. {
  106. Points.Add(point);
  107. point = point.parent;
  108. }
  109. }
  110. Points.Reverse();
  111. int posx = (int)System.Math.Ceiling(System.Math.Floor(_transform.x / size));
  112. int posy = (int)System.Math.Ceiling(System.Math.Floor(_transform.y / size));
  113. goal = GetPath(new Vector2(pl.x / size, pl.y / size), new Vector2(posx, posy));
  114. }
  115. public class Node
  116. {
  117. public Vector2 pos;
  118. public Node parent;
  119. public float DistStart;
  120. public float DistOnGoal;
  121. public float FullDist
  122. {
  123. get
  124. {
  125. return DistStart + DistOnGoal;
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement