diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-15 13:45:28 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-15 13:45:28 +0200 |
commit | d33b8f7b781aada0c1082ee426fca65c6f1747dd (patch) | |
tree | cc62ee637735ab3dd8dcd0c2f3c718c8d1390d97 | |
parent | a38cfbc9f15b0816e65efc7a8ccb30994ab60f30 (diff) |
implement tile poking command
-rw-r--r-- | BlueTileBehavior.cpp | 22 | ||||
-rw-r--r-- | BlueTileBehavior.h | 4 | ||||
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Canvas.cpp | 2 | ||||
-rw-r--r-- | RedTileBehavior.cpp | 5 | ||||
-rw-r--r-- | RedTileBehavior.h | 2 | ||||
-rw-r--r-- | Tile.cpp | 2 | ||||
-rw-r--r-- | Tile.h | 2 | ||||
-rw-r--r-- | TileBehaviorStrategy.cpp | 2 | ||||
-rw-r--r-- | TileBehaviorStrategy.h | 2 | ||||
-rw-r--r-- | TileDecayCommand.cpp | 10 | ||||
-rw-r--r-- | TileDecayCommand.h | 11 | ||||
-rw-r--r-- | ViewController.cpp | 3 |
13 files changed, 49 insertions, 19 deletions
diff --git a/BlueTileBehavior.cpp b/BlueTileBehavior.cpp index 487398a..b0716bc 100644 --- a/BlueTileBehavior.cpp +++ b/BlueTileBehavior.cpp @@ -9,25 +9,29 @@ using namespace std; BlueTileBehavior BlueTileBehavior::instance { BlueTileBehavior::type }; -void BlueTileBehavior::step(Artist & artist) { +void BlueTileBehavior::step(Artist * artist) { this->interactions++; - if (this->interactions > 1) return; + if (dx != 0 || dy != 0) return; + if (artist == nullptr) return; - if (artist.data.vx == 0) dx = 1; - if (artist.data.vy == 0) dy = 1; + if (artist->data.vx == 0) dx = 1; + if (artist->data.vy == 0) dy = 1; } -void BlueTileBehavior::update_neighbor(Tile * tile) { - if (tile == nullptr) return; +void BlueTileBehavior::update_neighbor(Tile & here, int dx, int dy) { + if (dx == 0 && dy == 0) return; + Tile * neighbor = here.get_neighbor(dx, dy); + if (neighbor == &here) return; + if (neighbor == nullptr) return; - tile->set_type(BlueTileBehavior::type); + neighbor->set_type(BlueTileBehavior::type); } void BlueTileBehavior::update(Tile & tile) { if (this->interactions < 1) return; - this->update_neighbor(tile.get_neighbor(this->dx, this->dy)); - this->update_neighbor(tile.get_neighbor(-this->dx, -this->dy)); + this->update_neighbor(tile, this->dx, this->dy); + this->update_neighbor(tile, -this->dx, -this->dy); tile.set_type(YellowTileBehavior::type); } diff --git a/BlueTileBehavior.h b/BlueTileBehavior.h index a01b50d..3aba86b 100644 --- a/BlueTileBehavior.h +++ b/BlueTileBehavior.h @@ -4,7 +4,7 @@ class BlueTileBehavior : public TileBehaviorStrategy { public: - virtual void step(Artist &); + virtual void step(Artist *); virtual void update(Tile &); virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &); @@ -16,7 +16,7 @@ private: BlueTileBehavior() = default; private: - virtual void update_neighbor(Tile *); + virtual void update_neighbor(Tile & here, int dx, int dy); int dx = 0; int dy = 0; }; diff --git a/CMakeLists.txt b/CMakeLists.txt index a238235..aa0ef49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ add_executable(main LoadFilesCommand.cpp MuseumPauseCommand.cpp ArtistVisibilityCommand.cpp + TileDecayCommand.cpp ) target_link_libraries(main @@ -79,7 +79,7 @@ void Canvas::update_steps() { if (artist->step == false) continue; artist->step = false; - this->get_tile(artist->data.x, artist->data.y).step(*artist); + this->get_tile(artist->data.x, artist->data.y).step(artist); } } diff --git a/RedTileBehavior.cpp b/RedTileBehavior.cpp index 0c0dff4..0f0426d 100644 --- a/RedTileBehavior.cpp +++ b/RedTileBehavior.cpp @@ -9,9 +9,10 @@ using namespace std; RedTileBehavior RedTileBehavior::instance { RedTileBehavior::type }; -void RedTileBehavior::step(Artist & artist) { +void RedTileBehavior::step(Artist * artist) { this->interactions++; - this->museum->people.remove_artist(artist); + if (artist != nullptr) + this->museum->people.remove_artist(*artist); } void RedTileBehavior::update(Tile & tile) { diff --git a/RedTileBehavior.h b/RedTileBehavior.h index 1ac3e64..b7443c9 100644 --- a/RedTileBehavior.h +++ b/RedTileBehavior.h @@ -4,7 +4,7 @@ class RedTileBehavior : public TileBehaviorStrategy { public: - virtual void step(Artist &); + virtual void step(Artist *); virtual void update(Tile &); virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum &); @@ -26,7 +26,7 @@ void Tile::update() { this->behavior->update(*this); } -void Tile::step(Artist & artist) { +void Tile::step(Artist * artist) { this->behavior->step(artist); } @@ -24,7 +24,7 @@ public: void set_data(TileData & data); void set_type(const std::string & type); void update(); - void step(Artist &); + void step(Artist *); Tile * get_neighbor(int dx, int dy); private: diff --git a/TileBehaviorStrategy.cpp b/TileBehaviorStrategy.cpp index ab551f1..99566b8 100644 --- a/TileBehaviorStrategy.cpp +++ b/TileBehaviorStrategy.cpp @@ -5,7 +5,7 @@ TileBehaviorStrategy::TileBehaviorStrategy(const std::string type) { TileBehavior::register_strategy(type, this); } -void TileBehaviorStrategy::step(Artist &) { +void TileBehaviorStrategy::step(Artist *) { this->interactions++; } diff --git a/TileBehaviorStrategy.h b/TileBehaviorStrategy.h index 156b601..0a24abb 100644 --- a/TileBehaviorStrategy.h +++ b/TileBehaviorStrategy.h @@ -9,7 +9,7 @@ class Artist; class TileBehaviorStrategy { public: - virtual void step(Artist &); + virtual void step(Artist *); virtual void update(Tile &) = 0; virtual std::unique_ptr<TileBehaviorStrategy> clone(Museum & m) = 0; diff --git a/TileDecayCommand.cpp b/TileDecayCommand.cpp new file mode 100644 index 0000000..9497121 --- /dev/null +++ b/TileDecayCommand.cpp @@ -0,0 +1,10 @@ +#include "TileDecayCommand.h" +#include "Museum.h" + +void TileDecayCommand::execute(unsigned int x, unsigned int y) { + Museum & museum = this->get_museum(); + if (x >= museum.canvas.data.columns) return; + if (y >= museum.canvas.data.rows) return; + museum.canvas.get_tile(x, y).step(nullptr); +} + diff --git a/TileDecayCommand.h b/TileDecayCommand.h new file mode 100644 index 0000000..6bb72de --- /dev/null +++ b/TileDecayCommand.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Command.h" + +class TileDecayCommand : public Command { + using Command::Command; + +public: + virtual void execute(unsigned int x, unsigned int y); +}; + diff --git a/ViewController.cpp b/ViewController.cpp index a23283d..303d5b9 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -5,6 +5,7 @@ #include "MouseCode.h" #include "MuseumPauseCommand.h" #include "OpenFileGUICommand.h" +#include "TileDecayCommand.h" #include "View.h" #include "Museum.h" @@ -66,6 +67,7 @@ void ViewController::ev_keydown(KeyboardCode key) { break; } case KEY_ENTER: { + TileDecayCommand(this->cmd_base).execute(get<0>(this->mouse_pos), get<1>(this->mouse_pos)); break; } case KEY_O: { @@ -93,6 +95,7 @@ void ViewController::ev_mousedown(MouseCode button) { try { switch (button) { case MOUSE_LEFT: { + TileDecayCommand(this->cmd_base).execute(get<0>(this->mouse_pos), get<1>(this->mouse_pos)); break; } default: break; |