//------------------------------------------------------------------------------------------------------- /** * @brief FindPath 寻路算法 * @param curPos 当前位置 * @param tarPos 目标位置 * @param[out] path 输出路径 */ public bool FindPath(Vector2 curPos, Vector2 tarPos, out List <Vector2> path) { path = null; //UnityEngine.Profiler.BeginSample("FindPath"); PathMove.Instance().findPath(new Vector2(curPos.x, -curPos.y), new Vector2(tarPos.x, -tarPos.y), 0.0f, out path); //UnityEngine.Profiler.EndSample(); if (path.Count <= 0) { return(false); } return(true); }
//------------------------------------------------------------------------------------------------------- // 获取地图上距离(计算阻挡) public bool CalcDistance(Vector2 curPos, Vector2 tarPos, out float fDistance, TileType tileType = TileType.TileType_None) { if (tileType == TileType.TileType_None) // 直接计算直线距离 { fDistance = Vector2.Distance(curPos, tarPos); return(true); } //if(MapObstacle.Instance.Check()) float ShortestMoveDst = 0.1f; MapGrid dstPt = MapGrid.GetMapGrid(new MapVector2(tarPos.x, tarPos.y)); Vector2 dir = tarPos - curPos; dir.Normalize(); dir *= ShortestMoveDst; MapGrid curPt = new MapGrid(0, 0); var vecDst = tarPos; bool bBlock = false; // 校验 for (; (vecDst - curPos).magnitude > ShortestMoveDst; curPos += dir) { var tmp = MapGrid.GetMapGrid(new MapVector2(curPos.x, curPos.y)); if (curPt != tmp) { curPt = tmp; } else { continue; } if (!MapObstacle.Instance.Check((int)curPt.x, (int)curPt.z, tileType)) { bBlock = true; break; } if (curPt == dstPt) { break; } } fDistance = 0.0f; if (!bBlock) { fDistance = Vector2.Distance(curPos, tarPos); } else { List <Vector2> path = null; if (!PathMove.Instance().findPath(new Vector2(curPos.x, curPos.y), new Vector2(tarPos.x, tarPos.y), 0.0f, out path)) { return(false); } Vector2 lastPos = curPos; for (int i = 0; i < path.Count; ++i) { fDistance += Vector2.Distance(lastPos, path[i]); lastPos = path[i]; } } return(true); }
//------------------------------------------------------------------------------------------------------- // 删除寻路回调 public void RemoveFindPathCallback(Action <List <Vector2> > findPath) { PathMove.Instance().PathFind -= findPath; }