Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Node GetPath(Vector2 goal, Vector2 start)
- {
- List<Node> OpenNodes = new List<Node>();//список с не просмотренными узлами
- List<Node> ClosedNodes = new List<Node>();//список с не просмотренными узлами
- goal = new Vector2(Mathf.RoundToInt(goal.x), Mathf.RoundToInt(goal.y));
- OpenNodes.Add(new Node
- {
- pos = start,
- parent = null,
- DistStart = 0,
- DistOnGoal = GetDistOnGoal(Vector2.zero, goal)
- });
- while (OpenNodes.Count > 0)
- {
- var FirstNode = OpenNodes.OrderBy(node => node.FullDist).First();
- ClosedNodes.Add(FirstNode);
- OpenNodes.Remove(FirstNode);
- if (FirstNode.pos == goal)
- {
- return FirstNode;
- }
- var nigh = NighBours(FirstNode, goal);
- foreach (Node n in nigh)
- {
- if (ClosedNodes.Count(node => node.pos == n.pos) > 0)
- {
- continue;
- }
- var opennod = OpenNodes.FirstOrDefault(node => node.pos == n.pos);
- if (opennod == null)
- {
- OpenNodes.Add(n);
- }
- else
- {
- if (opennod.DistStart > n.DistStart)
- {
- opennod.DistStart = n.DistStart;
- opennod.parent = FirstNode;
- }
- }
- }
- }
- return null;
- }
- public float GetDistOnGoal(Vector2 n, Vector2 goal)
- {//дистанция до цели
- return Mathf.Abs((n-goal).magnitude);
- }
- public List<Node> NighBours(Node n, Vector2 goal)//соседи
- {
- var Point = new Vector2[4];
- var Nodes = new List<Node>();
- Point[0] = new Vector2(n.pos.x - 1, n.pos.y);
- Point[1] = new Vector2(n.pos.x + 1, n.pos.y);
- Point[2] = new Vector2(n.pos.x, n.pos.y + 1);
- Point[3] = new Vector2(n.pos.x, n.pos.y - 1);
- for (var i = 0; i < Point.Length; i++)
- {
- if ((Point[i].x > 0 && Point[i].x < map.GetLength(0) - 1)||( Point[i].y > 0 && Point[i].y < map.GetLength(1) - 1))
- {
- if (map[(int)Point[i].x, (int)Point[i].y] == 0)
- {
- Nodes.Add(
- new Node
- {
- pos = Point[i],
- parent = n,
- DistStart = n.DistStart + 1,
- DistOnGoal = GetDistOnGoal(Point[i], goal),
- });
- }
- }
- }
- return Nodes;
- }
- public void Move(float speed)
- {
- if (Points.Count > 0)
- {
- rb.velocity = (Points[0].pos * size - new Vector2(transform.position.x, transform.position.y)).normalized * speed;
- if (Vector2.Distance(transform.position, Points[0].pos * size) < 0.1f)
- {
- Points.RemoveAt(0);
- }
- }
- }
- public void UpPos(object n)
- {
- Parametrs p = (Parametrs)n;
- Vector2 _transform = p._transform;
- Vector2 pl = p.pl;
- if (goal != null)
- {
- Points = new List<Node>();
- Node point = goal;
- while (point != null)
- {
- Points.Add(point);
- point = point.parent;
- }
- }
- Points.Reverse();
- int posx = (int)System.Math.Ceiling(System.Math.Floor(_transform.x / size));
- int posy = (int)System.Math.Ceiling(System.Math.Floor(_transform.y / size));
- goal = GetPath(new Vector2(pl.x / size, pl.y / size), new Vector2(posx, posy));
- }
- public class Node
- {
- public Vector2 pos;
- public Node parent;
- public float DistStart;
- public float DistOnGoal;
- public float FullDist
- {
- get
- {
- return DistStart + DistOnGoal;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement