From 64028952ceb17f97ded08f1ab7ec0b06c41e2b87 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 23 Oct 2024 14:36:41 +0200 Subject: add pathfinding start/end selection --- CMakeLists.txt | 1 + Museum.cpp | 2 +- Museum.h | 6 ++++-- PathfindingContext.cpp | 34 ++++++++++++++++++++++++++++++++++ PathfindingContext.h | 25 +++++++++++++++++++++++++ ViewController.cpp | 31 +++++++++++++++++++++++++++++-- ViewController.h | 11 ++++++++++- readme.md | 1 - 8 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 PathfindingContext.cpp create mode 100644 PathfindingContext.h 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 diff --git a/Museum.cpp b/Museum.cpp index aaf5e83..bed7285 100644 --- a/Museum.cpp +++ b/Museum.cpp @@ -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); } diff --git a/Museum.h b/Museum.h index 7d55ba6..27e7915 100644 --- a/Museum.h +++ b/Museum.h @@ -3,11 +3,12 @@ #include #include -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 & point) { + if (!this->valid_point(point)) return; + this->start_point = point; +} + +void PathfindingContext::set_end(const pair & point) { + if (!this->valid_point(point)) return; + this->end_point = point; +} + +bool PathfindingContext::valid_point(const std::pair & 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(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 + +class Museum; + +class PathfindingContext { +public: + PathfindingContext(Museum &); + +public: + void set_start(const std::pair & point); + const std::pair & get_start() { return this->start_point; } + void set_end(const std::pair & point); + const std::pair & get_end() { return this->end_point; } + bool valid_point(const std::pair & point); + +private: + std::pair start_point = { -1, -1 }; + std::pair 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(x * scale), + .y = static_cast(y * scale), + .width = static_cast(pathfinding_size), + .height = static_cast(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 +#include "Color.h" #include "Command.h" #include "KeyboardCode.h" #include "MouseCode.h" +#include "Rectangle.h" class View; class Museum; @@ -29,6 +31,10 @@ private: void update_quadtree(); 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; @@ -36,13 +42,16 @@ private: 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 mouse_pos = { 0, 0 }; }; diff --git a/readme.md b/readme.md index 5e5a8e4..12a8bb2 100644 --- a/readme.md +++ b/readme.md @@ -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 -- cgit v1.2.3