aboutsummaryrefslogtreecommitdiff
path: root/src/item.cc
blob: 24498db5add79897f05d086260829f994cbcf2b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "item.h"
#include "weapon.h"
#include "armor.h"
#include "utils.h"
#include <iostream>
#include <memory>
#include <sstream>
#include <nlohmann/json.hpp>

using namespace std;

namespace entry {
    shared_ptr<Item> Item::create(const nlohmann::json& data) {
        if(data["type"] == "weapons" || data["type"] == "spell attack") {
            auto w = utils::loadDFromJson<Item, Weapon>(data);
            if(! data["text"].empty()) {
                w->Entry::setText(data["text"]);
            }
            return w;
        } else if(data["type"] == "armor") {
            return utils::loadDFromJson<Item, Armor>(data);
        }
        return utils::loadDFromJson<Item, Item>(data);
    }

    struct itemImpl {
        int cost;
        double weight;
    };
    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(itemImpl, cost, weight);

    NLOHMANN_FRIEND_DEFS(Entry, Item, data);

    Item::Item() : data(new itemImpl()) {}

    int Item::getCost() const {return data->cost;}

    double Item::getWeight() const {return data->weight;}

    string Item::getCostWeightText() const {
        stringstream text;
        if(getCost() >= 0) {
            text << "Cost: ";
            string costStr = to_string(getCost()) + " cp";
            text << costStr;
            string condensedCostStr = utils::getCostString(getCost());
            if(costStr != condensedCostStr) {
                text << ", i.e., " << condensedCostStr;
            }
        }
        if(getWeight() >= 0) {
            text << ". Weight: " << getWeight() << " lbs.";
        }
        return text.str();
    }

    string Item::getText() const {
        return Entry::getText() + " " + getCostWeightText();
    }

    string Item::getText(const creature::Creature& c) const {
        return getText();
    }
}