载入中...
搜索中...
未找到
Item.cpp
浏览该文件的文档.
1#include "inventory/Item.h"
2
3#include "common/Json.h"
4
5#include <algorithm>
6
7namespace eve::inventory {
8
10
11bool ItemDefinition::hasTag(const std::string &tag) const {
12 return std::find(tags.begin(), tags.end(), tag) != tags.end();
13}
14
15std::string ItemDefinition::getExtra(const std::string &key, const std::string &fallback) const {
16 auto it = extra.find(key);
17 return it == extra.end() ? fallback : it->second;
18}
19
21 instanceId = 0;
22 itemId.clear();
23 quantity = 0;
24 durability = -1.f;
25 props.clear();
26 tags.clear();
27}
28
29bool ItemStack::hasTag(const std::string &tag) const {
30 return std::find(tags.begin(), tags.end(), tag) != tags.end();
31}
32
33std::string ItemStack::getProp(const std::string &key, const std::string &fallback) const {
34 auto it = props.find(key);
35 return it == props.end() ? fallback : it->second;
36}
37
38void ItemStack::setProp(const std::string &key, const std::string &value) { props[key] = value; }
39
40std::unordered_map<std::string, ItemDefinition> &ItemRegistry::table() {
41 static std::unordered_map<std::string, ItemDefinition> t;
42 return t;
43}
44
46 if (def.id.empty()) return;
47 ItemDefinition copy = def;
48 if (copy.maxStack <= 0) copy.maxStack = 1;
49 if (copy.displayName.empty()) copy.displayName = copy.id;
50 table()[copy.id] = std::move(copy);
51}
52
53const ItemDefinition *ItemRegistry::find(const std::string &id) {
54 auto &t = table();
55 auto it = t.find(id);
56 return it == t.end() ? nullptr : &it->second;
57}
58
59bool ItemRegistry::remove(const std::string &id) { return table().erase(id) > 0; }
60
61void ItemRegistry::clear() { table().clear(); }
62
63int ItemRegistry::count() { return int(table().size()); }
64
65namespace {
66
67ItemDefinition parseItemObject(Value o) {
69 if (!o.isObject()) return def;
70 def.id = o.getString("id");
71 def.displayName = o.getString("displayName", def.id);
72 def.maxStack = o.getInt("maxStack", 1);
73 def.weight = o.getFloat("weight", 0.f);
74 def.volume = o.getFloat("volume", 0.f);
75 def.tags = o.getStringArray("tags");
76 def.category = o.getString("category");
77 def.equipSlot = o.getString("equipSlot");
78 def.extra = o.getStringMap("extra");
79 return def;
80}
81
82} // namespace
83
84int ItemRegistry::loadFromJson(const std::string &json, std::string *error) {
85 std::string err;
86 const eve::json::Document doc = eve::json::Document::parse(json, &err);
87 if (!doc.valid()) {
88 if (error) *error = err.empty() ? "invalid json" : err;
89 return 0;
90 }
91
92 const Value root = doc.root();
93 int n = 0;
94 if (root.isArray()) {
95 for (size_t i = 0; i < root.size(); ++i) {
96 ItemDefinition def = parseItemObject(root.at(i));
97 if (def.id.empty()) continue;
98 registerItem(def);
99 ++n;
100 }
101 } else if (root.isObject()) {
102 ItemDefinition def = parseItemObject(root);
103 if (!def.id.empty()) {
104 registerItem(def);
105 ++n;
106 }
107 }
108 return n;
109}
110
111} // namespace eve::inventory
std::string value
glm::vec3 n
Definition Grass.cpp:64
std::string error
static bool remove(const std::string &id)
Definition Item.cpp:59
static int loadFromJson(const std::string &json, std::string *error=nullptr)
从 JSON 数组或单对象批量注册,返回成功数量。 元素形如: { "id": "potion.hp", "displayName": "治疗药水", "maxStack": 20,...
Definition Item.cpp:84
static void registerItem(const ItemDefinition &def)
Definition Item.cpp:45
static const ItemDefinition * find(const std::string &id)
Definition Item.cpp:53
bool valid() const
Definition Json.h:111
static Document parse(const std::string &text, std::string *error=nullptr)
Definition Json.cpp:449
Value root() const
Definition Json.h:112
bool isObject() const
Definition Json.cpp:296
size_t size() const
Definition Json.cpp:316
bool isArray() const
Definition Json.cpp:297
Value at(size_t index) const
Definition Json.cpp:323
float getFloat(const char *key, float fallback=0.f) const
Definition Json.cpp:386
int getInt(const char *key, int fallback=0) const
Definition Json.cpp:385
std::string getString(const char *key, const std::string &fallback={}) const
Definition Json.cpp:390
std::vector< std::string > getStringArray(const char *key) const
Definition Json.cpp:402
std::unordered_map< std::string, std::string > getStringMap(const char *key) const
Definition Json.cpp:424
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
Definition Bag.cpp:6
物品模板(进程级注册表中的定义,不含运行时数量)。
Definition ItemTypes.h:13
std::string equipSlot
非空表示建议装备槽名;是否可装备由 EquipmentSet 槽位配置最终决定。
Definition ItemTypes.h:22
std::unordered_map< std::string, std::string > extra
Definition ItemTypes.h:23
std::vector< std::string > tags
Definition ItemTypes.h:19
bool hasTag(const std::string &tag) const
Definition Item.cpp:11
std::string getExtra(const std::string &key, const std::string &fallback={}) const
Definition Item.cpp:15
bool hasTag(const std::string &tag) const
Definition Item.cpp:29
void setProp(const std::string &key, const std::string &value)
Definition Item.cpp:38
std::string getProp(const std::string &key, const std::string &fallback={}) const
Definition Item.cpp:33
std::unordered_map< std::string, std::string > props
Definition ItemTypes.h:35
float durability
< 0 表示不适用
Definition ItemTypes.h:34
std::vector< std::string > tags
Definition ItemTypes.h:36