载入中...
搜索中...
未找到
Skill.cpp
浏览该文件的文档.
1#include "rpg/Skill.h"
2
3#include "common/Json.h"
4
5#include <algorithm>
6#include <unordered_map>
7
8namespace eve::rpg {
9
11
12bool SkillDefinition::hasTag(const std::string &tag) const {
13 return std::find(tags.begin(), tags.end(), tag) != tags.end();
14}
15
16namespace {
17std::unordered_map<std::string, SkillDefinition> &table() {
18 static std::unordered_map<std::string, SkillDefinition> t;
19 return t;
20}
21} // namespace
22
24 if (def.id.empty()) return;
25 table()[def.id] = def;
26}
27
28const SkillDefinition *SkillRegistry::find(const std::string &id) {
29 auto &t = table();
30 auto it = t.find(id);
31 return it == t.end() ? nullptr : &it->second;
32}
33
34bool SkillRegistry::remove(const std::string &id) { return table().erase(id) > 0; }
35
36void SkillRegistry::clear() { table().clear(); }
37
38int SkillRegistry::count() { return int(table().size()); }
39
40namespace {
41
42SkillDefinition parseSkillObject(Value o) {
44 if (!o.isObject()) return def;
45 def.id = o.getString("id");
46 def.cooldown = o.getFloat("cooldown", 0.f);
47 def.castTime = o.getFloat("castTime", 0.f);
48 def.targetType = o.getString("targetType", "self");
49 def.grantedEffects = o.getStringArray("grantedEffects");
50 def.tags = o.getStringArray("tags");
51
52 const Value costs = o.get("costs");
53 for (size_t i = 0; i < costs.size(); ++i) {
54 const Value co = costs.at(i);
55 SkillCostSpec spec;
56 spec.attribute = co.getString("attribute");
57 spec.amount = co.getDouble("amount", 0.0);
58 if (!spec.attribute.empty()) def.costs.push_back(std::move(spec));
59 }
60
61 def.extra = o.getStringMap("extra");
62 return def;
63}
64
65} // namespace
66
67int SkillRegistry::loadFromJson(const std::string &json, std::string *error) {
68 std::string err;
69 const eve::json::Document doc = eve::json::Document::parse(json, &err);
70 if (!doc.valid()) {
71 if (error) *error = err.empty() ? "invalid json" : err;
72 return 0;
73 }
74
75 const Value root = doc.root();
76 int n = 0;
77 if (root.isArray()) {
78 for (size_t i = 0; i < root.size(); ++i) {
79 SkillDefinition def = parseSkillObject(root.at(i));
80 if (def.id.empty()) continue;
81 registerSkill(def);
82 ++n;
83 }
84 } else if (root.isObject()) {
85 SkillDefinition def = parseSkillObject(root);
86 if (!def.id.empty()) {
87 registerSkill(def);
88 ++n;
89 }
90 }
91 return n;
92}
93
94} // namespace eve::rpg
glm::vec3 n
Definition Grass.cpp:64
std::string error
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
Value get(const char *key) const
Definition Json.cpp:301
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
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
double getDouble(const char *key, double fallback=0.0) const
Definition Json.cpp:387
std::unordered_map< std::string, std::string > getStringMap(const char *key) const
Definition Json.cpp:424
static const SkillDefinition * find(const std::string &id)
Definition Skill.cpp:28
static int loadFromJson(const std::string &json, std::string *error=nullptr)
从 JSON 数组批量注册技能定义,返回成功注册数量。元素形如: { "id": "fireball", "cooldown": 4, "castTime": 1....
Definition Skill.cpp:67
static void registerSkill(const SkillDefinition &def)
Definition Skill.cpp:23
static bool remove(const std::string &id)
Definition Skill.cpp:34
static void clear()
Definition Skill.cpp:36
static int count()
Definition Skill.cpp:38
RPG 模块入口:属性 / 效果 / 状态 / 技能 / 结算五套系统的脚本绑定与帧调度点。
std::string attribute
Definition Skill.h:20
std::vector< std::string > tags
Definition Skill.h:31
float castTime
0 = 瞬发
Definition Skill.h:27
std::vector< SkillCostSpec > costs
Definition Skill.h:29
bool hasTag(const std::string &tag) const
Definition Skill.cpp:12
std::string targetType
Definition Skill.h:28
std::vector< std::string > grantedEffects
命中/释放后施加给目标的效果 id 列表
Definition Skill.h:30
std::unordered_map< std::string, std::string > extra
游戏自定义附加数据
Definition Skill.h:32