diff options
author | Your Name <you@example.com> | 2021-05-09 19:01:59 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-09 19:01:59 -0400 |
commit | e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd (patch) | |
tree | 344c09421c5839a764a132fe9166f0e6e3f90e45 /src/weapon.h | |
parent | d13358b71ec15085f2638fd9c3fc634df62dfc94 (diff) | |
download | dmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.tar.gz dmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.tar.bz2 dmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.zip |
Code refactoring
Diffstat (limited to 'src/weapon.h')
-rw-r--r-- | src/weapon.h | 59 |
1 files changed, 18 insertions, 41 deletions
diff --git a/src/weapon.h b/src/weapon.h index c1047bb..da6700c 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -1,7 +1,7 @@ #pragma once #include "item.h" #include "rules.h" -#include "json.hpp" +#include <nlohmann/json.hpp> #include <set> namespace creature { @@ -12,64 +12,41 @@ namespace entry { class Weapon; class Damage; - std::string genText(const Weapon& w, const creature::Creature& c); std::vector<rules::Ability> getAbilityOptions(const Weapon& w); std::vector<Damage> rollDmg(const Weapon& w, bool versatile=false); std::string formatDmg(const Weapon& w, const creature::Creature& c); - class Damage : public Jsonable { - public: - Damage(const nlohmann::json& data) : count(data["dmg_die_count"]), sides(data["dmg_die_sides"]), type(data["dmg_type"]), isOr(data["is_or"]) {} - const int count; - const int sides; - const std::string type; - const bool isOr; - int rolled = 0; - - nlohmann::json toJson(void) const { - return nlohmann::json({ - {"dmg_die_count", count}, - {"dmg_die_sides", sides}, - {"dmg_type", type}, - {"is_or", isOr} - }); - } + struct Damage { + int dmg_die_count; + int dmg_die_sides; + std::string dmg_type; + bool is_or; + int rolled = 0; }; + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Damage, dmg_die_count, dmg_die_sides, dmg_type, is_or); class Weapon : public Item, public Substantial { public: - Weapon(const nlohmann::json& data, const nlohmann::json& base) : Item(base), damage(utils::json2vec<Damage>(data["damage"])), properties(data["properties"]), weaponType(data["weapon_type"]), range(data["range"][0], data["range"][1]), reach(data["reach"]), cost(data["cost"]), weight(data["weight"]) {} - std::vector<Damage> getDamage(void) const {return damage;} std::set<std::string> getProperties(void) const {return properties;} - std::string getWeaponType(void) const {return weaponType;} + std::string getWeaponType(void) const {return weapon_type;} std::pair<int, int> getRange(void) const {return range;} int getReach(void) const {return reach;} int getCost(void) const {return cost;} double getWeight(void) const {return weight;} virtual std::string getText() const override; - virtual std::string getText(const creature::Creature& c) const override {return genText(*this, c);} + virtual std::string getText(const creature::Creature& c) const override; - virtual nlohmann::json toJson(void) const { - auto data = Item::toJson(); - data["damage"] = damage; - data["properties"] = properties; - data["weapon_type"] = weaponType; - data["range"] = range; - data["reach"] = reach; - data["cost"] = cost; - data["weight"] = weight; - return data; - } + NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Item, Weapon, damage, properties, weapon_type, range, reach, cost, weight); private: - const std::vector<Damage> damage; - const std::set<std::string> properties; - const std::string weaponType; - const std::pair<const int, const int> range; - const int reach; - const int cost; - const double weight; + std::vector<Damage> damage; + std::set<std::string> properties; + std::string weapon_type; + std::pair<int, int> range; + int reach; + int cost; + double weight; }; } |