BillySheet/include/characteristic.hpp

134 lines
4.4 KiB
C++

#ifndef BILLYSHEET_CHARACTERISTIC_HPP
#define BILLYSHEET_CHARACTERISTIC_HPP
#include <algorithm>
#include <cstdint>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace character {
class BillyObjects;
}
namespace character::characteristic {
/*!
* \brief Different characteristic type
*/
enum class characType : std::uint8_t {
Adresse = 0,
Endurance = 1,
Chance = 2,
Habilete = 3
};
/*!
* \brief This class represents a characteristic for Billy (one of characType)
*/
class Characteristic {
protected:
friend BillyObjects;
using defaultValue = std::numeric_limits<std::int32_t>;
//! Total point for this charac
mutable std::int32_t total{ defaultValue::max() };
//! Base value
const std::uint32_t base{ 0 };
//! Value depending of the characType for this characteristic.
std::uint32_t carac{ 0 };
//! Value changed from a billyObjects (billyEnums) possessed
std::uint32_t materiel{ 0 };
//! Additional value when creating the character
std::uint32_t additional{ 0 };
/*!
* \brief Get base value depending of the characType
* \param inType Characteristic type used to get base number
* \return Base value depending of the characType
*/
static constexpr std::uint32_t get_base(const characType inType) noexcept {
switch (inType) {
case characType::Adresse:
return 1;
case characType::Chance:
return 3;
case characType::Endurance:
case characType::Habilete:
return 2;
}
}
public:
const characType type{ characType::Adresse };
constexpr Characteristic() noexcept = default;
constexpr Characteristic(const characType inType,
const std::uint32_t carac,
const std::uint32_t materiel,
const std::uint32_t additional) :
base(get_base(inType)),
carac(carac),
materiel(materiel),
additional(additional),
type(inType) { (void) get_total(); }
constexpr explicit Characteristic(const characType inType) : Characteristic(inType, 0, 0, 0) {}
constexpr Characteristic(const Characteristic &charac) noexcept = default;
Characteristic &operator=(const Characteristic &charac) noexcept {
const_cast<std::uint32_t &>(base) = charac.base;
const_cast<characType &>(type) = charac.type;
carac = charac.carac;
materiel = charac.materiel;
additional = charac.additional;
total = charac.get_total();
return *this;
}
virtual ~Characteristic() noexcept = default;
[[nodiscard]] std::uint32_t get_base() const { return base; }
[[nodiscard]] std::uint32_t get_carac() const { return carac; }
[[nodiscard]] std::uint32_t get_materiel() const { return materiel; }
[[nodiscard]] std::uint32_t get_additional() const { return additional; }
[[nodiscard]] std::size_t get_total() const noexcept {
if (total == defaultValue::max()) {
total = static_cast<std::int32_t>(base + carac + materiel + additional);
total = std::max(total, 0);
}
return total;
}
friend void from_json(const json &j, Characteristic &charac) {
const_cast<std::uint32_t &>(charac.base) = j.at("base").get<std::uint32_t>();
const_cast<characType &>(charac.type) = static_cast<characType>(j.at("type").get<std::uint8_t>());
charac.carac = j.at("carac").get<std::uint32_t>();
charac.materiel = j.at("materiel").get<std::uint32_t>();
charac.additional = j.at("additional").get<std::uint32_t>();
charac.total = j.at("total").get<std::int32_t>();
}
};
static void to_json(json &j, const Characteristic &charac) {
j["base"] = charac.get_base();
j["type"] = charac.type;
j["carac"] = charac.get_carac();
j["materiel"] = charac.get_materiel();
j["additional"] = charac.get_additional();
j["total"] = charac.get_total();
}
}
#endif //BILLYSHEET_CHARACTERISTIC_HPP