From d8289105193707daede1a5b59137f18e20f20aeb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 18 Oct 2024 15:48:14 +0200 Subject: (2/2) rename --- ArtistVisibilityCommand.cpp | 13 -------- ArtistVisibilityCommand.h | 12 -------- BlueTileBehavior.cpp | 44 --------------------------- BlueTileBehavior.h | 22 -------------- CMakeLists.txt | 30 +++++++++---------- CreateArtistTileBehavior.cpp | 46 ++++++++++++++++++++++++++++ CreateArtistTileBehavior.h | 19 ++++++++++++ DeleteArtistTileBehavior.cpp | 28 +++++++++++++++++ DeleteArtistTileBehavior.h | 19 ++++++++++++ Deserializer.cpp | 22 -------------- Deserializer.h | 23 -------------- FileReader.cpp | 35 +++------------------- FileReader.h | 32 ++++++++++---------- FileReaderFactory.cpp | 36 ++++++++++++++++++++++ FileReaderFactory.h | 27 +++++++++++++++++ FileStrategy.cpp | 9 ------ FileStrategy.h | 25 ---------------- GrayTileBehavior.cpp | 21 ------------- GrayTileBehavior.h | 17 ----------- HTTPFile.cpp | 26 ---------------- HTTPFile.h | 29 ------------------ HTTPFileReader.cpp | 26 ++++++++++++++++ HTTPFileReader.h | 29 ++++++++++++++++++ LocalFile.cpp | 63 --------------------------------------- LocalFile.h | 30 ------------------- LocalFileReader.cpp | 63 +++++++++++++++++++++++++++++++++++++++ LocalFileReader.h | 30 +++++++++++++++++++ MuseumDeserializer.cpp | 22 ++++++++++++++ MuseumDeserializer.h | 23 ++++++++++++++ MuseumPauseCommand.cpp | 13 -------- MuseumPauseCommand.h | 12 -------- Parser.cpp | 32 -------------------- Parser.h | 15 ++-------- ParserFactory.cpp | 32 ++++++++++++++++++++ ParserFactory.h | 20 +++++++++++++ ParserStrategy.h | 11 ------- RedTileBehavior.cpp | 28 ----------------- RedTileBehavior.h | 19 ------------ SetNeighborTileBehavior.cpp | 44 +++++++++++++++++++++++++++ SetNeighborTileBehavior.h | 22 ++++++++++++++ StepTileBehavior.cpp | 21 +++++++++++++ StepTileBehavior.h | 17 +++++++++++ StepTileCommand.cpp | 12 ++++++++ StepTileCommand.h | 11 +++++++ TileAppearance.cpp | 19 ------------ TileAppearance.h | 27 ----------------- TileBehavior.cpp | 19 ++++-------- TileBehavior.h | 27 +++++++++-------- TileBehaviorFactory.cpp | 20 +++++++++++++ TileBehaviorFactory.h | 21 +++++++++++++ TileBehaviorStrategy.cpp | 11 ------- TileBehaviorStrategy.h | 24 --------------- TileColorFactory.cpp | 19 ++++++++++++ TileColorFactory.h | 27 +++++++++++++++++ TileDecayCommand.cpp | 12 -------- TileDecayCommand.h | 11 ------- ToggleArtistVisibilityCommand.cpp | 13 ++++++++ ToggleArtistVisibilityCommand.h | 12 ++++++++ ToggleMuseumPauseCommand.cpp | 13 ++++++++ ToggleMuseumPauseCommand.h | 12 ++++++++ YellowTileBehavior.cpp | 46 ---------------------------- YellowTileBehavior.h | 19 ------------ 62 files changed, 741 insertions(+), 741 deletions(-) delete mode 100644 ArtistVisibilityCommand.cpp delete mode 100644 ArtistVisibilityCommand.h delete mode 100644 BlueTileBehavior.cpp delete mode 100644 BlueTileBehavior.h create mode 100644 CreateArtistTileBehavior.cpp create mode 100644 CreateArtistTileBehavior.h create mode 100644 DeleteArtistTileBehavior.cpp create mode 100644 DeleteArtistTileBehavior.h delete mode 100644 Deserializer.cpp delete mode 100644 Deserializer.h create mode 100644 FileReaderFactory.cpp create mode 100644 FileReaderFactory.h delete mode 100644 FileStrategy.cpp delete mode 100644 FileStrategy.h delete mode 100644 GrayTileBehavior.cpp delete mode 100644 GrayTileBehavior.h delete mode 100644 HTTPFile.cpp delete mode 100644 HTTPFile.h create mode 100644 HTTPFileReader.cpp create mode 100644 HTTPFileReader.h delete mode 100644 LocalFile.cpp delete mode 100644 LocalFile.h create mode 100644 LocalFileReader.cpp create mode 100644 LocalFileReader.h create mode 100644 MuseumDeserializer.cpp create mode 100644 MuseumDeserializer.h delete mode 100644 MuseumPauseCommand.cpp delete mode 100644 MuseumPauseCommand.h delete mode 100644 Parser.cpp create mode 100644 ParserFactory.cpp create mode 100644 ParserFactory.h delete mode 100644 ParserStrategy.h delete mode 100644 RedTileBehavior.cpp delete mode 100644 RedTileBehavior.h create mode 100644 SetNeighborTileBehavior.cpp create mode 100644 SetNeighborTileBehavior.h create mode 100644 StepTileBehavior.cpp create mode 100644 StepTileBehavior.h create mode 100644 StepTileCommand.cpp create mode 100644 StepTileCommand.h delete mode 100644 TileAppearance.cpp delete mode 100644 TileAppearance.h create mode 100644 TileBehaviorFactory.cpp create mode 100644 TileBehaviorFactory.h delete mode 100644 TileBehaviorStrategy.cpp delete mode 100644 TileBehaviorStrategy.h create mode 100644 TileColorFactory.cpp create mode 100644 TileColorFactory.h delete mode 100644 TileDecayCommand.cpp delete mode 100644 TileDecayCommand.h create mode 100644 ToggleArtistVisibilityCommand.cpp create mode 100644 ToggleArtistVisibilityCommand.h create mode 100644 ToggleMuseumPauseCommand.cpp create mode 100644 ToggleMuseumPauseCommand.h delete mode 100644 YellowTileBehavior.cpp delete mode 100644 YellowTileBehavior.h diff --git a/ArtistVisibilityCommand.cpp b/ArtistVisibilityCommand.cpp deleted file mode 100644 index 6c22dbc..0000000 --- a/ArtistVisibilityCommand.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "ToggleArtistVisibilityCommand.h" -#include "ViewController.h" - -void ToggleArtistVisibilityCommand::set(bool visible) { - ViewController & controller = this->get_controller(); - controller.set_artists_visible(visible); -} - -void ToggleArtistVisibilityCommand::toggle() { - ViewController & controller = this->get_controller(); - controller.set_artists_visible(!controller.get_artists_visible()); -} - diff --git a/ArtistVisibilityCommand.h b/ArtistVisibilityCommand.h deleted file mode 100644 index 47e2158..0000000 --- a/ArtistVisibilityCommand.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "Command.h" - -class ToggleArtistVisibilityCommand : public Command { - using Command::Command; - -public: - virtual void toggle(); - virtual void set(bool visible); -}; - diff --git a/BlueTileBehavior.cpp b/BlueTileBehavior.cpp deleted file mode 100644 index d934d4b..0000000 --- a/BlueTileBehavior.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#include "SetNeighborTileBehavior.h" -#include "CreateArtistTileBehavior.h" -#include "Artist.h" -#include "Tile.h" - -using namespace std; - -SetNeighborTileBehavior SetNeighborTileBehavior::instance { SetNeighborTileBehavior::type }; - -void SetNeighborTileBehavior::step(Artist * artist) { - this->interactions++; - if (dx != 0 || dy != 0) return; - if (artist == nullptr) return; - - if (artist->data.vx == 0) dx = 1; - if (artist->data.vy == 0) dy = 1; -} - -static void 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; - - neighbor->set_type(SetNeighborTileBehavior::type); -} - -void SetNeighborTileBehavior::update(Tile & tile) { - if (this->interactions < 1) return; - - update_neighbor(tile, this->dx, this->dy); - update_neighbor(tile, -this->dx, -this->dy); - - tile.set_type(CreateArtistTileBehavior::type); -} - -unique_ptr SetNeighborTileBehavior::clone(Museum & museum) { - auto instance = new SetNeighborTileBehavior(); - instance->museum = &museum; - return unique_ptr(instance); -} - diff --git a/BlueTileBehavior.h b/BlueTileBehavior.h deleted file mode 100644 index d316420..0000000 --- a/BlueTileBehavior.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "TileBehavior.h" - -class SetNeighborTileBehavior : public TileBehavior { -public: - virtual void step(Artist *); - virtual void update(Tile &); - virtual std::unique_ptr clone(Museum &); - - static constexpr const char * type = "B"; - -private: - using TileBehavior::TileBehavior; - static SetNeighborTileBehavior instance; - SetNeighborTileBehavior() = default; - -private: - int dx = 0; - int dy = 0; -}; - diff --git a/CMakeLists.txt b/CMakeLists.txt index a74a6b3..c946384 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,26 +17,26 @@ project(main C CXX) add_executable(main main.cpp util.cpp - FileStrategy.cpp - HTTPFile.cpp - LocalFile.cpp - Exception.cpp FileReader.cpp + HTTPFileReader.cpp + LocalFileReader.cpp + Exception.cpp + FileReaderFactory.cpp Canvas.cpp - Parser.cpp + ParserFactory.cpp CSVParser.cpp XMLParser.cpp # TXTParser.cpp - Deserializer.cpp + MuseumDeserializer.cpp Tile.cpp - TileAppearance.cpp - TileBehaviorStrategy.cpp + TileColorFactory.cpp TileBehavior.cpp + TileBehaviorFactory.cpp NullTileBehavior.cpp - YellowTileBehavior.cpp - BlueTileBehavior.cpp - RedTileBehavior.cpp - GrayTileBehavior.cpp + CreateArtistTileBehavior.cpp + SetNeighborTileBehavior.cpp + DeleteArtistTileBehavior.cpp + StepTileBehavior.cpp ViewController.cpp View.cpp Museum.cpp @@ -45,9 +45,9 @@ add_executable(main Command.cpp OpenFileGUICommand.cpp LoadFilesCommand.cpp - MuseumPauseCommand.cpp - ArtistVisibilityCommand.cpp - TileDecayCommand.cpp + ToggleMuseumPauseCommand.cpp + ToggleArtistVisibilityCommand.cpp + StepTileCommand.cpp TimeTravelCommand.cpp ) diff --git a/CreateArtistTileBehavior.cpp b/CreateArtistTileBehavior.cpp new file mode 100644 index 0000000..ae0cd7c --- /dev/null +++ b/CreateArtistTileBehavior.cpp @@ -0,0 +1,46 @@ +#include +#include + +#include "CreateArtistTileBehavior.h" +#include "StepTileBehavior.h" +#include "Tile.h" +#include "Museum.h" + +using std::uniform_int_distribution; +using std::uniform_real_distribution; +using std::random_device; +using std::mt19937; +using namespace std; + +CreateArtistTileBehavior CreateArtistTileBehavior::instance { CreateArtistTileBehavior::type }; + +random_device dev{}; +mt19937 rng(dev()); +uniform_int_distribution random_bool(0, 1); +uniform_real_distribution random_float(-1, 1); + +void CreateArtistTileBehavior::update(Tile & tile) { + unsigned int new_artists = this->interactions - this->last_interactions; + this->last_interactions = this->interactions; + + for (unsigned int i = 0; i < new_artists; i++) { + if (i >= 2) break; + ArtistData new_data = { + .x = static_cast(tile.data.x), + .y = static_cast(tile.data.y), + }; + float velocity = random_float(rng); + random_bool(rng) ? new_data.vx = velocity : new_data.vy = velocity; + this->museum->people.add_artist(new_data); + } + + if (this->interactions < 2) return; + tile.set_type(StepTileBehavior::type); +} + +unique_ptr CreateArtistTileBehavior::clone(Museum & museum) { + auto instance = new CreateArtistTileBehavior(); + instance->museum = &museum; + return unique_ptr(instance); +} + diff --git a/CreateArtistTileBehavior.h b/CreateArtistTileBehavior.h new file mode 100644 index 0000000..6abd67f --- /dev/null +++ b/CreateArtistTileBehavior.h @@ -0,0 +1,19 @@ +#pragma once + +#include "TileBehavior.h" + +class CreateArtistTileBehavior : public TileBehavior { +public: + virtual void update(Tile &); + virtual std::unique_ptr clone(Museum &); + + static constexpr const char * type = "Y"; + +private: + using TileBehavior::TileBehavior; + static CreateArtistTileBehavior instance; + CreateArtistTileBehavior() = default; + + unsigned int last_interactions = 0; +}; + diff --git a/DeleteArtistTileBehavior.cpp b/DeleteArtistTileBehavior.cpp new file mode 100644 index 0000000..091a251 --- /dev/null +++ b/DeleteArtistTileBehavior.cpp @@ -0,0 +1,28 @@ +#include + +#include "DeleteArtistTileBehavior.h" +#include "SetNeighborTileBehavior.h" +#include "Tile.h" +#include "Museum.h" + +using namespace std; + +DeleteArtistTileBehavior DeleteArtistTileBehavior::instance { DeleteArtistTileBehavior::type }; + +void DeleteArtistTileBehavior::step(Artist * artist) { + this->interactions++; + if (artist != nullptr) + this->museum->people.remove_artist(*artist); +} + +void DeleteArtistTileBehavior::update(Tile & tile) { + if (this->interactions == 0) return; + tile.set_type(SetNeighborTileBehavior::type); +} + +unique_ptr DeleteArtistTileBehavior::clone(Museum & museum) { + auto instance = new DeleteArtistTileBehavior(); + instance->museum = &museum; + return unique_ptr(instance); +} + diff --git a/DeleteArtistTileBehavior.h b/DeleteArtistTileBehavior.h new file mode 100644 index 0000000..0b2b0d4 --- /dev/null +++ b/DeleteArtistTileBehavior.h @@ -0,0 +1,19 @@ +#pragma once + +#include "TileBehavior.h" + +class DeleteArtistTileBehavior : public TileBehavior { +public: + virtual void step(Artist *); + virtual void update(Tile &); + virtual std::unique_ptr clone(Museum &); + + static constexpr const char * type = "R"; + +private: + using TileBehavior::TileBehavior; + static DeleteArtistTileBehavior instance; + DeleteArtistTileBehavior() = default; +}; + + diff --git a/Deserializer.cpp b/Deserializer.cpp deleted file mode 100644 index fca8d50..0000000 --- a/Deserializer.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "MuseumDeserializer.h" -#include "TileColorFactory.h" - -MuseumDeserializer::MuseumDeserializer(Museum & museum) : museum(museum) { } - -void MuseumDeserializer::add_artist(ArtistData data) { - this->museum.people.add_artist(data); -} - -void MuseumDeserializer::set_canvas(CanvasData data) { - this->museum.canvas.set_data(data); -} - -void MuseumDeserializer::set_tile(TileData data) { - this->museum.canvas.set_tile(data); -} - -void MuseumDeserializer::add_type(std::string type, Color color, unsigned int weight) { - if (type.length() == 0) return; - TileColorFactory::register_color(type, color); -} - diff --git a/Deserializer.h b/Deserializer.h deleted file mode 100644 index 7fecf56..0000000 --- a/Deserializer.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "Museum.h" -#include "ArtistData.h" -#include "TileData.h" -#include "CanvasData.h" -#include "Color.h" - -class MuseumDeserializer { -public: - MuseumDeserializer(Museum & m); - -public: - void add_type(std::string type, Color color, unsigned int weight); - - void set_canvas(CanvasData); - void set_tile(TileData); - void add_artist(ArtistData); - -private: - Museum & museum; -}; - diff --git a/FileReader.cpp b/FileReader.cpp index 4b72b11..b76de50 100644 --- a/FileReader.cpp +++ b/FileReader.cpp @@ -1,36 +1,9 @@ -#include +#include -#include "FileReaderFactory.h" #include "FileReader.h" +#include "FileReaderFactory.h" -using namespace std; - -unique_ptr FileReaderFactory::open(const std::string url) { - FileReader * reader = find_reader(url)->clone(); - reader->open(url); - return unique_ptr(reader); -} - -void FileReaderFactory::register_strategy(const std::string type, const FileReader * node) { - static FactoryMap & map = get_map(); - map[type] = node; -} - -FactoryMap & FileReaderFactory::get_map() { - static FactoryMap map; - return map; -} - -const FileReader * FileReaderFactory::find_reader(const std::string type) { - static FactoryMap & map = get_map(); - - // try to find protocol by prefix - for (auto item : map) { - if (!type.starts_with(item.first)) continue; - return item.second; - } - - // fallback is local file - return map.find("file://")->second; +FileReader::FileReader(const std::string protocol) { + FileReaderFactory::register_strategy(protocol, this); } diff --git a/FileReader.h b/FileReader.h index 0829985..f896ec8 100644 --- a/FileReader.h +++ b/FileReader.h @@ -1,27 +1,25 @@ #pragma once -#include -#include -#include - -#include "FileReader.h" +class FileReaderFactory; -using FactoryMap = std::map; +#include -class FileReaderFactory { +class FileReader { +protected: + virtual void open(const std::string url) = 0; public: - static std::unique_ptr open(const std::string url); + virtual void close() = 0; + virtual const std::string read() = 0; -private: - FileReaderFactory() = default; - virtual ~FileReaderFactory() = default; +public: + virtual ~FileReader() = default; -private: - static void register_strategy(const std::string type, const FileReader * node); - static FactoryMap & get_map(); - static const FileReader * find_reader(const std::string type); +protected: + FileReader() = default; + virtual FileReader * clone() const = 0; -private: - friend FileReader; +protected: + FileReader(const std::string protocol); + friend FileReaderFactory; }; diff --git a/FileReaderFactory.cpp b/FileReaderFactory.cpp new file mode 100644 index 0000000..4b72b11 --- /dev/null +++ b/FileReaderFactory.cpp @@ -0,0 +1,36 @@ +#include + +#include "FileReaderFactory.h" +#include "FileReader.h" + +using namespace std; + +unique_ptr FileReaderFactory::open(const std::string url) { + FileReader * reader = find_reader(url)->clone(); + reader->open(url); + return unique_ptr(reader); +} + +void FileReaderFactory::register_strategy(const std::string type, const FileReader * node) { + static FactoryMap & map = get_map(); + map[type] = node; +} + +FactoryMap & FileReaderFactory::get_map() { + static FactoryMap map; + return map; +} + +const FileReader * FileReaderFactory::find_reader(const std::string type) { + static FactoryMap & map = get_map(); + + // try to find protocol by prefix + for (auto item : map) { + if (!type.starts_with(item.first)) continue; + return item.second; + } + + // fallback is local file + return map.find("file://")->second; +} + diff --git a/FileReaderFactory.h b/FileReaderFactory.h new file mode 100644 index 0000000..0829985 --- /dev/null +++ b/FileReaderFactory.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +#include "FileReader.h" + +using FactoryMap = std::map; + +class FileReaderFactory { +public: + static std::unique_ptr open(const std::string url); + +private: + FileReaderFactory() = default; + virtual ~FileReaderFactory() = default; + +private: + static void register_strategy(const std::string type, const FileReader * node); + static FactoryMap & get_map(); + static const FileReader * find_reader(const std::string type); + +private: + friend FileReader; +}; + diff --git a/FileStrategy.cpp b/FileStrategy.cpp deleted file mode 100644 index b76de50..0000000 --- a/FileStrategy.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -#include "FileReader.h" -#include "FileReaderFactory.h" - -FileReader::FileReader(const std::string protocol) { - FileReaderFactory::register_strategy(protocol, this); -} - diff --git a/FileStrategy.h b/FileStrategy.h deleted file mode 100644 index f896ec8..0000000 --- a/FileStrategy.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -class FileReaderFactory; - -#include - -class FileReader { -protected: - virtual void open(const std::string url) = 0; -public: - virtual void close() = 0; - virtual const std::string read() = 0; - -public: - virtual ~FileReader() = default; - -protected: - FileReader() = default; - virtual FileReader * clone() const = 0; - -protected: - FileReader(const std::string protocol); - friend FileReaderFactory; -}; - diff --git a/GrayTileBehavior.cpp b/GrayTileBehavior.cpp deleted file mode 100644 index 2023bb8..0000000 --- a/GrayTileBehavior.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -#include "StepTileBehavior.h" -#include "DeleteArtistTileBehavior.h" -#include "Tile.h" - -using namespace std; - -StepTileBehavior StepTileBehavior::instance { StepTileBehavior::type }; - -void StepTileBehavior::update(Tile & tile) { - if (this->interactions < 3) return; - tile.set_type(DeleteArtistTileBehavior::type); -} - -unique_ptr StepTileBehavior::clone(Museum & museum) { - auto instance = new StepTileBehavior(); - instance->museum = &museum; - return unique_ptr(instance); -} - diff --git a/GrayTileBehavior.h b/GrayTileBehavior.h deleted file mode 100644 index d044fe7..0000000 --- a/GrayTileBehavior.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "TileBehavior.h" - -class StepTileBehavior : public TileBehavior { -public: - virtual void update(Tile &); - virtual std::unique_ptr clone(Museum &); - - static constexpr const char * type = "G"; - -private: - using TileBehavior::TileBehavior; - static StepTileBehavior instance; - StepTileBehavior() = default; -}; - diff --git a/HTTPFile.cpp b/HTTPFile.cpp deleted file mode 100644 index d7f5dd4..0000000 --- a/HTTPFile.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include "HTTPFileReader.h" - -HTTPFileReader HTTPFileReader::instance(protocol); - -void HTTPFileReader::open(const std::string url) { - _res = cpr::Get(cpr::Url{url}); -} - -void HTTPFileReader::close() { } - -const std::string HTTPFileReader::read() { - return _res.text.data(); -} - -HTTPFileReader::~HTTPFileReader() { - this->close(); -} - -HTTPFileReader * HTTPFileReader::clone() const { - return new HTTPFileReader(this); -} - -HTTPFileReader::HTTPFileReader(const HTTPFileReader *) : FileReader() { } - diff --git a/HTTPFile.h b/HTTPFile.h deleted file mode 100644 index 4b05e18..0000000 --- a/HTTPFile.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -#include "FileReader.h" - -class HTTPFileReader : FileReader { -protected: - virtual void open(const std::string url); -public: - virtual void close(); - virtual const std::string read(); - -public: - virtual ~HTTPFileReader(); - -private: - HTTPFileReader(const HTTPFileReader *); - virtual HTTPFileReader * clone() const; - -private: - using FileReader::FileReader; - constexpr static const std::string protocol = "https://"; - static HTTPFileReader instance; - -private: - cpr::Response _res; -}; - diff --git a/HTTPFileReader.cpp b/HTTPFileReader.cpp new file mode 100644 index 0000000..d7f5dd4 --- /dev/null +++ b/HTTPFileReader.cpp @@ -0,0 +1,26 @@ +#include + +#include "HTTPFileReader.h" + +HTTPFileReader HTTPFileReader::instance(protocol); + +void HTTPFileReader::open(const std::string url) { + _res = cpr::Get(cpr::Url{url}); +} + +void HTTPFileReader::close() { } + +const std::string HTTPFileReader::read() { + return _res.text.data(); +} + +HTTPFileReader::~HTTPFileReader() { + this->close(); +} + +HTTPFileReader * HTTPFileReader::clone() const { + return new HTTPFileReader(this); +} + +HTTPFileReader::HTTPFileReader(const HTTPFileReader *) : FileReader() { } + diff --git a/HTTPFileReader.h b/HTTPFileReader.h new file mode 100644 index 0000000..4b05e18 --- /dev/null +++ b/HTTPFileReader.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "FileReader.h" + +class HTTPFileReader : FileReader { +protected: + virtual void open(const std::string url); +public: + virtual void close(); + virtual const std::string read(); + +public: + virtual ~HTTPFileReader(); + +private: + HTTPFileReader(const HTTPFileReader *); + virtual HTTPFileReader * clone() const; + +private: + using FileReader::FileReader; + constexpr static const std::string protocol = "https://"; + static HTTPFileReader instance; + +private: + cpr::Response _res; +}; + diff --git a/LocalFile.cpp b/LocalFile.cpp deleted file mode 100644 index 1f9d574..0000000 --- a/LocalFile.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include - -#include "LocalFileReader.h" -#include "Exception.h" - -LocalFileReader LocalFileReader::instance(protocol); - -void LocalFileReader::open(const std::string url) { - std::string path = url; - if (path.starts_with(protocol)) - path = path.substr(protocol.size()); - - this->file = new std::ifstream(path, std::ios::in); - if (this->file->fail() || !this->file->is_open()) - throw Exception("cannot open file://%s\n", path.c_str()); -} - -void LocalFileReader::close() { - if (this->file == nullptr) return; - - if (this->file->is_open()) - this->file->close(); -} - -const std::string LocalFileReader::read() { - if (this->content != nullptr) - return *this->content; - - if (this->file == nullptr) - throw Exception("FileReader read after destructor\n"); - - if (!this->file->is_open()) - throw Exception("FileReader read after close\n"); - - this->content = new std::string( - std::istreambuf_iterator(*this->file), - std::istreambuf_iterator() - ); - - return *this->content; -} - -LocalFileReader::~LocalFileReader() { - this->close(); - - if (this->file != nullptr) { - delete this->file; - this->file = nullptr; - } - - if (this->content != nullptr) { - delete this->content; - this->content = nullptr; - } -} - -LocalFileReader * LocalFileReader::clone() const { - return new LocalFileReader(this); -} - -LocalFileReader::LocalFileReader(const LocalFileReader *) : FileReader() { } - diff --git a/LocalFile.h b/LocalFile.h deleted file mode 100644 index 6ea4f84..0000000 --- a/LocalFile.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include - -#include "FileReader.h" - -class LocalFileReader : FileReader { -protected: - virtual void open(const std::string url); -public: - virtual void close(); - virtual const std::string read(); - -public: - virtual ~LocalFileReader(); - -private: - LocalFileReader(const LocalFileReader *); - virtual LocalFileReader * clone() const; - -private: - using FileReader::FileReader; - constexpr static const std::string protocol = "file://"; - static LocalFileReader instance; - -private: - std::ifstream * file = nullptr; - std::string * content = nullptr; -}; - diff --git a/LocalFileReader.cpp b/LocalFileReader.cpp new file mode 100644 index 0000000..1f9d574 --- /dev/null +++ b/LocalFileReader.cpp @@ -0,0 +1,63 @@ +#include +#include + +#include "LocalFileReader.h" +#include "Exception.h" + +LocalFileReader LocalFileReader::instance(protocol); + +void LocalFileReader::open(const std::string url) { + std::string path = url; + if (path.starts_with(protocol)) + path = path.substr(protocol.size()); + + this->file = new std::ifstream(path, std::ios::in); + if (this->file->fail() || !this->file->is_open()) + throw Exception("cannot open file://%s\n", path.c_str()); +} + +void LocalFileReader::close() { + if (this->file == nullptr) return; + + if (this->file->is_open()) + this->file->close(); +} + +const std::string LocalFileReader::read() { + if (this->content != nullptr) + return *this->content; + + if (this->file == nullptr) + throw Exception("FileReader read after destructor\n"); + + if (!this->file->is_open()) + throw Exception("FileReader read after close\n"); + + this->content = new std::string( + std::istreambuf_iterator(*this->file), + std::istreambuf_iterator() + ); + + return *this->content; +} + +LocalFileReader::~LocalFileReader() { + this->close(); + + if (this->file != nullptr) { + delete this->file; + this->file = nullptr; + } + + if (this->content != nullptr) { + delete this->content; + this->content = nullptr; + } +} + +LocalFileReader * LocalFileReader::clone() const { + return new LocalFileReader(this); +} + +LocalFileReader::LocalFileReader(const LocalFileReader *) : FileReader() { } + diff --git a/LocalFileReader.h b/LocalFileReader.h new file mode 100644 index 0000000..6ea4f84 --- /dev/null +++ b/LocalFileReader.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "FileReader.h" + +class LocalFileReader : FileReader { +protected: + virtual void open(const std::string url); +public: + virtual void close(); + virtual const std::string read(); + +public: + virtual ~LocalFileReader(); + +private: + LocalFileReader(const LocalFileReader *); + virtual LocalFileReader * clone() const; + +private: + using FileReader::FileReader; + constexpr static const std::string protocol = "file://"; + static LocalFileReader instance; + +private: + std::ifstream * file = nullptr; + std::string * content = nullptr; +}; + diff --git a/MuseumDeserializer.cpp b/MuseumDeserializer.cpp new file mode 100644 index 0000000..fca8d50 --- /dev/null +++ b/MuseumDeserializer.cpp @@ -0,0 +1,22 @@ +#include "MuseumDeserializer.h" +#include "TileColorFactory.h" + +MuseumDeserializer::MuseumDeserializer(Museum & museum) : museum(museum) { } + +void MuseumDeserializer::add_artist(ArtistData data) { + this->museum.people.add_artist(data); +} + +void MuseumDeserializer::set_canvas(CanvasData data) { + this->museum.canvas.set_data(data); +} + +void MuseumDeserializer::set_tile(TileData data) { + this->museum.canvas.set_tile(data); +} + +void MuseumDeserializer::add_type(std::string type, Color color, unsigned int weight) { + if (type.length() == 0) return; + TileColorFactory::register_color(type, color); +} + diff --git a/MuseumDeserializer.h b/MuseumDeserializer.h new file mode 100644 index 0000000..7fecf56 --- /dev/null +++ b/MuseumDeserializer.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Museum.h" +#include "ArtistData.h" +#include "TileData.h" +#include "CanvasData.h" +#include "Color.h" + +class MuseumDeserializer { +public: + MuseumDeserializer(Museum & m); + +public: + void add_type(std::string type, Color color, unsigned int weight); + + void set_canvas(CanvasData); + void set_tile(TileData); + void add_artist(ArtistData); + +private: + Museum & museum; +}; + diff --git a/MuseumPauseCommand.cpp b/MuseumPauseCommand.cpp deleted file mode 100644 index 57176af..0000000 --- a/MuseumPauseCommand.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "ToggleMuseumPauseCommand.h" -#include "Museum.h" - -void ToggleMuseumPauseCommand::set(bool paused) { - Museum & museum = this->get_museum(); - museum.set_pause(paused); -} - -void ToggleMuseumPauseCommand::toggle() { - Museum & museum = this->get_museum(); - museum.set_pause(!museum.get_pause()); -} - diff --git a/MuseumPauseCommand.h b/MuseumPauseCommand.h deleted file mode 100644 index 15d1388..0000000 --- a/MuseumPauseCommand.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "Command.h" - -class ToggleMuseumPauseCommand : public Command { - using Command::Command; - -public: - virtual void toggle(); - virtual void set(bool paused); -}; - diff --git a/Parser.cpp b/Parser.cpp deleted file mode 100644 index 49c4d00..0000000 --- a/Parser.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include - -#include "ParserFactory.h" -#include "Exception.h" - -void ParserFactory::parse(FileReader & file, MuseumDeserializer & deserializer) { - auto & col = ParserFactory::get_collection(); - if (col.size() < 1) - throw Exception("no parsers registered"); - - unsigned int best_score = 0; - Parser * best_strategy = nullptr; - for (Parser * strategy : col) { - unsigned int score = strategy->heuristic(file); - if (score <= best_score) continue; - - best_score = score; - best_strategy = strategy; - } - - if (best_strategy == nullptr) - throw Exception("unknown file type"); - - best_strategy->parse(file, deserializer); -} - -void ParserFactory::register_strategy(Parser * ps) { - auto & col = ParserFactory::get_collection(); - if (std::find(col.begin(), col.end(), ps) != col.end()) return; - col.push_back(ps); -} - diff --git a/Parser.h b/Parser.h index 5430366..01efb3c 100644 --- a/Parser.h +++ b/Parser.h @@ -2,19 +2,10 @@ #include "FileReader.h" #include "MuseumDeserializer.h" -#include "Parser.h" - -class ParserFactory { - typedef std::vector ParserCollection; +class Parser { public: - static void parse(FileReader & f, MuseumDeserializer & d); - static void register_strategy(Parser * p); - -private: - static ParserCollection & get_collection() { - static ParserCollection c = {}; - return c; - } + virtual void parse(FileReader & f, MuseumDeserializer & d) = 0; + virtual unsigned int heuristic(FileReader & f) = 0; }; diff --git a/ParserFactory.cpp b/ParserFactory.cpp new file mode 100644 index 0000000..49c4d00 --- /dev/null +++ b/ParserFactory.cpp @@ -0,0 +1,32 @@ +#include + +#include "ParserFactory.h" +#include "Exception.h" + +void ParserFactory::parse(FileReader & file, MuseumDeserializer & deserializer) { + auto & col = ParserFactory::get_collection(); + if (col.size() < 1) + throw Exception("no parsers registered"); + + unsigned int best_score = 0; + Parser * best_strategy = nullptr; + for (Parser * strategy : col) { + unsigned int score = strategy->heuristic(file); + if (score <= best_score) continue; + + best_score = score; + best_strategy = strategy; + } + + if (best_strategy == nullptr) + throw Exception("unknown file type"); + + best_strategy->parse(file, deserializer); +} + +void ParserFactory::register_strategy(Parser * ps) { + auto & col = ParserFactory::get_collection(); + if (std::find(col.begin(), col.end(), ps) != col.end()) return; + col.push_back(ps); +} + diff --git a/ParserFactory.h b/ParserFactory.h new file mode 100644 index 0000000..5430366 --- /dev/null +++ b/ParserFactory.h @@ -0,0 +1,20 @@ +#pragma once + +#include "FileReader.h" +#include "MuseumDeserializer.h" +#include "Parser.h" + +class ParserFactory { + typedef std::vector ParserCollection; + +public: + static void parse(FileReader & f, MuseumDeserializer & d); + static void register_strategy(Parser * p); + +private: + static ParserCollection & get_collection() { + static ParserCollection c = {}; + return c; + } +}; + diff --git a/ParserStrategy.h b/ParserStrategy.h deleted file mode 100644 index 01efb3c..0000000 --- a/ParserStrategy.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "FileReader.h" -#include "MuseumDeserializer.h" - -class Parser { -public: - virtual void parse(FileReader & f, MuseumDeserializer & d) = 0; - virtual unsigned int heuristic(FileReader & f) = 0; -}; - diff --git a/RedTileBehavior.cpp b/RedTileBehavior.cpp deleted file mode 100644 index 091a251..0000000 --- a/RedTileBehavior.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -#include "DeleteArtistTileBehavior.h" -#include "SetNeighborTileBehavior.h" -#include "Tile.h" -#include "Museum.h" - -using namespace std; - -DeleteArtistTileBehavior DeleteArtistTileBehavior::instance { DeleteArtistTileBehavior::type }; - -void DeleteArtistTileBehavior::step(Artist * artist) { - this->interactions++; - if (artist != nullptr) - this->museum->people.remove_artist(*artist); -} - -void DeleteArtistTileBehavior::update(Tile & tile) { - if (this->interactions == 0) return; - tile.set_type(SetNeighborTileBehavior::type); -} - -unique_ptr DeleteArtistTileBehavior::clone(Museum & museum) { - auto instance = new DeleteArtistTileBehavior(); - instance->museum = &museum; - return unique_ptr(instance); -} - diff --git a/RedTileBehavior.h b/RedTileBehavior.h deleted file mode 100644 index 0b2b0d4..0000000 --- a/RedTileBehavior.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "TileBehavior.h" - -class DeleteArtistTileBehavior : public TileBehavior { -public: - virtual void step(Artist *); - virtual void update(Tile &); - virtual std::unique_ptr clone(Museum &); - - static constexpr const char * type = "R"; - -private: - using TileBehavior::TileBehavior; - static DeleteArtistTileBehavior instance; - DeleteArtistTileBehavior() = default; -}; - - diff --git a/SetNeighborTileBehavior.cpp b/SetNeighborTileBehavior.cpp new file mode 100644 index 0000000..d934d4b --- /dev/null +++ b/SetNeighborTileBehavior.cpp @@ -0,0 +1,44 @@ +#include + +#include "SetNeighborTileBehavior.h" +#include "CreateArtistTileBehavior.h" +#include "Artist.h" +#include "Tile.h" + +using namespace std; + +SetNeighborTileBehavior SetNeighborTileBehavior::instance { SetNeighborTileBehavior::type }; + +void SetNeighborTileBehavior::step(Artist * artist) { + this->interactions++; + if (dx != 0 || dy != 0) return; + if (artist == nullptr) return; + + if (artist->data.vx == 0) dx = 1; + if (artist->data.vy == 0) dy = 1; +} + +static void 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; + + neighbor->set_type(SetNeighborTileBehavior::type); +} + +void SetNeighborTileBehavior::update(Tile & tile) { + if (this->interactions < 1) return; + + update_neighbor(tile, this->dx, this->dy); + update_neighbor(tile, -this->dx, -this->dy); + + tile.set_type(CreateArtistTileBehavior::type); +} + +unique_ptr SetNeighborTileBehavior::clone(Museum & museum) { + auto instance = new SetNeighborTileBehavior(); + instance->museum = &museum; + return unique_ptr(instance); +} + diff --git a/SetNeighborTileBehavior.h b/SetNeighborTileBehavior.h new file mode 100644 index 0000000..d316420 --- /dev/null +++ b/SetNeighborTileBehavior.h @@ -0,0 +1,22 @@ +#pragma once + +#include "TileBehavior.h" + +class SetNeighborTileBehavior : public TileBehavior { +public: + virtual void step(Artist *); + virtual void update(Tile &); + virtual std::unique_ptr clone(Museum &); + + static constexpr const char * type = "B"; + +private: + using TileBehavior::TileBehavior; + static SetNeighborTileBehavior instance; + SetNeighborTileBehavior() = default; + +private: + int dx = 0; + int dy = 0; +}; + diff --git a/StepTileBehavior.cpp b/StepTileBehavior.cpp new file mode 100644 index 0000000..2023bb8 --- /dev/null +++ b/StepTileBehavior.cpp @@ -0,0 +1,21 @@ +#include + +#include "StepTileBehavior.h" +#include "DeleteArtistTileBehavior.h" +#include "Tile.h" + +using namespace std; + +StepTileBehavior StepTileBehavior::instance { StepTileBehavior::type }; + +void StepTileBehavior::update(Tile & tile) { + if (this->interactions < 3) return; + tile.set_type(DeleteArtistTileBehavior::type); +} + +unique_ptr StepTileBehavior::clone(Museum & museum) { + auto instance = new StepTileBehavior(); + instance->museum = &museum; + return unique_ptr(instance); +} + diff --git a/StepTileBehavior.h b/StepTileBehavior.h new file mode 100644 index 0000000..d044fe7 --- /dev/null +++ b/StepTileBehavior.h @@ -0,0 +1,17 @@ +#pragma once + +#include "TileBehavior.h" + +class StepTileBehavior : public TileBehavior { +public: + virtual void update(Tile &); + virtual std::unique_ptr clone(Museum &); + + static constexpr const char * type = "G"; + +private: + using TileBehavior::TileBehavior; + static StepTileBehavior instance; + StepTileBehavior() = default; +}; + diff --git a/StepTileCommand.cpp b/StepTileCommand.cpp new file mode 100644 index 0000000..ff5c6b2 --- /dev/null +++ b/StepTileCommand.cpp @@ -0,0 +1,12 @@ +#include "StepTileCommand.h" +#include "Museum.h" + +void StepTileCommand::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; + Tile & tile = museum.canvas.get_tile(x, y); + tile.step(nullptr); + tile.update(); +} + diff --git a/StepTileCommand.h b/StepTileCommand.h new file mode 100644 index 0000000..ce9dccd --- /dev/null +++ b/StepTileCommand.h @@ -0,0 +1,11 @@ +#pragma once + +#include "Command.h" + +class StepTileCommand : public Command { + using Command::Command; + +public: + virtual void execute(unsigned int x, unsigned int y); +}; + diff --git a/TileAppearance.cpp b/TileAppearance.cpp deleted file mode 100644 index 7c5b080..0000000 --- a/TileAppearance.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "TileColorFactory.h" - -using namespace std; - -Color TileColorFactory::get_color(string type) { - auto & type_map = TileColorFactory::get_collection(); - - if (type_map.contains(type)) - return type_map.at(type); - - return TileColorFactory::default_color; -} - -void TileColorFactory::register_color(string type, Color color) { - auto & type_map = TileColorFactory::get_collection(); - - type_map[type] = color; -} - diff --git a/TileAppearance.h b/TileAppearance.h deleted file mode 100644 index 83dfeed..0000000 --- a/TileAppearance.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include - -#include "Color.h" - -class TileColorFactory { - typedef std::map TileAppearanceCollection; - -public: - static Color get_color(std::string); - static void register_color(std::string, Color); - -private: - static TileAppearanceCollection & get_collection() { - static TileAppearanceCollection c = {}; - return c; - } - - static constexpr Color default_color = { - .red = 0xff, - .green = 0xff, - .blue = 0xff, - }; -}; - diff --git a/TileBehavior.cpp b/TileBehavior.cpp index 99301ac..9de896e 100644 --- a/TileBehavior.cpp +++ b/TileBehavior.cpp @@ -1,20 +1,11 @@ #include "TileBehaviorFactory.h" -#include "Exception.h" +#include "TileBehavior.h" -using namespace std; - -TileBehavior & TileBehaviorFactory::get_strategy(string type) { - auto & type_map = TileBehaviorFactory::get_collection(); - - if (type_map.contains(type)) - return *type_map.at(type); - - throw Exception("unknown behavior for tile type \"%s\"", type.c_str()); +TileBehavior::TileBehavior(const std::string type) { + TileBehaviorFactory::register_strategy(type, this); } -void TileBehaviorFactory::register_strategy(string type, TileBehavior * strategy) { - auto & type_map = TileBehaviorFactory::get_collection(); - - type_map[type] = strategy; +void TileBehavior::step(Artist *) { + this->interactions++; } diff --git a/TileBehavior.h b/TileBehavior.h index 33cd795..b79e7f5 100644 --- a/TileBehavior.h +++ b/TileBehavior.h @@ -1,21 +1,24 @@ #pragma once -#include #include +#include -#include "TileBehavior.h" - -class TileBehaviorFactory { - typedef std::map TileBehaviorCollection; +class Tile; +class Museum; +class Artist; +class TileBehavior { public: - static TileBehavior & get_strategy(std::string); - static void register_strategy(std::string, TileBehavior *); + virtual void step(Artist *); + virtual void update(Tile &) = 0; + virtual std::unique_ptr clone(Museum & m) = 0; + +protected: + TileBehavior(const std::string type); + TileBehavior() = default; -private: - static TileBehaviorCollection & get_collection() { - static TileBehaviorCollection c = {}; - return c; - } +protected: + unsigned int interactions = 0; + Museum * museum = nullptr; }; diff --git a/TileBehaviorFactory.cpp b/TileBehaviorFactory.cpp new file mode 100644 index 0000000..99301ac --- /dev/null +++ b/TileBehaviorFactory.cpp @@ -0,0 +1,20 @@ +#include "TileBehaviorFactory.h" +#include "Exception.h" + +using namespace std; + +TileBehavior & TileBehaviorFactory::get_strategy(string type) { + auto & type_map = TileBehaviorFactory::get_collection(); + + if (type_map.contains(type)) + return *type_map.at(type); + + throw Exception("unknown behavior for tile type \"%s\"", type.c_str()); +} + +void TileBehaviorFactory::register_strategy(string type, TileBehavior * strategy) { + auto & type_map = TileBehaviorFactory::get_collection(); + + type_map[type] = strategy; +} + diff --git a/TileBehaviorFactory.h b/TileBehaviorFactory.h new file mode 100644 index 0000000..33cd795 --- /dev/null +++ b/TileBehaviorFactory.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +#include "TileBehavior.h" + +class TileBehaviorFactory { + typedef std::map TileBehaviorCollection; + +public: + static TileBehavior & get_strategy(std::string); + static void register_strategy(std::string, TileBehavior *); + +private: + static TileBehaviorCollection & get_collection() { + static TileBehaviorCollection c = {}; + return c; + } +}; + diff --git a/TileBehaviorStrategy.cpp b/TileBehaviorStrategy.cpp deleted file mode 100644 index 9de896e..0000000 --- a/TileBehaviorStrategy.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "TileBehaviorFactory.h" -#include "TileBehavior.h" - -TileBehavior::TileBehavior(const std::string type) { - TileBehaviorFactory::register_strategy(type, this); -} - -void TileBehavior::step(Artist *) { - this->interactions++; -} - diff --git a/TileBehaviorStrategy.h b/TileBehaviorStrategy.h deleted file mode 100644 index b79e7f5..0000000 --- a/TileBehaviorStrategy.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include - -class Tile; -class Museum; -class Artist; - -class TileBehavior { -public: - virtual void step(Artist *); - virtual void update(Tile &) = 0; - virtual std::unique_ptr clone(Museum & m) = 0; - -protected: - TileBehavior(const std::string type); - TileBehavior() = default; - -protected: - unsigned int interactions = 0; - Museum * museum = nullptr; -}; - diff --git a/TileColorFactory.cpp b/TileColorFactory.cpp new file mode 100644 index 0000000..7c5b080 --- /dev/null +++ b/TileColorFactory.cpp @@ -0,0 +1,19 @@ +#include "TileColorFactory.h" + +using namespace std; + +Color TileColorFactory::get_color(string type) { + auto & type_map = TileColorFactory::get_collection(); + + if (type_map.contains(type)) + return type_map.at(type); + + return TileColorFactory::default_color; +} + +void TileColorFactory::register_color(string type, Color color) { + auto & type_map = TileColorFactory::get_collection(); + + type_map[type] = color; +} + diff --git a/TileColorFactory.h b/TileColorFactory.h new file mode 100644 index 0000000..83dfeed --- /dev/null +++ b/TileColorFactory.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include "Color.h" + +class TileColorFactory { + typedef std::map TileAppearanceCollection; + +public: + static Color get_color(std::string); + static void register_color(std::string, Color); + +private: + static TileAppearanceCollection & get_collection() { + static TileAppearanceCollection c = {}; + return c; + } + + static constexpr Color default_color = { + .red = 0xff, + .green = 0xff, + .blue = 0xff, + }; +}; + diff --git a/TileDecayCommand.cpp b/TileDecayCommand.cpp deleted file mode 100644 index ff5c6b2..0000000 --- a/TileDecayCommand.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "StepTileCommand.h" -#include "Museum.h" - -void StepTileCommand::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; - Tile & tile = museum.canvas.get_tile(x, y); - tile.step(nullptr); - tile.update(); -} - diff --git a/TileDecayCommand.h b/TileDecayCommand.h deleted file mode 100644 index ce9dccd..0000000 --- a/TileDecayCommand.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "Command.h" - -class StepTileCommand : public Command { - using Command::Command; - -public: - virtual void execute(unsigned int x, unsigned int y); -}; - diff --git a/ToggleArtistVisibilityCommand.cpp b/ToggleArtistVisibilityCommand.cpp new file mode 100644 index 0000000..6c22dbc --- /dev/null +++ b/ToggleArtistVisibilityCommand.cpp @@ -0,0 +1,13 @@ +#include "ToggleArtistVisibilityCommand.h" +#include "ViewController.h" + +void ToggleArtistVisibilityCommand::set(bool visible) { + ViewController & controller = this->get_controller(); + controller.set_artists_visible(visible); +} + +void ToggleArtistVisibilityCommand::toggle() { + ViewController & controller = this->get_controller(); + controller.set_artists_visible(!controller.get_artists_visible()); +} + diff --git a/ToggleArtistVisibilityCommand.h b/ToggleArtistVisibilityCommand.h new file mode 100644 index 0000000..47e2158 --- /dev/null +++ b/ToggleArtistVisibilityCommand.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Command.h" + +class ToggleArtistVisibilityCommand : public Command { + using Command::Command; + +public: + virtual void toggle(); + virtual void set(bool visible); +}; + diff --git a/ToggleMuseumPauseCommand.cpp b/ToggleMuseumPauseCommand.cpp new file mode 100644 index 0000000..57176af --- /dev/null +++ b/ToggleMuseumPauseCommand.cpp @@ -0,0 +1,13 @@ +#include "ToggleMuseumPauseCommand.h" +#include "Museum.h" + +void ToggleMuseumPauseCommand::set(bool paused) { + Museum & museum = this->get_museum(); + museum.set_pause(paused); +} + +void ToggleMuseumPauseCommand::toggle() { + Museum & museum = this->get_museum(); + museum.set_pause(!museum.get_pause()); +} + diff --git a/ToggleMuseumPauseCommand.h b/ToggleMuseumPauseCommand.h new file mode 100644 index 0000000..15d1388 --- /dev/null +++ b/ToggleMuseumPauseCommand.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Command.h" + +class ToggleMuseumPauseCommand : public Command { + using Command::Command; + +public: + virtual void toggle(); + virtual void set(bool paused); +}; + diff --git a/YellowTileBehavior.cpp b/YellowTileBehavior.cpp deleted file mode 100644 index ae0cd7c..0000000 --- a/YellowTileBehavior.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -#include "CreateArtistTileBehavior.h" -#include "StepTileBehavior.h" -#include "Tile.h" -#include "Museum.h" - -using std::uniform_int_distribution; -using std::uniform_real_distribution; -using std::random_device; -using std::mt19937; -using namespace std; - -CreateArtistTileBehavior CreateArtistTileBehavior::instance { CreateArtistTileBehavior::type }; - -random_device dev{}; -mt19937 rng(dev()); -uniform_int_distribution random_bool(0, 1); -uniform_real_distribution random_float(-1, 1); - -void CreateArtistTileBehavior::update(Tile & tile) { - unsigned int new_artists = this->interactions - this->last_interactions; - this->last_interactions = this->interactions; - - for (unsigned int i = 0; i < new_artists; i++) { - if (i >= 2) break; - ArtistData new_data = { - .x = static_cast(tile.data.x), - .y = static_cast(tile.data.y), - }; - float velocity = random_float(rng); - random_bool(rng) ? new_data.vx = velocity : new_data.vy = velocity; - this->museum->people.add_artist(new_data); - } - - if (this->interactions < 2) return; - tile.set_type(StepTileBehavior::type); -} - -unique_ptr CreateArtistTileBehavior::clone(Museum & museum) { - auto instance = new CreateArtistTileBehavior(); - instance->museum = &museum; - return unique_ptr(instance); -} - diff --git a/YellowTileBehavior.h b/YellowTileBehavior.h deleted file mode 100644 index 6abd67f..0000000 --- a/YellowTileBehavior.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "TileBehavior.h" - -class CreateArtistTileBehavior : public TileBehavior { -public: - virtual void update(Tile &); - virtual std::unique_ptr clone(Museum &); - - static constexpr const char * type = "Y"; - -private: - using TileBehavior::TileBehavior; - static CreateArtistTileBehavior instance; - CreateArtistTileBehavior() = default; - - unsigned int last_interactions = 0; -}; - -- cgit v1.2.3