diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 14:44:20 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 14:44:20 +0200 |
commit | afc66d3013b7d47c6c22d6a99809bc3e7d1ff0dc (patch) | |
tree | de50baa5d2f87cc8a416cbd321f7c7f430b03613 /ViewController.cpp | |
parent | 1e0a52b03fe655d7073ef20703dbb2e7646f74d3 (diff) |
implement breadth-first search pathfinding
Diffstat (limited to 'ViewController.cpp')
-rw-r--r-- | ViewController.cpp | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/ViewController.cpp b/ViewController.cpp index 40e624a..b1fd795 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -14,6 +14,7 @@ #include "TimeTravelCommand.h" #include "View.h" #include "Museum.h" +#include "Pathfinder.h" using namespace std; @@ -43,10 +44,10 @@ void ViewController::update_tiles() { for (int x = 0; x < this->museum.canvas.data.columns; x++) { Tile & tile = this->museum.canvas.get_tile({ x, y }); Rectangle rect = { - .x = static_cast<float>(x * scale), - .y = static_cast<float>(y * scale), - .width = static_cast<float>(scale - line_width), - .height = static_cast<float>(scale - line_width), + .x = x * scale, + .y = y * scale, + .width = scale, + .height = scale, }; this->view.fill_rect(rect, tile.color); } @@ -57,10 +58,10 @@ void ViewController::update_artists() { People & people = this->museum.people; for (Artist * artist : people.get_artists()) { Rectangle rect = { - .x = static_cast<float>(artist->data.x * scale), - .y = static_cast<float>(artist->data.y * scale), - .width = static_cast<float>(artist_size), - .height = static_cast<float>(artist_size), + .x = artist->data.x * scale, + .y = artist->data.y * scale, + .width = artist_size, + .height = artist_size, }; this->view.fill_rect(rect, artist->color); } @@ -68,26 +69,36 @@ void ViewController::update_artists() { void ViewController::draw_pathfinding_dot(const XY & point, const Color & color) { this->view.fill_rect(center({ - .x = static_cast<float>(point.x * scale), - .y = static_cast<float>(point.y * scale), - .width = static_cast<float>(pathfinding_size), - .height = static_cast<float>(pathfinding_size), + .x = point.x * scale, + .y = point.y * scale, + .width = pathfinding_size, + .height = pathfinding_size, }), color); } void ViewController::update_pathfinding() { - PathfindingContext & ctx = this->museum.pathfinding; - this->draw_pathfinding_dot(ctx.get_end(), { - .red = 0x00, - .green = 0x00, - .blue = 0xdd, - }); - this->draw_pathfinding_dot(ctx.get_start(), { - .red = 0xff, - .green = 0xff, - .blue = 0xff, - }); + + Pathfinder & solver = ctx.get_solver(); + for (int y = 0; y < this->museum.canvas.data.rows; y++) { + for (int x = 0; x < this->museum.canvas.data.columns; x++) { + if (!solver.is_visited({ x, y })) continue; + Rectangle rect = { + .x = x * scale, + .y = y * scale, + .width = scale, + .height = scale, + }; + this->view.draw_rect(rect, { 0, 0, 0 }, 2); + } + } + + const Pathfinder::Path & solution = solver.get_path(); + for (const XY & point : solution) { + this->draw_pathfinding_dot(point, { 0, 0, 0 }); + } + + this->draw_pathfinding_dot(ctx.get_start(), { 0xff, 0xff, 0xff }); } void ViewController::update_quadtree_recursive(QuadTreeCollisionChecker * tree) { |