public void Clear() { pathNodes = new List <PathNode>(); closed = new List <Vector3>(); opened = new List <PathNode>(); startPos = Vector3.zero; endPos = Vector3.zero; mode = PathFindingMode.Diagonal; }
/// <summary> /// Calcs a path /// </summary> /// <param name="navQuery">Navigation query</param> /// <param name="filter">Filter</param> /// <param name="polyPickExt">Extensions</param> /// <param name="mode">Path mode</param> /// <param name="startPos">Start position</param> /// <param name="endPos">End position</param> /// <param name="resultPath">Result path</param> /// <returns>Returns the status of the path calculation</returns> private static Status CalcPath( NavMeshQuery navQuery, QueryFilter filter, Vector3 polyPickExt, PathFindingMode mode, Vector3 startPos, Vector3 endPos, out Vector3[] resultPath) { resultPath = null; navQuery.FindNearestPoly(startPos, polyPickExt, filter, out int startRef, out Vector3 nsp); navQuery.FindNearestPoly(endPos, polyPickExt, filter, out int endRef, out Vector3 nep); var endPointsDefined = (startRef != 0 && endRef != 0); if (!endPointsDefined) { return(Status.DT_FAILURE); } if (mode == PathFindingMode.TOOLMODE_PATHFIND_FOLLOW) { if (CalcPathFollow(navQuery, filter, startPos, endPos, startRef, endRef, out var path)) { resultPath = path; return(Status.DT_SUCCESS); } } else if (mode == PathFindingMode.TOOLMODE_PATHFIND_STRAIGHT) { if (CalcPathStraigh(navQuery, filter, startPos, endPos, startRef, endRef, out var path)) { resultPath = path; return(Status.DT_SUCCESS); } } else if (mode == PathFindingMode.TOOLMODE_PATHFIND_SLICED) { return(navQuery.InitSlicedFindPath( startRef, endRef, startPos, endPos, filter, FindPathOptions.DT_FINDPATH_ANY_ANGLE)); } return(Status.DT_FAILURE); }
public static List <HexTile> FindPath(HexTile start, HexTile end, PathFindingMode mode = PathFindingMode.None) { endNode = end; startNode = start; pathFindingMode = mode; if (pathFindingMode == PathFindingMode.ExcludeTarget) { targetFrame = endNode.currentFrame; } f = new Dictionary <HexTile, float>(); g = new Dictionary <HexTile, float>(); previous = new Dictionary <HexTile, HexTile>(); openList = new List <HexTile>(); closedList = new List <HexTile>(); f[startNode] = 0.0f; openList.Add(start); while (openList.Count > 0) { HexTile current = popMinTile(); if (current == end) { return(createPath()); } closedList.Add(current); epxandNode(current); } // No path found! return(null); }
public static List <PathNode> GetNearPathNodes(Vector3 pos, Vector3 startPos, Vector3 endPos, PathFindingMode mode) { List <PathNode> _pathNodes = new List <PathNode>(); var cpCoord = Utils.ChunkPosAndCoordForPosition(pos); int4 cp = cpCoord.Item1; int3 coord = cpCoord.Item2; void Try(int3 offset) { if (Utils.GetBlock(coord, cp, offset).x == 0) // check ofsetted to be air { if (Utils.GetBlock(new int3(coord.x, coord.y - 1, coord.z), cp, offset).x != 0) //check block under offsetted to be not air { if (Utils.GetBlock(new int3(coord.x, coord.y + 1, coord.z), cp, offset).x == 0) // check block above ofsetted to be air { if (offset.y == -1) { // for blocks which are lower need to make sure that there is extra space above if (Utils.GetBlock(new int3(coord.x, coord.y + 2, coord.z), cp, offset).x != 0) { return; } } if (offset.y == 1) { // for upper blocks need to make sure that character can jump if (Utils.GetBlock(new int3(coord.x, coord.y + 2, coord.z), cp, int3.zero).x != 0) { return; } } var v = new Vector3(pos.x + offset.x, pos.y + offset.y, pos.z + offset.z); var PathNode = new PathNode(v, startPos, endPos); _pathNodes.Add(PathNode); } } } } Try(new int3(-1, -1, 0)); Try(new int3(0, -1, -1)); Try(new int3(0, -1, 1)); Try(new int3(1, -1, 0)); Try(new int3(-1, 0, 0)); Try(new int3(0, 0, -1)); Try(new int3(0, 0, 1)); Try(new int3(1, 0, 0)); Try(new int3(-1, 1, 0)); Try(new int3(0, 1, -1)); Try(new int3(0, 1, 1)); Try(new int3(1, 1, 0)); if (mode == PathFindingMode.Diagonal) { Try(new int3(-1, -1, -1)); Try(new int3(-1, -1, 1)); Try(new int3(1, -1, -1)); Try(new int3(1, -1, 1)); Try(new int3(-1, 0, -1)); Try(new int3(-1, 0, 1)); Try(new int3(1, 0, -1)); Try(new int3(1, 0, 1)); Try(new int3(-1, 1, -1)); Try(new int3(-1, 1, 1)); Try(new int3(1, 1, -1)); Try(new int3(1, 1, 1)); } SortNodes(ref _pathNodes); return(_pathNodes); }
public void ToggleMode() { mode = mode == PathFindingMode.Diagonal ? PathFindingMode.Squared : PathFindingMode.Diagonal; }