aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-11 18:39:15 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-11 18:39:15 +0200
commit12a719a9fd9b8e89159c407c3b612b226c860778 (patch)
treefdfdb2d78bde252946a27d04d44ce9e1cafe2e15
parent70feb9ed564fb7f557b7ab8b8d65e6376811b6ce (diff)
add placeholder tile behaviors
-rw-r--r--BlueTileBehavior.cpp17
-rw-r--r--BlueTileBehavior.h14
-rw-r--r--CMakeLists.txt7
-rw-r--r--GrayTileBehavior.cpp17
-rw-r--r--GrayTileBehavior.h13
-rw-r--r--NullTileBehavior.cpp18
-rw-r--r--NullTileBehavior.h15
-rw-r--r--RedTileBehavior.cpp17
-rw-r--r--RedTileBehavior.h14
-rw-r--r--Tile.cpp2
-rw-r--r--Tile.h7
-rw-r--r--TileBehavior.cpp20
-rw-r--r--TileBehavior.h15
-rw-r--r--TileBehaviorStrategy.cpp7
-rw-r--r--TileBehaviorStrategy.h17
-rw-r--r--YellowTileBehavior.cpp17
-rw-r--r--YellowTileBehavior.h13
-rw-r--r--docs/class-diag.puml7
18 files changed, 234 insertions, 3 deletions
diff --git a/BlueTileBehavior.cpp b/BlueTileBehavior.cpp
new file mode 100644
index 0000000..95a78ec
--- /dev/null
+++ b/BlueTileBehavior.cpp
@@ -0,0 +1,17 @@
+#include <memory>
+
+#include "BlueTileBehavior.h"
+
+using namespace std;
+
+BlueTileBehavior BlueTileBehavior::instance {"B"};
+
+void BlueTileBehavior::run(Tile &) {
+ printf("TODO: %s\n", __PRETTY_FUNCTION__);
+}
+
+unique_ptr<TileBehaviorStrategy> BlueTileBehavior::clone() {
+ auto instance = new BlueTileBehavior();
+ return unique_ptr<TileBehaviorStrategy>(instance);
+}
+
diff --git a/BlueTileBehavior.h b/BlueTileBehavior.h
index 6f70f09..98a8c86 100644
--- a/BlueTileBehavior.h
+++ b/BlueTileBehavior.h
@@ -1 +1,15 @@
#pragma once
+
+#include "TileBehaviorStrategy.h"
+
+class BlueTileBehavior : public TileBehaviorStrategy {
+public:
+ virtual void run(Tile &);
+ virtual std::unique_ptr<TileBehaviorStrategy> clone();
+
+private:
+ using TileBehaviorStrategy::TileBehaviorStrategy;
+ static BlueTileBehavior instance;
+ BlueTileBehavior() = default;
+};
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d8fe9a5..aaf2d07 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,6 +30,13 @@ add_executable(main
Deserializer.cpp
Tile.cpp
TileAppearance.cpp
+ TileBehaviorStrategy.cpp
+ TileBehavior.cpp
+ NullTileBehavior.cpp
+ YellowTileBehavior.cpp
+ BlueTileBehavior.cpp
+ RedTileBehavior.cpp
+ GrayTileBehavior.cpp
)
target_link_libraries(main
diff --git a/GrayTileBehavior.cpp b/GrayTileBehavior.cpp
new file mode 100644
index 0000000..af054d5
--- /dev/null
+++ b/GrayTileBehavior.cpp
@@ -0,0 +1,17 @@
+#include <memory>
+
+#include "GrayTileBehavior.h"
+
+using namespace std;
+
+GrayTileBehavior GrayTileBehavior::instance {"G"};
+
+void GrayTileBehavior::run(Tile &) {
+ printf("TODO: %s\n", __PRETTY_FUNCTION__);
+}
+
+unique_ptr<TileBehaviorStrategy> GrayTileBehavior::clone() {
+ auto instance = new GrayTileBehavior();
+ return unique_ptr<TileBehaviorStrategy>(instance);
+}
+
diff --git a/GrayTileBehavior.h b/GrayTileBehavior.h
index 3f59c93..9ea4d96 100644
--- a/GrayTileBehavior.h
+++ b/GrayTileBehavior.h
@@ -1,2 +1,15 @@
#pragma once
+#include "TileBehaviorStrategy.h"
+
+class GrayTileBehavior : public TileBehaviorStrategy {
+public:
+ virtual void run(Tile &);
+ virtual std::unique_ptr<TileBehaviorStrategy> clone();
+
+private:
+ using TileBehaviorStrategy::TileBehaviorStrategy;
+ static GrayTileBehavior instance;
+ GrayTileBehavior() = default;
+};
+
diff --git a/NullTileBehavior.cpp b/NullTileBehavior.cpp
new file mode 100644
index 0000000..542b67c
--- /dev/null
+++ b/NullTileBehavior.cpp
@@ -0,0 +1,18 @@
+#include <memory>
+
+#include "NullTileBehavior.h"
+
+using namespace std;
+
+NullTileBehavior NullTileBehavior::instance {""};
+
+void NullTileBehavior::run(Tile &) {
+
+}
+
+unique_ptr<TileBehaviorStrategy> NullTileBehavior::clone() {
+ auto instance = new NullTileBehavior();
+ return unique_ptr<TileBehaviorStrategy>(instance);
+}
+
+
diff --git a/NullTileBehavior.h b/NullTileBehavior.h
new file mode 100644
index 0000000..6b68a4a
--- /dev/null
+++ b/NullTileBehavior.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "TileBehaviorStrategy.h"
+
+class NullTileBehavior : public TileBehaviorStrategy {
+public:
+ virtual void run(Tile &);
+ virtual std::unique_ptr<TileBehaviorStrategy> clone();
+
+private:
+ using TileBehaviorStrategy::TileBehaviorStrategy;
+ static NullTileBehavior instance;
+ NullTileBehavior() = default;
+};
+
diff --git a/RedTileBehavior.cpp b/RedTileBehavior.cpp
new file mode 100644
index 0000000..e295b4b
--- /dev/null
+++ b/RedTileBehavior.cpp
@@ -0,0 +1,17 @@
+#include <memory>
+
+#include "RedTileBehavior.h"
+
+using namespace std;
+
+RedTileBehavior RedTileBehavior::instance {"R"};
+
+void RedTileBehavior::run(Tile &) {
+ printf("TODO: %s\n", __PRETTY_FUNCTION__);
+}
+
+unique_ptr<TileBehaviorStrategy> RedTileBehavior::clone() {
+ auto instance = new RedTileBehavior();
+ return unique_ptr<TileBehaviorStrategy>(instance);
+}
+
diff --git a/RedTileBehavior.h b/RedTileBehavior.h
index 3f59c93..7b686b8 100644
--- a/RedTileBehavior.h
+++ b/RedTileBehavior.h
@@ -1,2 +1,16 @@
#pragma once
+#include "TileBehaviorStrategy.h"
+
+class RedTileBehavior : public TileBehaviorStrategy {
+public:
+ virtual void run(Tile &);
+ virtual std::unique_ptr<TileBehaviorStrategy> clone();
+
+private:
+ using TileBehaviorStrategy::TileBehaviorStrategy;
+ static RedTileBehavior instance;
+ RedTileBehavior() = default;
+};
+
+
diff --git a/Tile.cpp b/Tile.cpp
index fd33423..cb7b89b 100644
--- a/Tile.cpp
+++ b/Tile.cpp
@@ -2,6 +2,7 @@
#include "Tile.h"
#include "TileAppearance.h"
+#include "TileBehavior.h"
Tile::Tile() {
this->data = {};
@@ -15,5 +16,6 @@ Tile::Tile(TileData data) {
void Tile::update() {
this->color = TileAppearance::get_color(this->data.type);
+ this->behavior = TileBehavior::get_strategy(this->data.type).clone();
}
diff --git a/Tile.h b/Tile.h
index 538a857..dc0b375 100644
--- a/Tile.h
+++ b/Tile.h
@@ -1,8 +1,10 @@
#pragma once
+#include <memory>
+
#include "TileData.h"
-#include "TileBehavior.h"
#include "Color.h"
+#include "TileBehaviorStrategy.h"
class Tile {
public:
@@ -12,8 +14,7 @@ public:
public:
TileData data;
Color color;
-private:
- TileBehavior behavior;
+ std::unique_ptr<TileBehaviorStrategy> behavior = nullptr;
public:
void update();
diff --git a/TileBehavior.cpp b/TileBehavior.cpp
new file mode 100644
index 0000000..6cabd40
--- /dev/null
+++ b/TileBehavior.cpp
@@ -0,0 +1,20 @@
+#include "TileBehavior.h"
+#include "Exception.h"
+
+using namespace std;
+
+TileBehaviorStrategy & TileBehavior::get_strategy(string type) {
+ auto & type_map = TileBehavior::get_collection();
+
+ if (type_map.contains(type))
+ return *type_map.at(type);
+
+ throw Exception("unknown behavior for tile type \"%s\"", type.c_str());
+}
+
+void TileBehavior::register_strategy(string type, TileBehaviorStrategy * strategy) {
+ auto & type_map = TileBehavior::get_collection();
+
+ type_map[type] = strategy;
+}
+
diff --git a/TileBehavior.h b/TileBehavior.h
index e46c0cf..2298c07 100644
--- a/TileBehavior.h
+++ b/TileBehavior.h
@@ -1,6 +1,21 @@
#pragma once
+#include <map>
+#include <string>
+
+#include "TileBehaviorStrategy.h"
+
class TileBehavior {
+ typedef std::map<std::string, TileBehaviorStrategy *> TileBehaviorCollection;
+
+public:
+ static TileBehaviorStrategy & get_strategy(std::string);
+ static void register_strategy(std::string, TileBehaviorStrategy *);
+private:
+ static TileBehaviorCollection & get_collection() {
+ static TileBehaviorCollection c = {};
+ return c;
+ }
};
diff --git a/TileBehaviorStrategy.cpp b/TileBehaviorStrategy.cpp
new file mode 100644
index 0000000..9e829cc
--- /dev/null
+++ b/TileBehaviorStrategy.cpp
@@ -0,0 +1,7 @@
+#include "TileBehavior.h"
+#include "TileBehaviorStrategy.h"
+
+TileBehaviorStrategy::TileBehaviorStrategy(const std::string type) {
+ TileBehavior::register_strategy(type, this);
+}
+
diff --git a/TileBehaviorStrategy.h b/TileBehaviorStrategy.h
new file mode 100644
index 0000000..a2c90e3
--- /dev/null
+++ b/TileBehaviorStrategy.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+#include <memory>
+
+class Tile;
+
+class TileBehaviorStrategy {
+public:
+ virtual void run(Tile &) = 0;
+ virtual std::unique_ptr<TileBehaviorStrategy> clone() = 0;
+
+protected:
+ TileBehaviorStrategy(const std::string type);
+ TileBehaviorStrategy() = default;
+};
+
diff --git a/YellowTileBehavior.cpp b/YellowTileBehavior.cpp
new file mode 100644
index 0000000..2999229
--- /dev/null
+++ b/YellowTileBehavior.cpp
@@ -0,0 +1,17 @@
+#include <memory>
+
+#include "YellowTileBehavior.h"
+
+using namespace std;
+
+YellowTileBehavior YellowTileBehavior::instance {"Y"};
+
+void YellowTileBehavior::run(Tile &) {
+ printf("TODO: %s\n", __PRETTY_FUNCTION__);
+}
+
+unique_ptr<TileBehaviorStrategy> YellowTileBehavior::clone() {
+ auto instance = new YellowTileBehavior();
+ return unique_ptr<TileBehaviorStrategy>(instance);
+}
+
diff --git a/YellowTileBehavior.h b/YellowTileBehavior.h
index 3f59c93..7a457a4 100644
--- a/YellowTileBehavior.h
+++ b/YellowTileBehavior.h
@@ -1,2 +1,15 @@
#pragma once
+#include "TileBehaviorStrategy.h"
+
+class YellowTileBehavior : public TileBehaviorStrategy {
+public:
+ virtual void run(Tile &);
+ virtual std::unique_ptr<TileBehaviorStrategy> clone();
+
+private:
+ using TileBehaviorStrategy::TileBehaviorStrategy;
+ static YellowTileBehavior instance;
+ YellowTileBehavior() = default;
+};
+
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index 3502aa5..a59f531 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -150,22 +150,29 @@ rectangle Group_Model as "Model" <<group>> {
class TileBehavior
together {
+ class NullTileBehavior
+
class GrayTileBehavior
class RedTileBehavior
class BlueTileBehavior
class YellowTileBehavior
+ NullTileBehavior -d[hidden]- GrayTileBehavior
GrayTileBehavior -d[hidden]- RedTileBehavior
RedTileBehavior -d[hidden]- BlueTileBehavior
BlueTileBehavior -d[hidden]- YellowTileBehavior
}
+ TileBehaviorStrategy <|.. NullTileBehavior
TileBehaviorStrategy <|.. GrayTileBehavior
TileBehaviorStrategy <|.. RedTileBehavior
TileBehaviorStrategy <|.. BlueTileBehavior
TileBehaviorStrategy <|.. YellowTileBehavior
Tile --> "state" TileBehavior
+
+ TileBehavior .> TileBehaviorStrategy
+ TileBehavior <. TileBehaviorStrategy
}
Museum --> People