From 16422be351704c39f8b6548d66f51aec8bd44649 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 26 Feb 2024 14:27:40 +0100 Subject: [PATCH] Adding objects is done except for the dagger --- include/billy_objects.hpp | 104 ++++++++++++++++++ include/characteristic/chance.hpp | 3 + src/billy_objects.cpp | 169 ++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 include/billy_objects.hpp create mode 100644 src/billy_objects.cpp diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp new file mode 100644 index 0000000..3841153 --- /dev/null +++ b/include/billy_objects.hpp @@ -0,0 +1,104 @@ +// +// Created by postaron on 20/02/24. +// + +#ifndef BILLYSHEET_BILLY_OBJECTS_HPP +#define BILLYSHEET_BILLY_OBJECTS_HPP + +#include +#include +#include +#include +#include +#include "characteristic/characteristic.hpp" + +// helper type for the visitor +template +struct overloaded : Ts ... { using Ts::operator()...; }; +template +overloaded(Ts...) -> overloaded; + +namespace character { + class CharacterSheet; + + enum class weapons : std::uint8_t { + Sword = 0, + Lance = 1, + Morgenstern = 2, + Bow = 3 + }; + + enum class equipments : std::uint8_t { + Chainmail = 0, + CookingPot = 1, + PamphletTourist = 2, + MedicKit = 3, + }; + + enum class tools : std::uint8_t { + Fourche = 0, + Dagger = 1, + RockClimbingKit = 2, + SackOfGrain = 3 + }; + + class BillyObjects final { + public: + using billyObject = std::variant; + using container = std::array; + + static constexpr std::string_view sword{ "Sword" }; + static constexpr std::string_view lance{ "Lance" }; + static constexpr std::string_view morgenstern{ "Morgenstern" }; + static constexpr std::string_view bow{ "Bow" }; + + static constexpr std::string_view chainmail{ "Chainmail" }; + static constexpr std::string_view cooking_pot{ "Cooking pot" }; + static constexpr std::string_view medic_kit{ "Medic kit" }; + + static constexpr std::string_view fourche{ "Fourche" }; + static constexpr std::string_view dagger{ "Dagger" }; + static constexpr std::string_view rock_climbing_kit{ "Rock climbing kit" }; + static constexpr std::string_view sack_of_grain{ "Sack of grain" }; + + static std::string_view billy_object_to_string(const billyObject &object) noexcept; + + void add_object(const billyObject &object, CharacterSheet &sheet) noexcept; + + void insert_weapon(weapons weapon, CharacterSheet &sheet) noexcept; + + private: + container objects; + std::plus plus; + std::minus minus; + std::uint8_t end_object{ 0 }; + + static void change_carac_weapon(CharacterSheet &sheet, + const weapons &arg, + characteristic::Characteristic &localHabilete, + characteristic::Characteristic &localAdresse, + characteristic::Characteristic &localEndurance, + const std::function &operation) noexcept; + + static void change_carac_equipment(const equipments &arg, + CharacterSheet &sheet, + characteristic::Characteristic &localHabilete, + characteristic::Characteristic &localAdresse, + characteristic::Characteristic &localEndurance, + characteristic::Characteristic &localChance, + const std::function &primary, + const std::function &complement) noexcept; + + static void change_carac_tools(const tools &arg, + CharacterSheet &sheet, + characteristic::Characteristic &localHabilete, + characteristic::Characteristic &localAdresse, + characteristic::Characteristic &localEndurance, + characteristic::Characteristic &localChance, + const std::function &operation) noexcept; + }; +} + +#endif //BILLYSHEET_BILLY_OBJECTS_HPP diff --git a/include/characteristic/chance.hpp b/include/characteristic/chance.hpp index 7e36426..aed65ee 100644 --- a/include/characteristic/chance.hpp +++ b/include/characteristic/chance.hpp @@ -8,6 +8,9 @@ namespace character::characteristic { public: Chance() noexcept: Characteristic(3, 0, 0, 0) {} + Chance(const std::uint32_t carac, const std::uint32_t materiel, const std::uint32_t additional) : + Characteristic(3, carac, materiel, additional) {} + ~Chance() noexcept final = default; friend void from_json(const json &j, Chance &chance) { diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp new file mode 100644 index 0000000..1ce312b --- /dev/null +++ b/src/billy_objects.cpp @@ -0,0 +1,169 @@ +// +// Created by postaron on 23/02/24. +// +#include "billy_objects.hpp" +#include "characteristic/characteristic.hpp" +#include "character_sheet.hpp" + +namespace character { + using characteristic::Characteristic; + + void BillyObjects::add_object(const billyObject &object, CharacterSheet &sheet) noexcept { + if (end_object < 3) { + objects.at(end_object) = object; + + auto &local_habilete = static_cast(sheet.habilete); + auto &local_adresse = static_cast(sheet.adresse); + auto &local_endurance = static_cast(sheet.endurance); + auto &local_chance = static_cast(sheet.chance); + + std::visit(overloaded{ + [&](const weapons &arg) { + ++sheet.nb_weapons; + change_carac_weapon(sheet, arg, local_habilete, local_adresse, local_endurance, plus); + }, + [&](const equipments &arg) { + ++sheet.nb_equipments; + change_carac_equipment(arg, + sheet, + local_habilete, + local_adresse, + local_endurance, + local_chance, + plus, + minus); + }, + [&](const tools &arg) { + ++sheet.nb_tools; + change_carac_tools(arg, + sheet, + local_habilete, + local_adresse, + local_endurance, + local_chance, + plus); + }, + }, object); + } + } + + void BillyObjects::change_carac_tools(const tools &arg, + CharacterSheet &sheet, + Characteristic &localHabilete, + Characteristic &localAdresse, + Characteristic &localEndurance, + Characteristic &localChance, + const std::function &operation) noexcept { + switch (arg) { + case tools::Fourche: + localHabilete.materiel = operation(localHabilete.materiel, 1); + localEndurance.materiel = operation(localEndurance.materiel, 3); + break; + case tools::Dagger: + break; + case tools::RockClimbingKit: + localAdresse.materiel = operation(localAdresse.materiel, 1); + break; + case tools::SackOfGrain: + localEndurance.materiel = operation(localEndurance.materiel, 2); + localChance.materiel = operation(localChance.materiel, 2); + break; + } + } + + void BillyObjects::change_carac_equipment(const equipments &arg, + CharacterSheet &sheet, + Characteristic &localHabilete, + Characteristic &localAdresse, + Characteristic &localEndurance, + Characteristic &localChance, + const std::function &primary, + const std::function &complement) noexcept { + switch (arg) { + case equipments::Chainmail: + localHabilete.materiel = complement(localHabilete.materiel, 1); + localAdresse.materiel = complement(localAdresse.materiel, 1); + localEndurance.materiel = primary(localEndurance.materiel, 1); + sheet.armor = primary(sheet.armor, 2); + break; + case equipments::CookingPot: + localEndurance.materiel = primary(localEndurance.materiel, 2); + sheet.armor = primary(sheet.armor, 1); + break; + case equipments::PamphletTourist: + localChance.materiel = primary(localChance.materiel, 4); + break; + case equipments::MedicKit: + localChance.materiel = primary(localChance.materiel, 1); + break; + } + } + + void BillyObjects::change_carac_weapon(CharacterSheet &sheet, + const weapons &arg, + Characteristic &localHabilete, + Characteristic &localAdresse, + Characteristic &localEndurance, + const std::function &operation) noexcept { + switch (arg) { + case weapons::Sword: + localHabilete.materiel = operation(localHabilete.materiel, 4); + break; + case weapons::Lance: + localHabilete.materiel = operation(localHabilete.materiel, 3); + localAdresse.materiel = operation(localAdresse.materiel, 1); + break; + case weapons::Morgenstern: + localHabilete.materiel = operation(localHabilete.materiel, 1); + localEndurance.materiel = operation(localEndurance.materiel, 1); + sheet.damage = operation(sheet.damage, 1); + break; + case weapons::Bow: + localHabilete.materiel = operation(localHabilete.materiel, 3); + localAdresse.materiel = operation(localAdresse.materiel, 1); + break; + } + } + + std::string_view BillyObjects::billy_object_to_string(const billyObject &object) noexcept { + return std::visit(overloaded{ + [](const weapons &arg) { + switch (arg) { + case weapons::Sword: + return sword; + case weapons::Lance: + return lance; + case weapons::Morgenstern: + return morgenstern; + case weapons::Bow: + return bow; + } + }, + [](const equipments &arg) { + switch (arg) { + case equipments::Chainmail: + return chainmail; + case equipments::CookingPot: + return cooking_pot; + case equipments::MedicKit: + return medic_kit; + } + }, + [](const tools &arg) { + switch (arg) { + case tools::Fourche: + return fourche; + case tools::Dagger: + return dagger; + case tools::RockClimbingKit: + return rock_climbing_kit; + case tools::SackOfGrain: + return sack_of_grain; + } + } + }, object); + } +} \ No newline at end of file