#include "DfsStrategy.h" #include "routing/depth_first_search.h" #include "routing_api.h" #include "graph.h" using namespace routing; DfsStrategy::DfsStrategy(Vector3 pos_, Vector3 des_, const IGraph* graph_) { des = des_; currentIndex = 0; graph = graph_; std::vector posTemp; std::vector desTemp; posTemp.push_back(pos_.x); posTemp.push_back(pos_.y); posTemp.push_back(pos_.z); desTemp.push_back(des_.x); desTemp.push_back(des_.y); desTemp.push_back(des_.z); std::vector start = graph->NearestNode(posTemp, EuclideanDistance())->GetPosition(); std::vector end = graph->NearestNode(desTemp, EuclideanDistance())->GetPosition(); path = graph->GetPath(start, end, DepthFirstSearch::Default()); currentIndex = 0; maxIndex = path.size()-1; } bool DfsStrategy::IsCompleted(){ return currentIndex == maxIndex; } void DfsStrategy::Move(IEntity* entity, double dt){ Vector3 currentPos = entity->GetPosition(); Vector3 desVec = Vector3(path[currentIndex].at(0), path[currentIndex].at(1), path[currentIndex].at(2)); Vector3 dir = (desVec - currentPos).Unit(); float speed = entity->GetSpeed(); currentPos = currentPos + dir * speed * dt; lastPos = currentPos; entity->SetPosition(currentPos); entity->SetDirection(dir); if((desVec - lastPos).Magnitude()<1.0){ currentIndex++; } } std::vector> DfsStrategy::GetPath() { return path; }