From 3e1b0eb968d90f1ba5163b220c4e39dd7fd1e51b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 22 Oct 2024 19:55:53 +0200 Subject: finish collision checking implementation --- CMakeLists.txt | 1 + CollisionContext.cpp | 22 ++++++++++++++++++++-- CollisionContext.h | 6 ++++++ CycleCollisionMethodCommand.cpp | 9 +++++++++ CycleCollisionMethodCommand.h | 17 +++++++++++++++++ NaiveCollisionChecker.cpp | 1 - NullCollisionChecker.h | 12 ++++++++++++ QuadTreeCollisionChecker.cpp | 1 - ViewController.cpp | 3 ++- ViewController.h | 2 +- docs/class-diag.puml | 4 ++++ readme.md | 6 ------ 12 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 CycleCollisionMethodCommand.cpp create mode 100644 CycleCollisionMethodCommand.h create mode 100644 NullCollisionChecker.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cf2f157..d43e54a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ add_executable(main ControlBooleanCommand.cpp CollisionChecker.cpp NaiveCollisionChecker.cpp + CycleCollisionMethodCommand.cpp ) target_link_libraries(main diff --git a/CollisionContext.cpp b/CollisionContext.cpp index ec608b5..6df5d3b 100644 --- a/CollisionContext.cpp +++ b/CollisionContext.cpp @@ -2,7 +2,9 @@ #include "CollisionContext.h" #include "Museum.h" + #include "NaiveCollisionChecker.h" +#include "NullCollisionChecker.h" #include "QuadTreeCollisionChecker.h" using namespace std; @@ -13,9 +15,25 @@ shared_ptr CollisionContext::get_checker() { return this->checker; } +shared_ptr CollisionContext::create_checker() { + switch(this->checker_index) { + case 0: return make_shared(this->museum); + case 1: return make_shared(this->museum); + case 2: return make_shared(this->museum); + } + + this->checker_index = 0; + return this->create_checker(); +} + void CollisionContext::update() { - this->checker = std::make_shared(this->museum); - // this->checker = std::make_shared(this->museum); + for (Artist * artist : this->museum.people.get_artists()) + artist->colliding = false; + this->checker = this->create_checker(); this->checker->check(); } +void CollisionContext::cycle_method() { + this->checker_index++; +} + diff --git a/CollisionContext.h b/CollisionContext.h index e35c373..0dd37c0 100644 --- a/CollisionContext.h +++ b/CollisionContext.h @@ -1,4 +1,5 @@ #pragma once + #include #include "CollisionChecker.h" @@ -12,11 +13,16 @@ public: void update(); std::shared_ptr get_checker(); + void cycle_method(); private: Museum & museum; private: std::shared_ptr checker = nullptr; + +private: + std::shared_ptr create_checker(); + size_t checker_index = 0; }; diff --git a/CycleCollisionMethodCommand.cpp b/CycleCollisionMethodCommand.cpp new file mode 100644 index 0000000..cc7b7f0 --- /dev/null +++ b/CycleCollisionMethodCommand.cpp @@ -0,0 +1,9 @@ +#include "CycleCollisionMethodCommand.h" +#include "Museum.h" + +CycleCollisionMethodCommand::CycleCollisionMethodCommand(Museum & m) : museum(m) { } + +void CycleCollisionMethodCommand::execute() { + this->museum.collision.cycle_method(); +} + diff --git a/CycleCollisionMethodCommand.h b/CycleCollisionMethodCommand.h new file mode 100644 index 0000000..1caee53 --- /dev/null +++ b/CycleCollisionMethodCommand.h @@ -0,0 +1,17 @@ +#pragma once + +#include "Command.h" + +class Museum; + +class CycleCollisionMethodCommand : public Command { +public: + CycleCollisionMethodCommand(Museum & m); + +public: + virtual void execute(); + +private: + Museum & museum; +}; + diff --git a/NaiveCollisionChecker.cpp b/NaiveCollisionChecker.cpp index db277ab..e26c3d2 100644 --- a/NaiveCollisionChecker.cpp +++ b/NaiveCollisionChecker.cpp @@ -5,7 +5,6 @@ void NaiveCollisionChecker::check() { auto artists = this->museum.people.get_artists(); - for (Artist * artist : artists) artist->colliding = false; auto begin = artists.begin(); auto end = artists.end(); for (auto it1 = begin; it1 != end; ++it1) { diff --git a/NullCollisionChecker.h b/NullCollisionChecker.h new file mode 100644 index 0000000..f6b4af2 --- /dev/null +++ b/NullCollisionChecker.h @@ -0,0 +1,12 @@ +#pragma once + +#include "CollisionChecker.h" + +class NullCollisionChecker : public CollisionChecker { + using CollisionChecker::CollisionChecker; + +public: + virtual void check() {}; + +}; + diff --git a/QuadTreeCollisionChecker.cpp b/QuadTreeCollisionChecker.cpp index 0d211c0..72337ee 100644 --- a/QuadTreeCollisionChecker.cpp +++ b/QuadTreeCollisionChecker.cpp @@ -86,7 +86,6 @@ void QuadTreeCollisionChecker::check() { if (this->artists_size > 0) { auto begin = this->artists.begin(); auto end = this->artists.end(); - for (Artist * artist : this->artists) artist->colliding = false; for (auto it1 = begin; it1 != end; ++it1) { auto it2 = it1; ++it2; diff --git a/ViewController.cpp b/ViewController.cpp index 7f3fdfc..989f11a 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -3,6 +3,7 @@ #include "ViewController.h" #include "CollisionChecker.h" #include "ControlBooleanCommand.h" +#include "CycleCollisionMethodCommand.h" #include "QuadTreeCollisionChecker.h" #include "Exception.h" #include "KeyboardCode.h" @@ -121,7 +122,7 @@ void ViewController::ev_keydown(KeyboardCode key) { break; } case KEY_C: { - // CycleCollisionMethodCommand(this->museum).execute(); + CycleCollisionMethodCommand(this->museum).execute(); break; } case KEY_Q: { diff --git a/ViewController.h b/ViewController.h index 4ff73c5..162ae19 100644 --- a/ViewController.h +++ b/ViewController.h @@ -37,7 +37,7 @@ private: private: bool draw_artists = true; bool draw_pathfinding = false; - bool draw_quadtree = false; + bool draw_quadtree = true; private: unsigned int scale = 16; diff --git a/docs/class-diag.puml b/docs/class-diag.puml index 58ed950..9a37256 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -388,6 +388,9 @@ rectangle Group_Commands as "Commands" <> { - value : bool - target : bool & } + class CycleCollisionMethodCommand { + + constructor(Museum &) + } Command <|-d- ToggleMuseumPauseCommand Command <|-u- OpenFileGUICommand @@ -395,6 +398,7 @@ rectangle Group_Commands as "Commands" <> { Command <|-d- StepTileCommand Command <|-d- LoadFilesCommand Command <|-d- TimeTravelCommand + Command <|-u- CycleCollisionMethodCommand } } /' LAYOUT '/ diff --git a/readme.md b/readme.md index 11574a0..5e5a8e4 100644 --- a/readme.md +++ b/readme.md @@ -12,9 +12,3 @@ ALGA: - show {shortest,fastest,no} path toggle - vast meer -TODO: - -- low-binding factories naar high-binding factories -- prototype met geen factory maar map ofzo fixen -- txt file parser - -- cgit v1.2.3