public void Start(Vector3 curPos, Vector3 endPos) { OpenList.Clear(); ClosedList.Clear(); Path.Clear(); started = true; float startDist = 100000.0f; float endDist = 100000.0f; foreach (ListedNode n in this.nodeList) { float dist = Vector3.Distance(curPos, n.Position); if (dist < startDist) { startDist = dist; startNode = (ListedNode)n; } dist = Vector3.Distance(endPos, n.Position); if (dist < endDist) { endDist = dist; endNode = (ListedNode)n; } } startNode.Scores(startNode.Position, endNode.Position); endNode.Scores(startNode.Position, endNode.Position); currentNode = startNode; OpenList.Add(startNode); }
// We need to convert the grid we have, into a referenced grid to nodes without links. void Start() { int i = 0; // Set the nodes in a neat array myWaypoints = new ListedNode[WaypointList.Count]; foreach (WaypointNode n in WaypointList) { myWaypoints[i] = (ListedNode)n; i++; } // Set the neighbors in the neat array nodes for (i = 0; i < WaypointList.Count; i++) { // ITerate through the neighbornodes for (int x = 0; x < WaypointList[i].NeighborNodes.Length; x++) { ListedNode listedNode = getListedNode(WaypointList[i].NeighborNodes[x].from); if (myWaypoints[i].Position != listedNode.Position) { myWaypoints[i].NeighborNodes[x] = listedNode; continue; } listedNode = getListedNode(WaypointList[i].NeighborNodes[x].to); if (myWaypoints[i].Position != listedNode.Position) { myWaypoints[i].NeighborNodes[x] = listedNode; continue; } Debug.LogWarning("ERROR, NO MIRRION DORRARS FOR U!! F**K U!!"); } } }
void AddNeighboursToOpenList(ListedNode n) { foreach (ListedNode ln in n.NeighborNodes) { if (!OpenList.Contains(ln) && !ClosedList.Contains(ln)) { ln.Parent = currentNode; ln.Scores(startNode.Position, endNode.Position); OpenList.Add(ln); } } }
public bool Update() { if (started) { ListedNode bestNode = null; float BestF = 10000.0f; if (currentNode == endNode) { started = false; while (currentNode != startNode) { Path.Add(currentNode); currentNode = currentNode.Parent; ClosedList.Remove(currentNode); } } foreach (ListedNode n in OpenList) { if (n.F < BestF) { BestF = n.F; bestNode = n; } } currentNode = bestNode; if (bestNode != null) { OpenList.Remove(currentNode); ClosedList.Add(currentNode); AddNeighboursToOpenList(currentNode); } else { Debug.LogError("bestNode should not be null!!!!"); } return(true); } else { return(false); } }
// Update is called once per frame void FixedUpdate() { if (Input.GetKeyDown(KeyCode.F6) && Application.isEditor) { showNodes = !showNodes; } if (findPath) { if (instant) { while (myFinder.Update()) { } } else { myFinder.Update(); } if (!myFinder.Update()) { myPath = myFinder.GetPath(); findPath = false; startMoving = true; pointInPath = myPath.Count - 1; } } if (startMoving) { ListedNode point = null; if (pointInPath >= 0) { point = (ListedNode)myPath[pointInPath]; } if (point != null) { transform.Translate(((point.Position - transform.position).normalized * Time.deltaTime * 15.0f)); // transform.LookAt(point.Position); if (Vector3.Distance(transform.position, point.Position) < 0.2f) { pointInPath--; } } } }
public bool Update() { if (started) { ListedNode bestNode = null; float BestF = 10000.0f; if (currentNode == endNode) { started = false; while (currentNode != startNode) { Path.Add(currentNode); currentNode = currentNode.Parent; ClosedList.Remove(currentNode); } } foreach (ListedNode n in OpenList) { if (n.F < BestF) { BestF = n.F; bestNode = n; } } currentNode = bestNode; if (bestNode != null) { OpenList.Remove(currentNode); ClosedList.Add(currentNode); AddNeighboursToOpenList(currentNode); } else Debug.LogError("bestNode should not be null!!!!"); return true; } else { return false; } }
// Constructor for pathfinder. public PathFinder(ListedNode[] nodeList) { this.nodeList = nodeList; }