From afc66d3013b7d47c6c22d6a99809bc3e7d1ff0dc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 24 Oct 2024 14:44:20 +0200 Subject: implement breadth-first search pathfinding --- ViewController.cpp | 57 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 23 deletions(-) (limited to 'ViewController.cpp') 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(x * scale), - .y = static_cast(y * scale), - .width = static_cast(scale - line_width), - .height = static_cast(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(artist->data.x * scale), - .y = static_cast(artist->data.y * scale), - .width = static_cast(artist_size), - .height = static_cast(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(point.x * scale), - .y = static_cast(point.y * scale), - .width = static_cast(pathfinding_size), - .height = static_cast(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) { -- cgit v1.2.3