MobaPath RetracePath(Node startNode, Node endNode) { List <Node> nodes = new List <Node>(); Node currentNode = endNode; while (currentNode != startNode) { nodes.Add(currentNode); currentNode = currentNode.parent; } nodes.Reverse(); if (nodes != null && nodes.Count > 0) { List <Node> smoothPath = SmoothPath(nodes); Vector2[] pathCorners = new Vector2[smoothPath.Count]; for (int i = 0; i < smoothPath.Count; i++) { pathCorners[i] = smoothPath[i].m_position; } MobaPath path = new MobaPath(smoothPath, pathCorners); sw.Stop(); //UnityEngine.Debug.Log("Smooth Path calculated in: " + sw.ElapsedMilliseconds); return(path); } return(null); }
public CalculatePathTask(MobaEntity entity, Vector2 startPos, Vector2 destinationPos, int time) : base(time) { m_startPos = startPos; m_entity = entity; if (Pathfinding.instance != null) { path = Pathfinding.instance.FindPath(startPos, destinationPos); } }
//Override method to calculate the path for the AI Entities public override MobaPath ServerCalculatePath(Vector2 target) { MobaPath path = base.ServerCalculatePath(target); if (path == null) { return(null); } Vector3[] points = new Vector3[path.Corners.Length]; for (int i = 0; i < path.Corners.Length; i++) { points[i] = new Vector3(path.Corners[i].x, transform.position.y + 0.02f, path.Corners[i].y); } m_debugPathLineRend.numPositions = path.Corners.Length; m_debugPathLineRend.SetPositions(points); return(path); }
public virtual MobaPath ServerCalculatePath(Vector2 target) { if (IsStun) { return(null); } MobaPath path = Pathfinding.instance.FindPath(Position, target); if (path == null) { //UnityEngine.Debug.Log("Path is null"); return(null); } RpcClientCalculatePath(Position, target, NetworkTime.Instance.ServerStep()); SetPath(path.Corners); //m_entityAbilities.IsCasting = false; return(path); }
public MobaPath FindPath(Vector2 startPos, Vector2 targetPos) { sw = new Stopwatch(); sw.Start(); Node startNode = grid.NodeFromWorldPoint(startPos); Node targetNode = grid.NodeFromWorldPoint(targetPos); if (!targetNode.walkable) { targetNode = GetNearestWalkableNode(startPos, targetPos, 1); if (targetNode == null) { UnityEngine.Debug.Log("Cannot find nearest walkable node"); return(null); } } if (startNode == targetNode) { if (Vector2.Distance(startPos, targetPos) > 0.1f) { List <Node> smoothPath = new List <Node>() { startNode, targetNode }; Vector2[] pathCorners = new Vector2[] { startPos, targetNode.m_position }; MobaPath path = new MobaPath(smoothPath, pathCorners); return(path); } else { return(null); } } Heap <Node> openSet = new Heap <Node>(grid.MaxSize); HashSet <Node> closedSet = new HashSet <Node>(); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { //sw.Stop(); //UnityEngine.Debug.Log("Path calculated in: " + sw.ElapsedMilliseconds); return(RetracePath(startNode, targetNode)); } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { //openSet.UpdateItem(neighbour); } } } } return(null); }