summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--battles/include/kurator/battles/ShipConfig.h8
-rw-r--r--battles/src/BaseBattle.cpp15
-rw-r--r--battles/src/scenarios.cpp37
-rw-r--r--universe/CMakeLists.txt7
-rw-r--r--universe/include/kurator/universe.h18
-rw-r--r--universe/include/kurator/universe/NotFound.h24
-rw-r--r--universe/include/kurator/universe/Repository.h25
-rw-r--r--universe/src/NotFound.cpp27
-rw-r--r--universe/src/SampleRepository.cpp51
-rw-r--r--universe/src/SampleRepository.h25
-rw-r--r--universe/src/universe.cpp24
11 files changed, 227 insertions, 34 deletions
diff --git a/battles/include/kurator/battles/ShipConfig.h b/battles/include/kurator/battles/ShipConfig.h
index bc01ad2..2066430 100644
--- a/battles/include/kurator/battles/ShipConfig.h
+++ b/battles/include/kurator/battles/ShipConfig.h
@@ -1,10 +1,8 @@
#pragma once
+#include <string>
#include <vector>
-#include <kurator/universe/ShipType.h>
-#include <kurator/universe/TurretType.h>
-
namespace kurator
{
@@ -15,8 +13,8 @@ namespace battles
struct ShipConfig
{
int team;
- universe::ShipType type;
- std::vector<universe::TurretType> turrets;
+ std::string type;
+ std::vector<std::string> turrets;
};
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp
index 815c683..8a3dc37 100644
--- a/battles/src/BaseBattle.cpp
+++ b/battles/src/BaseBattle.cpp
@@ -1,13 +1,13 @@
#include "BaseBattle.h"
-#include <cmath>
#include <memory>
+#include <type_traits>
#include <entt/entity/registry.hpp>
#include <kurator/battles/components.h>
#include <kurator/battles/Scenario.h>
-#include <kurator/universe/ShipType.h>
+#include <kurator/universe.h>
namespace kurator
@@ -23,17 +23,20 @@ BaseBattle::BaseBattle(const Scenario& scenario) :
_registry {},
spawner {total_teams_in(scenario), 2.5, 0.1}
{
+ const auto repo = universe::load_sample();
for (const auto& ship : scenario.ships) {
const auto entity = _registry.create();
- _registry.emplace<universe::ShipType>(entity, ship.type);
+ const auto type = repo->ship_type(ship.type);
+ _registry.emplace<std::decay<decltype(type)>::type>(entity, type);
_registry.emplace<Team>(entity, ship.team);
_registry.emplace<Transform>(entity, spawner.get(ship.team));
_registry.emplace<FloatingMovement>(entity, 0.4);
_registry.emplace<AIState>(entity, Point{0.0, 0.0});
- _registry.emplace<HitPoints>(entity, ship.type.base_health_points);
- for (const auto& turret_def : ship.turrets) {
+ _registry.emplace<HitPoints>(entity, type.base_health_points);
+ for (const auto& turret_name : ship.turrets) {
const auto turret = _registry.create();
- _registry.emplace<universe::TurretType>(turret, turret_def);
+ const auto def = repo->turret_type(turret_name);
+ _registry.emplace<std::decay<decltype(def)>::type>(turret, def);
_registry.emplace<TurretControl>(turret, 0.0, entity);
_registry.emplace<Transform>(turret, Point{0.0, 0.0}, 0.0, entity);
}
diff --git a/battles/src/scenarios.cpp b/battles/src/scenarios.cpp
index 3b35f44..7a79942 100644
--- a/battles/src/scenarios.cpp
+++ b/battles/src/scenarios.cpp
@@ -1,7 +1,6 @@
#include <kurator/battles/scenarios.h>
#include <kurator/battles/Scenario.h>
-#include <kurator/universe/ShipType.h>
namespace kurator
@@ -15,29 +14,25 @@ namespace scenarios
Scenario
example()
{
- const universe::ShipType halo {"halo", 6.0};
- const universe::ShipType cube {"cube", 12.0};
- const universe::ShipType bell {"bell", 30.0};
- const universe::TurretType cannon {"cannon", 1.0, 1.0, 0.5};
return {
"example",
{
- {0, halo, {cannon}},
- {0, halo, {cannon}},
- {0, cube, {cannon, cannon}},
- {0, cube, {cannon, cannon}},
- {0, cube, {cannon, cannon}},
- {0, bell, {cannon}},
- {0, bell, {cannon}},
- {0, bell, {cannon}},
- {1, halo, {cannon}},
- {1, halo, {cannon}},
- {1, bell, {cannon}},
- {1, cube, {cannon, cannon}},
- {1, cube, {cannon, cannon}},
- {1, cube, {cannon, cannon}},
- {1, bell, {cannon}},
- {1, bell, {cannon}},
+ {0, "halo", {"cannon"}},
+ {0, "halo", {"cannon"}},
+ {0, "cube", {"cannon", "cannon"}},
+ {0, "cube", {"cannon", "cannon"}},
+ {0, "cube", {"cannon", "cannon"}},
+ {0, "bell", {"cannon"}},
+ {0, "bell", {"cannon"}},
+ {0, "bell", {"cannon"}},
+ {1, "halo", {"cannon"}},
+ {1, "halo", {"cannon"}},
+ {1, "bell", {"cannon"}},
+ {1, "cube", {"cannon", "cannon"}},
+ {1, "cube", {"cannon", "cannon"}},
+ {1, "cube", {"cannon", "cannon"}},
+ {1, "bell", {"cannon"}},
+ {1, "bell", {"cannon"}},
},
};
}
diff --git a/universe/CMakeLists.txt b/universe/CMakeLists.txt
index 24457c5..bb7253f 100644
--- a/universe/CMakeLists.txt
+++ b/universe/CMakeLists.txt
@@ -1,8 +1,11 @@
project(universe)
add_library(
- ${PROJECT_NAME} INTERFACE
+ ${PROJECT_NAME}
+ src/NotFound.cpp
+ src/SampleRepository.cpp
+ src/universe.cpp
)
target_include_directories(
${PROJECT_NAME}
- INTERFACE include
+ PUBLIC include
)
diff --git a/universe/include/kurator/universe.h b/universe/include/kurator/universe.h
new file mode 100644
index 0000000..73d06ae
--- /dev/null
+++ b/universe/include/kurator/universe.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <memory>
+
+#include "universe/Repository.h"
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+auto load_sample() -> std::shared_ptr<Repository>;
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/include/kurator/universe/NotFound.h b/universe/include/kurator/universe/NotFound.h
new file mode 100644
index 0000000..03d344d
--- /dev/null
+++ b/universe/include/kurator/universe/NotFound.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <exception>
+#include <string>
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+class NotFound : public std::exception
+{
+public:
+ explicit NotFound(std::string _id);
+ const char* what() const noexcept override;
+private:
+ const std::string id;
+};
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/include/kurator/universe/Repository.h b/universe/include/kurator/universe/Repository.h
new file mode 100644
index 0000000..8d44793
--- /dev/null
+++ b/universe/include/kurator/universe/Repository.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+
+#include "ShipType.h"
+#include "TurretType.h"
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+class Repository
+{
+public:
+ virtual ~Repository() = default;
+ virtual ShipType ship_type(const std::string& id) const = 0;
+ virtual TurretType turret_type(const std::string& id) const = 0;
+};
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/src/NotFound.cpp b/universe/src/NotFound.cpp
new file mode 100644
index 0000000..e5a4109
--- /dev/null
+++ b/universe/src/NotFound.cpp
@@ -0,0 +1,27 @@
+#include <kurator/universe/NotFound.h>
+
+#include <string>
+#include <utility>
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+NotFound::NotFound(std::string _id) :
+ id {std::move(_id)}
+{
+}
+
+
+const char*
+NotFound::what() const noexcept
+{
+ return "item not found in repository"; // what is not found? use that id
+}
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/src/SampleRepository.cpp b/universe/src/SampleRepository.cpp
new file mode 100644
index 0000000..66a3105
--- /dev/null
+++ b/universe/src/SampleRepository.cpp
@@ -0,0 +1,51 @@
+#include "SampleRepository.h"
+
+#include <stdexcept>
+#include <string>
+#include <unordered_map>
+
+#include <kurator/universe/NotFound.h>
+#include <kurator/universe/ShipType.h>
+#include <kurator/universe/TurretType.h>
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+static const std::unordered_map<std::string, ShipType> ships {
+ {"cube", {"Cube", 10.0}},
+ {"halo", {"Halo", 4.0}},
+ {"bell", {"Bell", 18.0}},
+};
+
+
+static const std::unordered_map<std::string, TurretType> turrets {
+ {"cannon", {"Cannon", 1.0, 1.0, 0.5}},
+};
+
+
+ShipType
+SampleRepository::ship_type(const std::string& id) const
+try {
+ return ships.at(id);
+}
+catch (const std::out_of_range&) {
+ throw NotFound(id);
+}
+
+
+TurretType
+SampleRepository::turret_type(const std::string& id) const
+try {
+ return turrets.at(id);
+}
+catch (const std::out_of_range&) {
+ throw NotFound(id);
+}
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/src/SampleRepository.h b/universe/src/SampleRepository.h
new file mode 100644
index 0000000..1cf4fb4
--- /dev/null
+++ b/universe/src/SampleRepository.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+
+#include <kurator/universe/Repository.h>
+#include <kurator/universe/ShipType.h>
+#include <kurator/universe/TurretType.h>
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+class SampleRepository : public Repository
+{
+public:
+ ShipType ship_type(const std::string& id) const override;
+ TurretType turret_type(const std::string& id) const override;
+};
+
+
+} // namespace universe
+} // namespace kurator
diff --git a/universe/src/universe.cpp b/universe/src/universe.cpp
new file mode 100644
index 0000000..b77ce7c
--- /dev/null
+++ b/universe/src/universe.cpp
@@ -0,0 +1,24 @@
+#include <kurator/universe.h>
+
+#include <memory>
+
+#include <kurator/universe/Repository.h>
+
+#include "SampleRepository.h"
+
+
+namespace kurator
+{
+namespace universe
+{
+
+
+std::shared_ptr<Repository>
+load_sample()
+{
+ return std::make_shared<SampleRepository>();
+}
+
+
+} // namespace universe
+} // namespace kurator