From 8429e7ff8f7b01028690eacc99d2a1f2db14d131 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 10:55:52 +0100 Subject: [PATCH 1/2] Generic object is good for now --- include/generic_object.hpp | 27 ++++++++++++++++++--------- src/generic_object.cpp | 8 ++++---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/include/generic_object.hpp b/include/generic_object.hpp index 298985e..d3b51a3 100644 --- a/include/generic_object.hpp +++ b/include/generic_object.hpp @@ -47,11 +47,8 @@ namespace character { class Tools; - using billyObjects = std::variant; using billyEnums = std::variant; - static billyObjects new_object(const billyEnums &inputObject); - class GenericObject { public: virtual ~GenericObject() = default; @@ -65,13 +62,19 @@ namespace character { [[nodiscard]] virtual std::uint32_t add_materiel(const characteristic::characType inType) const noexcept = 0; [[nodiscard]] virtual std::string_view to_string() const noexcept = 0; + + [[nodiscard]] virtual billyEnums get_type() const noexcept = 0; }; + using billyObjects = std::unique_ptr; + + static billyObjects new_object(const billyEnums &inputObject) noexcept; + class Weapons final : virtual public GenericObject { public: const weapons type{ weapons::Sword }; - friend billyObjects new_object(const billyEnums &inputObject); + friend billyObjects new_object(const billyEnums &inputObject) noexcept; Weapons() = delete; @@ -85,7 +88,9 @@ namespace character { [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; - [[nodiscard]] std::string_view to_string() const noexcept override; + [[nodiscard]] std::string_view to_string() const noexcept final; + + [[nodiscard]] billyEnums get_type() const noexcept final { return type; } private: explicit Weapons(const weapons type) : type(type) {} @@ -95,7 +100,7 @@ namespace character { public: const equipments type{ equipments::Chainmail }; - friend billyObjects new_object(const billyEnums &inputObject); + friend billyObjects new_object(const billyEnums &inputObject) noexcept; Equipments() = delete; @@ -109,7 +114,9 @@ namespace character { [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; - [[nodiscard]] std::string_view to_string() const noexcept override; + [[nodiscard]] std::string_view to_string() const noexcept final; + + [[nodiscard]] billyEnums get_type() const noexcept final { return type; } private: explicit Equipments(const equipments type) : type(type) {} @@ -119,7 +126,7 @@ namespace character { public: const tools type{ tools::Fourche }; - friend billyObjects new_object(const billyEnums &inputObject); + friend billyObjects new_object(const billyEnums &inputObject) noexcept; Tools() = delete; @@ -133,7 +140,9 @@ namespace character { [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; - [[nodiscard]] std::string_view to_string() const noexcept override; + [[nodiscard]] std::string_view to_string() const noexcept final; + + [[nodiscard]] billyEnums get_type() const noexcept final { return type; } private: explicit Tools(const tools type) : type(type) {} diff --git a/src/generic_object.cpp b/src/generic_object.cpp index 7d21f4b..00e194d 100644 --- a/src/generic_object.cpp +++ b/src/generic_object.cpp @@ -160,11 +160,11 @@ namespace character { } } - billyObjects new_object(const billyEnums &inputObject) { + billyObjects new_object(const billyEnums &inputObject) noexcept { return std::visit(overloaded{ - [](const weapons input) { return billyObjects{ Weapons{ input }}; }, - [](const equipments input) { return billyObjects{ Equipments{ input }}; }, - [](const tools input) { return billyObjects{ Tools{ input }}; } + [](const weapons input) { return billyObjects{ new Weapons{ input }}; }, + [](const equipments input) { return billyObjects{ new Equipments{ input }}; }, + [](const tools input) { return billyObjects{ new Tools{ input }}; } }, inputObject); } } From 05782bae6928a4bc02f5bb70bfad4ec947ef0cf4 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 12:17:14 +0100 Subject: [PATCH 2/2] Correct rules for equipments --- include/generic_object.hpp | 8 ++++---- src/generic_object.cpp | 14 ++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/generic_object.hpp b/include/generic_object.hpp index d3b51a3..0d2e45d 100644 --- a/include/generic_object.hpp +++ b/include/generic_object.hpp @@ -59,7 +59,7 @@ namespace character { [[nodiscard]] virtual std::uint32_t add_damage() const noexcept = 0; - [[nodiscard]] virtual std::uint32_t add_materiel(const characteristic::characType inType) const noexcept = 0; + [[nodiscard]] virtual std::int32_t add_materiel(const characteristic::characType inType) const noexcept = 0; [[nodiscard]] virtual std::string_view to_string() const noexcept = 0; @@ -86,7 +86,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; @@ -112,7 +112,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; @@ -138,7 +138,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::uint32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; diff --git a/src/generic_object.cpp b/src/generic_object.cpp index 00e194d..c83cccb 100644 --- a/src/generic_object.cpp +++ b/src/generic_object.cpp @@ -30,7 +30,7 @@ namespace character { std::uint32_t Weapons::add_damage() const noexcept { return type == weapons::Morgenstern ? 1 : 0; } - std::uint32_t Weapons::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Weapons::add_materiel(const characteristic::characType inType) const noexcept { switch (type) { case weapons::Sword: if (inType == characType::Habilete) { @@ -93,12 +93,14 @@ namespace character { std::uint32_t Equipments::add_damage() const noexcept { return 0; } - std::uint32_t Equipments::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Equipments::add_materiel(const characteristic::characType inType) const noexcept { switch (type) { case equipments::Chainmail: - return inType == characType::Habilete || - inType == characType::Adresse || - inType == characType::Endurance ? 1 : 0; + if (inType == characType::Habilete || + inType == characType::Adresse) { + return -1; + } + return inType == characType::Endurance ? 1 : 0; case equipments::CookingPot: return inType == characType::Endurance ? 2 : 0; case equipments::PamphletTourist: @@ -127,7 +129,7 @@ namespace character { std::uint32_t Tools::add_damage() const noexcept { return 0; } - std::uint32_t Tools::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Tools::add_materiel(const characteristic::characType inType) const noexcept { switch (type) { case tools::Fourche: switch (inType) {