#include "PathfindingContext.h" #include "XY.h" #include "Museum.h" #include "NullTileBehavior.h" #include "ToggleMuseumPauseCommand.h" #include "DijkstraPathfinder.h" #include "BreadthFirstPathfinder.h" using namespace std; PathfindingContext::PathfindingContext(Museum & m) : museum(m) { // this->solvers.push_back(unique_ptr(new DijkstraPathfinder(m))); this->solvers.push_back(unique_ptr(new BreadthFirstPathfinder(m))); } void PathfindingContext::cycle_solver() { this->solver_index = (this->solver_index + 1) % this->solvers.size(); } Pathfinder & PathfindingContext::get_solver() { return *this->solvers[this->solver_index]; } void PathfindingContext::set_start(const XY & point) { if (!this->valid_point(point)) return; this->start_point = point; this->update(); } void PathfindingContext::set_end(const XY & point) { if (!this->valid_point(point)) return; this->end_point = point; this->update(); } bool PathfindingContext::valid_point(const XY & point) { try { // check if square is empty (has null behavior) Tile & tile = this->museum.canvas.get_tile(point); TileBehavior * behavior = tile.behavior.get(); if (dynamic_cast(behavior) != nullptr) return false; } catch (...) { // get_tile throws an exception if the point is outside the canvas bounds return false; } return true; } void PathfindingContext::update() { bool valid = true; if (!this->valid_point(this->start_point)) { this->start_point = { -1, -1 }; valid = false; } if (!this->valid_point(this->end_point)) { this->end_point = { -1, -1 }; valid = false; } if (!valid) return; ToggleMuseumPauseCommand(this->museum, true).execute(); Pathfinder & solver = this->get_solver(); solver.find_between(this->start_point, this->end_point); }