aboutsummaryrefslogtreecommitdiff
path: root/PathfindingContext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'PathfindingContext.cpp')
-rw-r--r--PathfindingContext.cpp27
1 files changed, 13 insertions, 14 deletions
diff --git a/PathfindingContext.cpp b/PathfindingContext.cpp
index a1d31f6..cfebb44 100644
--- a/PathfindingContext.cpp
+++ b/PathfindingContext.cpp
@@ -1,4 +1,5 @@
#include "PathfindingContext.h"
+#include "XY.h"
#include "Museum.h"
#include "NullTileBehavior.h"
@@ -6,28 +7,26 @@ using namespace std;
PathfindingContext::PathfindingContext(Museum & m) : museum(m) {}
-void PathfindingContext::set_start(const pair<int, int> & point) {
+void PathfindingContext::set_start(const XY & point) {
if (!this->valid_point(point)) return;
this->start_point = point;
}
-void PathfindingContext::set_end(const pair<int, int> & point) {
+void PathfindingContext::set_end(const XY & point) {
if (!this->valid_point(point)) return;
this->end_point = point;
}
-bool PathfindingContext::valid_point(const std::pair<int, int> & point) {
- // check if out of bounds
- Canvas & canvas = this->museum.canvas;
- if (point.first < 0) return false;
- if (point.second < 0) return false;
- if (point.first >= canvas.data.columns) return false;
- if (point.second >= canvas.data.rows) return false;
-
- // check if square is empty (has null behavior)
- Tile & tile = canvas.get_tile(point.first, point.second);
- TileBehavior * behavior = tile.behavior.get();
- if (dynamic_cast<NullTileBehavior *>(behavior) != nullptr) return false;
+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<NullTileBehavior *>(behavior) != nullptr) return false;
+ } catch (...) {
+ // get_tile throws an exception if the point is outside the canvas bounds
+ return false;
+ }
return true;
}