IEnumerator ComputeAsync(Vector2 startPos, Vector2 endPos, int stepIterations) { m_isComputingPath = true; // start and endPos are swapped because the result linkedlist is reversed IEnumerator coroutine = PathFinding.GetRouteFromToAsync(endPos, startPos); bool isFinished = false; do { for (int i = 0; i < stepIterations && !isFinished; ++i) { isFinished = !coroutine.MoveNext(); } yield return(null); }while (!isFinished); //Debug.Log("GetRouteFromToAsync execution time(ms): " + (Time.realtimeSinceStartup - now) * 1000); PathFinding.FindingParams findingParams = (PathFinding.FindingParams)coroutine.Current; m_pathNodes = findingParams.computedPath; ProcessComputedPath(); m_isComputingPath = false; if (OnComputedPath != null) { OnComputedPath(this); } yield return(null); }
IEnumerator ComputePath() { m_isComputing = true; IEnumerator coroutine = m_mapPathFinding.GetRouteFromToAsync(m_startTileIdx, m_endTileIdx); while (coroutine.MoveNext()) { yield return(null); } //Debug.Log("GetRouteFromToAsync execution time(ms): " + (Time.realtimeSinceStartup - now) * 1000); PathFinding.FindingParams findingParams = (PathFinding.FindingParams)coroutine.Current; m_path = findingParams.computedPath; //+++find closest node and take next one if possible m_curNode = Path.First; if (m_curNode != null) { Vector3 vPos = transform.position; vPos.z = ((MapTileNode)m_curNode.Value).Position.z; // use same z level while (m_curNode != null && m_curNode.Next != null) { MapTileNode n0 = m_curNode.Value as MapTileNode; MapTileNode n1 = m_curNode.Next.Value as MapTileNode; float distSqr = (vPos - n0.Position).sqrMagnitude; float distSqr2 = (vPos - n1.Position).sqrMagnitude; if (distSqr2 < distSqr) { m_curNode = m_curNode.Next; } else { break; } } // take next one, avoid moving backward in the path if (m_curNode.Next != null) { m_curNode = m_curNode.Next; } } //--- m_isComputing = false; if (OnComputedPath != null) { OnComputedPath(this); } yield return(null); }