diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 19:16:19 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 19:16:19 +0200 |
commit | 1e0a52b03fe655d7073ef20703dbb2e7646f74d3 (patch) | |
tree | f1709c2e9565d78c791653e71e6a4b26b3138423 /PathfindingContext.cpp | |
parent | 277157b3e06b2deeacbdbc8bf6190de19f88169d (diff) |
add XY struct for 2d points and offsets
Diffstat (limited to 'PathfindingContext.cpp')
-rw-r--r-- | PathfindingContext.cpp | 27 |
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; } |