diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 14:36:41 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 14:36:41 +0200 |
commit | 64028952ceb17f97ded08f1ab7ec0b06c41e2b87 (patch) | |
tree | f792377f93ff30a41a0c559c67aa874a022d4763 | |
parent | e522f2a36ee00a3e0890adb2c34bfc8431711265 (diff) |
add pathfinding start/end selection
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Museum.cpp | 2 | ||||
-rw-r--r-- | Museum.h | 6 | ||||
-rw-r--r-- | PathfindingContext.cpp | 34 | ||||
-rw-r--r-- | PathfindingContext.h | 25 | ||||
-rw-r--r-- | ViewController.cpp | 31 | ||||
-rw-r--r-- | ViewController.h | 11 | ||||
-rw-r--r-- | readme.md | 1 |
8 files changed, 104 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d43e54a..034f9ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ add_executable(main CollisionChecker.cpp NaiveCollisionChecker.cpp CycleCollisionMethodCommand.cpp + PathfindingContext.cpp ) target_link_libraries(main @@ -2,7 +2,7 @@ using namespace std; -Museum::Museum() : people(*this), canvas(*this), collision(*this) { +Museum::Museum() : people(*this), canvas(*this), collision(*this), pathfinding(*this) { this->worker = new std::thread(&Museum::work, this); } @@ -3,11 +3,12 @@ #include <thread> #include <chrono> -using namespace std::chrono_literals; - #include "People.h" #include "Canvas.h" #include "CollisionContext.h" +#include "PathfindingContext.h" + +using namespace std::chrono_literals; class Museum { public: @@ -18,6 +19,7 @@ public: People people; Canvas canvas; CollisionContext collision; + PathfindingContext pathfinding; public: bool paused = true; diff --git a/PathfindingContext.cpp b/PathfindingContext.cpp new file mode 100644 index 0000000..a1d31f6 --- /dev/null +++ b/PathfindingContext.cpp @@ -0,0 +1,34 @@ +#include "PathfindingContext.h" +#include "Museum.h" +#include "NullTileBehavior.h" + +using namespace std; + +PathfindingContext::PathfindingContext(Museum & m) : museum(m) {} + +void PathfindingContext::set_start(const pair<int, int> & point) { + if (!this->valid_point(point)) return; + this->start_point = point; +} + +void PathfindingContext::set_end(const pair<int, int> & 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; + + return true; +} + diff --git a/PathfindingContext.h b/PathfindingContext.h new file mode 100644 index 0000000..ce46a55 --- /dev/null +++ b/PathfindingContext.h @@ -0,0 +1,25 @@ +#pragma once + +#include <utility> + +class Museum; + +class PathfindingContext { +public: + PathfindingContext(Museum &); + +public: + void set_start(const std::pair<int, int> & point); + const std::pair<int, int> & get_start() { return this->start_point; } + void set_end(const std::pair<int, int> & point); + const std::pair<int, int> & get_end() { return this->end_point; } + bool valid_point(const std::pair<int, int> & point); + +private: + std::pair<int, int> start_point = { -1, -1 }; + std::pair<int, int> end_point = { -1, -1 }; + +private: + Museum & museum; +}; + diff --git a/ViewController.cpp b/ViewController.cpp index 989f11a..ec0315d 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -66,7 +66,28 @@ void ViewController::update_artists() { } } +void ViewController::draw_pathfinding_dot(unsigned int x, unsigned int y, const Color & c) { + this->view.fill_rect(center({ + .x = static_cast<float>(x * scale), + .y = static_cast<float>(y * scale), + .width = static_cast<float>(pathfinding_size), + .height = static_cast<float>(pathfinding_size), + }), c); +} + void ViewController::update_pathfinding() { + + PathfindingContext & ctx = this->museum.pathfinding; + this->draw_pathfinding_dot(ctx.get_end().first, ctx.get_end().second, { + .red = 0x00, + .green = 0x00, + .blue = 0xdd, + }); + this->draw_pathfinding_dot(ctx.get_start().first, ctx.get_start().second, { + .red = 0xff, + .green = 0xff, + .blue = 0xff, + }); } void ViewController::update_quadtree_recursive(QuadTreeCollisionChecker * tree) { @@ -140,11 +161,11 @@ void ViewController::ev_mousedown(MouseCode button) { try { switch (button) { case MOUSE_LEFT: { - // TODO: pathfinding start point + this->museum.pathfinding.set_start(this->mouse_pos); break; } case MOUSE_RIGHT: { - // TODO: pathfinding end point + this->museum.pathfinding.set_end(this->mouse_pos); break; } default: break; @@ -161,3 +182,9 @@ void ViewController::ev_mousemove(unsigned x, unsigned y) { }; } +Rectangle ViewController::center(Rectangle rect) { + rect.x += (scale - rect.width) / 2; + rect.y += (scale - rect.height) / 2; + return rect; +} + diff --git a/ViewController.h b/ViewController.h index 162ae19..d18e96a 100644 --- a/ViewController.h +++ b/ViewController.h @@ -2,9 +2,11 @@ #include <utility> +#include "Color.h" #include "Command.h" #include "KeyboardCode.h" #include "MouseCode.h" +#include "Rectangle.h" class View; class Museum; @@ -30,19 +32,26 @@ private: void update_quadtree_recursive(QuadTreeCollisionChecker * tree); private: + void draw_pathfinding_dot(unsigned int x, unsigned int y, const Color & c); + Rectangle center(Rectangle); + +private: Museum & museum; View & view; const Command * cmd_base = nullptr; private: bool draw_artists = true; - bool draw_pathfinding = false; + bool draw_pathfinding = true; bool draw_quadtree = true; private: unsigned int scale = 16; unsigned int line_width = 0; unsigned int artist_size = (scale - line_width) / 2; + unsigned int pathfinding_size = scale - 4; + +private: std::pair<float, float> mouse_pos = { 0, 0 }; }; @@ -6,7 +6,6 @@ DPA: ALGA: -- artist-artist collision behavior - artist-path collision behavior (toggleable) - pathfinding start/end selection w/ mouse buttons (left for start, right for end) - show {shortest,fastest,no} path toggle |