aboutsummaryrefslogtreecommitdiff
path: root/src/spellcasting.h
diff options
context:
space:
mode:
authorYour Name <you@example.com>2021-05-09 19:01:59 -0400
committerYour Name <you@example.com>2021-05-09 19:01:59 -0400
commite044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd (patch)
tree344c09421c5839a764a132fe9166f0e6e3f90e45 /src/spellcasting.h
parentd13358b71ec15085f2638fd9c3fc634df62dfc94 (diff)
downloaddmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.tar.gz
dmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.tar.bz2
dmtool-e044fc4255aa64ef1dbc3d20ed87ed6e2f61a6bd.zip
Code refactoring
Diffstat (limited to 'src/spellcasting.h')
-rw-r--r--src/spellcasting.h53
1 files changed, 18 insertions, 35 deletions
diff --git a/src/spellcasting.h b/src/spellcasting.h
index bb99756..f5cebcb 100644
--- a/src/spellcasting.h
+++ b/src/spellcasting.h
@@ -1,62 +1,45 @@
#pragma once
#include "feature.h"
-#include "json.hpp"
#include "spell.h"
-#include "jsonable.h"
#include "rules.h"
#include "utils.h"
#include <memory>
+#include <nlohmann/json.hpp>
typedef nlohmann::json json;
namespace entry {
- struct SlotLevel : public Jsonable {
- SlotLevel(const json& data) : numSlots(data["slots"]), spells(utils::instantiateNames<Spell>("spells", data["spells"])) {}
+ struct SlotLevel;
+
+ void to_json(nlohmann::json& j, const SlotLevel& sl);
+ void from_json(const nlohmann::json& j, SlotLevel& sl);
+
+ struct SlotLevel {
SlotLevel() : numSlots(0) {}
- virtual ~SlotLevel() {}
int numSlots;
std::vector<std::shared_ptr<Spell>> spells;
static std::shared_ptr<SlotLevel> create(const nlohmann::json& data);
-
- json toJson(void) const {
- std::vector<std::string> s;
- for(auto spell : spells) {
- s.push_back(spell->getName());
- }
- return json({
- {"slots", numSlots},
- {"spells", s}
- });
+ nlohmann::json serialize() {
+ nlohmann::json ret;
+ to_json(ret, *this);
+ return ret;
}
};
class Spellcasting : public Feature {
public:
- Spellcasting(const json& data, const json& base) : Feature(base), innate(data["innate"]), ability(rules::Ability(data["spellcasting_ability"])), spellsBySlot(data["levels"]) {}
- // Can also be instantiated programatically
- Spellcasting(const std::string& entry, const std::string& name, const std::string& type, const std::string& text, const rules::Ability& ability, bool isInnate) : Feature(entry, name, type, text), innate(isInnate), ability(ability) {}
- virtual ~Spellcasting() {}
-
bool isInnate(void) const {return innate;}
- rules::Ability getAbility(void) const {return ability;}
- const std::vector<std::shared_ptr<SlotLevel>>& getSlotLevels(void) const {return spellsBySlot;}
- void addSlotLevel(void) {spellsBySlot.push_back(std::shared_ptr<SlotLevel>(new SlotLevel()));}
+ rules::Ability getAbility(void) const {return spellcasting_ability;}
+ const std::vector<std::shared_ptr<SlotLevel>>& getSlotLevels(void) const {return levels;}
+ void addSlotLevel(void) {levels.push_back(std::shared_ptr<SlotLevel>(new SlotLevel()));}
std::vector<std::shared_ptr<Spell>> getSpells(void) const;
virtual std::string getText(const creature::Creature& c) const;
- virtual json toJson(void) const {
- auto data = Feature::toJson();
- data["innate"] = innate;
- data["spellcasting_ability"] = ability;
- data["levels"] = spellsBySlot;
- return data;
- }
+ NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Feature, Spellcasting, innate, spellcasting_ability, levels);
private:
- const bool innate;
- const rules::Ability ability;
- std::vector<std::shared_ptr<SlotLevel>> spellsBySlot;
+ bool innate;
+ rules::Ability spellcasting_ability;
+ std::vector<std::shared_ptr<SlotLevel>> levels;
};
-
- std::string genText(const Spellcasting& s, const creature::Creature& c);
}