void MoveToNextPoint_Local() { if (IsMoving) { return; } if (moveRode == null || moveRode.Count < 1) { IsMoving = false; objSign.SetActive(false); return; } MapManager.NavPoint point = moveRode.First.Value; moveRode.RemoveFirst(); Vector3 position = new Vector3(point.col, 0, point.row); if (position == transform.position || point.type < 1) { MoveToNextPoint_Local(); return; } Tweener tweener = transform.DOMove(position, fMoveCost); tweener.SetEase(Ease.InOutQuad); tweener.onComplete = () => { IsMoving = false; MoveToNextPoint_Local(); }; IsMoving = true; currentPoint = point; }
void SetTarget_Local(MapManager.NavPoint point) { moveRode = null; objSign.SetActive(true); objSign.transform.position = new Vector3(point.col, 0, point.row); System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); switch (navType) { case NavLibrary.Algorithm.DFS: moveRode = DFS.Navigation(currentPoint, point); break; case NavLibrary.Algorithm.BFS: moveRode = BFS.Navigation(currentPoint, point); break; case NavLibrary.Algorithm.A_Star: moveRode = A_Star.Navigation(currentPoint, point); break; default: moveRode = null; break; } stopWatch.Stop(); MainView.Instance.RefreshInfo(point.row, point.col, stopWatch.Elapsed.TotalMilliseconds); Debug.Log("Cost Time: " + stopWatch.Elapsed.TotalMilliseconds); if (!IsMoving) { MoveToNextPoint_Local(); } }
public static LinkedList <MapManager.NavPoint> Navigation(MapManager.NavPoint start, MapManager.NavPoint target) { curTarget = target; dicClosedNodes.Clear(); listNavResult.Clear(); FindNextPoint(start); return(listNavResult); }
public static LinkedList <MapManager.NavPoint> Navigation(MapManager.NavPoint start, MapManager.NavPoint target) { targetPoint = target; dicClosedNodes.Clear(); listNavResult.Clear(); listOpenNodes.Clear(); listOpenNodes.AddLast(new NodeRecord(start, null)); List <NodeRecord> listNeighbors = new List <NodeRecord>(4); while (listOpenNodes.Count > 0) { NodeRecord curRecord = listOpenNodes.First.Value; MapManager.NavPoint curPoint = curRecord.point; listOpenNodes.RemoveFirst(); if (dicClosedNodes.ContainsKey(curPoint.id)) { continue; } dicClosedNodes[curPoint.id] = true; if (curPoint == target) { do { listNavResult.AddFirst(curRecord.point); }while (curRecord = curRecord.parentRecord); return(listNavResult); } if (curPoint.type < 1) { continue; } listNeighbors.Clear(); for (int i = 0, length = rowNeighbors.Length; i < length; ++i) { MapManager.NavPoint neighbor = MapManager.Instance.GetPoint(curPoint.row + rowNeighbors[i], curPoint.col + colNeighbors[i]); if (neighbor && !dicClosedNodes.ContainsKey(neighbor.id)) { listNeighbors.Add(new NodeRecord(neighbor, curRecord)); } } listNeighbors.Sort((a, b) => { return(a.hScore - b.hScore); }); for (int i = 0, length = listNeighbors.Count; i < length; ++i) { listOpenNodes.AddLast(listNeighbors[i]); } } return(listNavResult); }
static bool FindNextPoint(MapManager.NavPoint point) { dicClosedNodes[point.id] = true; if (point == curTarget) { listNavResult.AddFirst(point); return(true); } if (point.type < 1) { return(false); } List <MapManager.NavPoint> listNeighbors = new List <MapManager.NavPoint>(4); for (int i = 0, length = rowNeighbors.Length; i < length; ++i) { MapManager.NavPoint neighbor = MapManager.Instance.GetPoint(point.row + rowNeighbors[i], point.col + colNeighbors[i]); if (neighbor) { listNeighbors.Add(neighbor); } } listNeighbors.Sort((a, b) => { return(Mathf.Abs(a.row - curTarget.row) + Mathf.Abs(a.col - curTarget.col) - Mathf.Abs(b.row - curTarget.row) - Mathf.Abs(b.col - curTarget.col)); }); for (int i = 0, length = listNeighbors.Count; i < length; ++i) { if (!dicClosedNodes.ContainsKey(listNeighbors[i].id) && FindNextPoint(listNeighbors[i])) { listNavResult.AddFirst(point); return(true); } } return(false); }
public NodeRecord(MapManager.NavPoint p, NodeRecord parent) { point = p; fScore = (Mathf.Abs(p.row - curTarget.row) + Mathf.Abs(p.col - curTarget.col)) + (gScore = (parentRecord = parent) == null ? 0 : parent.gScore + 1); }
public NodeRecord(MapManager.NavPoint p, NodeRecord parent) { point = p; parentRecord = parent; hScore = Mathf.Abs(p.row - targetPoint.row) + Mathf.Abs(p.col - targetPoint.col); }