/////////////////////////////////// /// Here Lies the Dijkstra Call /// /////////////////////////////////// // Update is called once per frame void Update() { // If there is not startNode and no Goal node if (StartNode != null && GoalNode != null) { // have somewhere to go if (path == null || path.StartNode != StartNode || path.GoalNode != GoalNode) { // either have no path yet or path data is stale (no longer correct) so get new path path = algorithm.GetPath(StartNode, GoalNode, TravelerProfileCatalog.GetProfile(Type), CostMethod); if (path != null && path.GetCurrentVertex() != null && StartAtStartNode) { transform.position = path.GetCurrentVertex().transform.position; StartAtStartNode = false; } } } else { // have nowhere to go path = null; } if (Move && path != null && path.GetCurrentVertex() != null) { VertexScript nextNode = path.GetCurrentVertex(); // <- In PathData.cs, returns the current vertex currentNode = path.GetCurrentVertex(); // returns the current vertex the actor is at // TODO: Upon getting current vertex, if the actors heartrate > maxBPM // Wait untill heartrate is at safe BPM // after reaching safe BPM, continue with movement Vector3 nextDestination = nextNode.transform.position; Vector3 currentPosition = transform.position; Vector3 difference = nextDestination - currentPosition; // difference.Normalize (); // scales vector to have length = 1 FacingDirection = difference; float distanceToDest = difference.magnitude; if (distanceToDest <= DELTA) { // we're already there, so just correct the position // and advance to the next vertex in the path transform.position = nextDestination; path.AdvanceToNextVertex(); } else if (distanceToDest < Speed * Time.deltaTime) { // we're close enough to arrive there this frame transform.position = nextDestination; } else { // it'll take more than one frame to arrive there Vector3 normalizedVelocity = difference.normalized; // velocity vector Vector3 movementThisFrame = Speed * normalizedVelocity * Time.deltaTime; transform.position += movementThisFrame; } } }
/////////////////////////////////// /// Here Lies the Dijkstra Call /// /////////////////////////////////// // Update is called once per frame void Update() { // Always checks to ensure the actor is at a safeBPM safeBPMCheck(); // If there is a startNode and a Goal node if (StartNode != null && GoalNode != null) { // have somewhere to go if (path == null || path.StartNode != StartNode || path.GoalNode != GoalNode) { // Checks if Dijkstra has already ran before if (dijkstraIsRunning == false && dijkstraHasPath == false || GoalNode != CurrentNode) { // runs Dijkstra's algorithm runDijkstra(ref StartNode, ref GoalNode, CostMethod); } if (path != null && path.GetCurrentVertex() != null && StartAtStartNode) { transform.position = path.GetCurrentVertex().transform.position; StartAtStartNode = false; } } // Sets the current node as the start node if (CurrentNode == GoalNode && dijkstraHasPath == true) { dijkstraHasPath = false; StartNode = GoalNode; } } else { // have nowhere to go path = null; } // If the actor can move if (Move && path != null && path.GetCurrentVertex() != null) { VertexScript nextNode = path.GetCurrentVertex(); // <- In PathData.cs, returns the current vertex NextNode = path.GetCurrentVertex(); // returns the current vertex the actor is at // TODO: Upon getting current vertex, if the actors heartrate > maxBPM // Wait untill heartrate is at safe BPM // after reaching safe BPM, continue with movement Vector3 nextDestination = nextNode.transform.position; Vector3 currentPosition = transform.position; Vector3 difference = nextDestination - currentPosition; // difference.Normalize (); // scales vector to have length = 1 FacingDirection = difference; float distanceToDest = difference.magnitude; if (distanceToDest <= DELTA) { // we're already there, so just correct the position // and advance to the next vertex in the path transform.position = nextDestination; path.AdvanceToNextVertex(); } else if (distanceToDest < Speed * Time.deltaTime) { // we're close enough to arrive there this frame transform.position = nextDestination; } else { // it'll take more than one frame to arrive there Vector3 normalizedVelocity = difference.normalized; // velocity vector Vector3 movementThisFrame = Speed * normalizedVelocity * Time.deltaTime; transform.position += movementThisFrame; } } }