diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 16:09:47 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-23 16:09:47 +0200 |
commit | 277157b3e06b2deeacbdbc8bf6190de19f88169d (patch) | |
tree | db9c013b67f93d27fa5bb9cf84c7d6dc53f72734 | |
parent | 64028952ceb17f97ded08f1ab7ec0b06c41e2b87 (diff) |
more refactoring / preparation for pathfinding
-rw-r--r-- | Canvas.cpp | 2 | ||||
-rw-r--r-- | Canvas.h | 7 | ||||
-rw-r--r-- | MuseumDeserializer.cpp | 3 | ||||
-rw-r--r-- | Tile.cpp | 6 | ||||
-rw-r--r-- | TileBehaviorFactory.h | 5 | ||||
-rw-r--r-- | TileColorFactory.cpp | 14 | ||||
-rw-r--r-- | TileColorFactory.h | 10 | ||||
-rw-r--r-- | ViewController.cpp | 12 | ||||
-rw-r--r-- | ViewController.h | 2 | ||||
-rw-r--r-- | docs/class-diag.puml | 94 |
10 files changed, 86 insertions, 69 deletions
@@ -7,7 +7,7 @@ using namespace std; -Canvas::Canvas(Museum & museum) : museum(museum) { } +Canvas::Canvas(Museum & museum) : museum(museum), tile_behavior(museum), tile_color() { } Tile & Canvas::get_tile(unsigned x, unsigned y) { return *this->tiles[this->pos_to_index(x, y)]; @@ -6,6 +6,9 @@ #include "CanvasData.h" #include "Tile.h" +#include "TileBehaviorFactory.h" +#include "TileColorFactory.h" + class Museum; class Canvas { @@ -23,6 +26,10 @@ public: void update(); void set_data(CanvasData data); +public: + TileColorFactory tile_color; + TileBehaviorFactory tile_behavior; + private: void update_steps(); void update_tiles(); diff --git a/MuseumDeserializer.cpp b/MuseumDeserializer.cpp index fca8d50..19fc1a6 100644 --- a/MuseumDeserializer.cpp +++ b/MuseumDeserializer.cpp @@ -17,6 +17,7 @@ void MuseumDeserializer::set_tile(TileData data) { void MuseumDeserializer::add_type(std::string type, Color color, unsigned int weight) { if (type.length() == 0) return; - TileColorFactory::register_color(type, color); + this->museum.canvas.tile_color.register_color(type, color); + // this->museum.pathfinding.weights.register_weight(type, weight); } @@ -18,8 +18,10 @@ void Tile::set_type(const string & type) { void Tile::set_data(TileData & data) { this->data = data; - this->color = TileColorFactory::get_color(this->data.type); - this->behavior = TileBehaviorFactory(this->museum).create(this->data.type); + + Canvas & canvas = this->museum.canvas; + this->color = canvas.tile_color.get_color(this->data.type); + this->behavior = canvas.tile_behavior.create(this->data.type); } void Tile::update() { diff --git a/TileBehaviorFactory.h b/TileBehaviorFactory.h index 6167067..9a4b101 100644 --- a/TileBehaviorFactory.h +++ b/TileBehaviorFactory.h @@ -1,9 +1,10 @@ #pragma once #include <string> +#include <memory> -#include "Museum.h" -#include "TileBehavior.h" +class Museum; +class TileBehavior; class TileBehaviorFactory { public: diff --git a/TileColorFactory.cpp b/TileColorFactory.cpp index 7c5b080..22738f6 100644 --- a/TileColorFactory.cpp +++ b/TileColorFactory.cpp @@ -2,18 +2,14 @@ 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); +const Color & TileColorFactory::get_color(const string & type) { + if (this->collection.contains(type)) + return this->collection.at(type); return TileColorFactory::default_color; } -void TileColorFactory::register_color(string type, Color color) { - auto & type_map = TileColorFactory::get_collection(); - - type_map[type] = color; +void TileColorFactory::register_color(const string & type, const Color & color) { + this->collection[type] = color; } diff --git a/TileColorFactory.h b/TileColorFactory.h index 83dfeed..4394c9e 100644 --- a/TileColorFactory.h +++ b/TileColorFactory.h @@ -9,15 +9,11 @@ class TileColorFactory { typedef std::map<std::string, Color> TileAppearanceCollection; public: - static Color get_color(std::string); - static void register_color(std::string, Color); + const Color & get_color(const std::string &); + void register_color(const std::string &, const Color &); private: - static TileAppearanceCollection & get_collection() { - static TileAppearanceCollection c = {}; - return c; - } - + TileAppearanceCollection collection; static constexpr Color default_color = { .red = 0xff, .green = 0xff, diff --git a/ViewController.cpp b/ViewController.cpp index ec0315d..67fd77c 100644 --- a/ViewController.cpp +++ b/ViewController.cpp @@ -66,24 +66,24 @@ void ViewController::update_artists() { } } -void ViewController::draw_pathfinding_dot(unsigned int x, unsigned int y, const Color & c) { +void ViewController::draw_pathfinding_dot(pair<unsigned int, unsigned int> point, const Color & color) { this->view.fill_rect(center({ - .x = static_cast<float>(x * scale), - .y = static_cast<float>(y * scale), + .x = static_cast<float>(point.first * scale), + .y = static_cast<float>(point.second * scale), .width = static_cast<float>(pathfinding_size), .height = static_cast<float>(pathfinding_size), - }), c); + }), color); } void ViewController::update_pathfinding() { PathfindingContext & ctx = this->museum.pathfinding; - this->draw_pathfinding_dot(ctx.get_end().first, ctx.get_end().second, { + this->draw_pathfinding_dot(ctx.get_end(), { .red = 0x00, .green = 0x00, .blue = 0xdd, }); - this->draw_pathfinding_dot(ctx.get_start().first, ctx.get_start().second, { + this->draw_pathfinding_dot(ctx.get_start(), { .red = 0xff, .green = 0xff, .blue = 0xff, diff --git a/ViewController.h b/ViewController.h index d18e96a..f05d62b 100644 --- a/ViewController.h +++ b/ViewController.h @@ -32,7 +32,7 @@ private: void update_quadtree_recursive(QuadTreeCollisionChecker * tree); private: - void draw_pathfinding_dot(unsigned int x, unsigned int y, const Color & c); + void draw_pathfinding_dot(std::pair<unsigned int, unsigned int>, const Color &); Rectangle center(Rectangle); private: diff --git a/docs/class-diag.puml b/docs/class-diag.puml index b20b3e1..2d890d5 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -149,6 +149,19 @@ rectangle Group_Pathfinding as "Pathfinding" <<group>> { class PathfindingContext { + PathfindingContext(Museum &) } + class Pathfinder <<abstract>> { + + Pathfinder(Museum &) + -- + # museum : Museum & + } + class BreadthFirstPathfinder { + } + class DijkstraPathfinder { + } + + Pathfinder <|-- BreadthFirstPathfinder + Pathfinder <|-- DijkstraPathfinder + PathfindingContext -> Pathfinder } rectangle Group_Model as "Model" <<group>> { class Museum { @@ -179,6 +192,9 @@ rectangle Group_Model as "Model" <<group>> { + data : CanvasData + set_data(CanvasData) -- + + tile_color : TileColorFactory + + tile_behavior : TileBehaviorFactory + -- - tiles : vector<Tile *> - museum : Museum & } @@ -230,19 +246,23 @@ rectangle Group_Model as "Model" <<group>> { + vx : float + vy : float } + class TileColorFactory <<factory>> { + + get_color(string) : Color <<static>> + + register_color(string, Color) <<static>> + } + class TileBehaviorFactory <<factory>> { + + TileBehaviorFactory(Museum &) + + create(string) : uniq<TileBehavior> + -- + - museum : Museum & + } } - together { struct Color { red : unsigned int green : unsigned int blue : unsigned int } - class TileColorFactory <<factory>> <<singleton>> { - + get_color(string) : Color <<static>> - + register_color(string, Color) <<static>> - } - } together { interface TileBehavior { @@ -254,40 +274,32 @@ rectangle Group_Model as "Model" <<group>> { # interactions : unsigned int # museum : Museum & } - class TileBehaviorFactory <<factory>> { - + TileBehaviorFactory(Museum &) - + create(string) : uniq<TileBehavior> - -- - - museum : Museum & - } - } - together { - class NullTileBehavior { - - type = "" <<static constexpr>> - } - class StepTileBehavior { - - type = "G" : <<static constexpr>> - } - class DeleteArtistTileBehavior { - - type = "R" : <<static constexpr>> - } - class SetNeighborTileBehavior { - - type = "B" : <<static constexpr>> - -- - - dx : int - - dy : int - } - class CreateArtistTileBehavior { - - type = "Y" : <<static constexpr>> - -- - - last_interactions : unsigned int - } - - NullTileBehavior -d[hidden]- StepTileBehavior - StepTileBehavior -d[hidden]- DeleteArtistTileBehavior - DeleteArtistTileBehavior -d[hidden]- SetNeighborTileBehavior - SetNeighborTileBehavior -d[hidden]- CreateArtistTileBehavior + class NullTileBehavior { + - type = "" <<constexpr>> + } + class StepTileBehavior { + - type = "G" : <<constexpr>> + } + class DeleteArtistTileBehavior { + - type = "R" : <<constexpr>> + } + class SetNeighborTileBehavior { + - type = "B" : <<constexpr>> + -- + - dx : int + - dy : int + } + class CreateArtistTileBehavior { + - type = "Y" : <<constexpr>> + -- + - last_interactions : unsigned int + } + + NullTileBehavior -d[hidden]- StepTileBehavior + StepTileBehavior -d[hidden]- DeleteArtistTileBehavior + DeleteArtistTileBehavior -d[hidden]- SetNeighborTileBehavior + SetNeighborTileBehavior -d[hidden]- CreateArtistTileBehavior } Canvas -l[hidden] People @@ -296,6 +308,8 @@ rectangle Group_Model as "Model" <<group>> { Museum --> Canvas Canvas --> Tile + Canvas --> TileColorFactory + Canvas --> TileBehaviorFactory People --> Artist Tile -> TileData @@ -303,7 +317,7 @@ rectangle Group_Model as "Model" <<group>> { Canvas -> CanvasData Tile --> "state" Color - Tile .[norank].> TileColorFactory + Tile .> TileColorFactory TileColorFactory -> Color |