载入中...
搜索中...
未找到
RPGActor.cpp
浏览该文件的文档.
1#include "rpg/RPGActor.h"
3#include "rpg/StatusSystem.h"
4#include "rpg/SkillSystem.h"
5
6#include <algorithm>
7
8namespace eve::rpg {
9
10namespace {
11
12// 按 handle(id + generation)而非裸指针跟踪存活 actor:
13// ECS 的 entity 槽位在销毁后会被复用,复用后地址相同但其实是另一个实体,
14// 裸指针 + is_entity_visible 无法区分这种复用(复用后 flags 会被重置,
15// 旧的裸指针看起来又"活"了),必须连同 generation 一起校验才安全。
16std::vector<ecs::EntityHandle> &registryStorage() {
17 static std::vector<ecs::EntityHandle> v;
18 return v;
19}
20
21} // namespace
22
24 RPGActor *actor = RPGActor::create();
25 // Touch each component once so accessors are cheap/valid immediately.
26 actor->attributes();
27 actor->statuses();
28 actor->skills();
29 registryStorage().push_back(ecs::handle_of(actor));
30 return actor;
31}
32
33const std::vector<RPGActor *> &RPGActor::liveActors() {
34 static std::vector<RPGActor *> resolved;
35 auto &handles = registryStorage();
36
37 handles.erase(std::remove_if(handles.begin(), handles.end(),
38 [](const ecs::EntityHandle &h) { return ecs::try_get(h) == nullptr; }),
39 handles.end());
40
41 resolved.clear();
42 resolved.reserve(handles.size());
43 for (const ecs::EntityHandle &h : handles)
44 resolved.push_back(static_cast<RPGActor *>(ecs::try_get(h)));
45 return resolved;
46}
47
48// ---- Attributes ----
49
50void RPGActor::setBaseAttribute(const std::string &attribute, double value) {
51 AttributeSystem::setBase(this, attribute, value);
52}
53
54double RPGActor::getBaseAttribute(const std::string &attribute) {
55 return AttributeSystem::getBase(this, attribute);
56}
57
58void RPGActor::modifyBaseAttribute(const std::string &attribute, double delta) {
59 AttributeSystem::modifyBase(this, attribute, delta);
60}
61
62bool RPGActor::hasAttribute(const std::string &attribute) {
63 return AttributeSystem::hasAttribute(this, attribute);
64}
65
66std::string RPGActor::addAttributeModifier(const std::string &attribute, const std::string &source,
67 const std::string &op, double value, int priority) {
68 return AttributeSystem::addModifier(this, attribute, source, op, value, priority);
69}
70
71bool RPGActor::removeAttributeModifier(const std::string &attribute, const std::string &modifierId) {
72 return AttributeSystem::removeModifier(this, attribute, modifierId);
73}
74
75int RPGActor::removeAttributeModifiersBySource(const std::string &attribute,
76 const std::string &source) {
77 return AttributeSystem::removeModifiersBySource(this, attribute, source);
78}
79
80int RPGActor::removeAllAttributeModifiersBySource(const std::string &source) {
82}
83
84double RPGActor::getFinalAttribute(const std::string &attribute) {
85 return AttributeSystem::getFinal(this, attribute);
86}
87
88// ---- Status ----
89
90int RPGActor::applyEffect(const std::string &effectId, const std::string &source) {
91 return StatusSystem::apply(this, effectId, source);
92}
93
94bool RPGActor::removeStatus(int instanceId) { return StatusSystem::remove(this, instanceId); }
95
96int RPGActor::removeStatusByEffect(const std::string &effectId) {
97 return StatusSystem::removeByEffect(this, effectId);
98}
99
100int RPGActor::removeStatusBySource(const std::string &source) {
101 return StatusSystem::removeBySource(this, source);
102}
103
104int RPGActor::removeStatusByTag(const std::string &tag) {
105 return StatusSystem::removeByTag(this, tag);
106}
107
108bool RPGActor::hasEffect(const std::string &effectId) { return StatusSystem::hasEffect(this, effectId); }
109
110bool RPGActor::hasStatusTag(const std::string &tag) { return StatusSystem::hasTag(this, tag); }
111
113
114std::string RPGActor::getStatusEffectId(int index) {
115 return StatusSystem::getActiveEffectId(this, index);
116}
117
118int RPGActor::getStatusStacks(int index) { return StatusSystem::getActiveStacks(this, index); }
119
120float RPGActor::getStatusRemaining(int index) { return StatusSystem::getActiveRemaining(this, index); }
121
123
124std::string RPGActor::getStatusSource(int index) { return StatusSystem::getActiveSource(this, index); }
125
126std::string RPGActor::getStatusProp(int instanceId, const std::string &key,
127 const std::string &fallback) {
128 return StatusSystem::getProp(this, instanceId, key, fallback);
129}
130
131bool RPGActor::setStatusProp(int instanceId, const std::string &key, const std::string &value) {
132 return StatusSystem::setProp(this, instanceId, key, value);
133}
134
135int RPGActor::applyBuff(const std::string &effectId, const std::string &source) {
136 return applyEffect(effectId, source);
137}
138
139bool RPGActor::removeBuff(int instanceId) { return removeStatus(instanceId); }
140
141int RPGActor::removeBuffByEffect(const std::string &effectId) {
142 return removeStatusByEffect(effectId);
143}
144
145int RPGActor::removeBuffBySource(const std::string &source) {
146 return removeStatusBySource(source);
147}
148
149int RPGActor::removeBuffByTag(const std::string &tag) { return removeStatusByTag(tag); }
150
151bool RPGActor::hasBuff(const std::string &effectId) { return hasEffect(effectId); }
152
153bool RPGActor::hasBuffTag(const std::string &tag) { return hasStatusTag(tag); }
154
156
157std::string RPGActor::getBuffEffectId(int index) { return getStatusEffectId(index); }
158
159int RPGActor::getBuffStacks(int index) { return getStatusStacks(index); }
160
161float RPGActor::getBuffRemaining(int index) { return getStatusRemaining(index); }
162
163int RPGActor::getBuffInstanceId(int index) { return getStatusInstanceId(index); }
164
165std::string RPGActor::getBuffSource(int index) { return getStatusSource(index); }
166
167std::string RPGActor::getBuffProp(int instanceId, const std::string &key,
168 const std::string &fallback) {
169 return getStatusProp(instanceId, key, fallback);
170}
171
172bool RPGActor::setBuffProp(int instanceId, const std::string &key, const std::string &value) {
173 return setStatusProp(instanceId, key, value);
174}
175
176// ---- Skills ----
177
178void RPGActor::learnSkill(const std::string &skillId) { SkillSystem::learn(this, skillId); }
179
180bool RPGActor::knowsSkill(const std::string &skillId) { return SkillSystem::knows(this, skillId); }
181
182bool RPGActor::forgetSkill(const std::string &skillId) { return SkillSystem::forget(this, skillId); }
183
184float RPGActor::getSkillCooldown(const std::string &skillId) {
185 return SkillSystem::getCooldownRemaining(this, skillId);
186}
187
188void RPGActor::setSkillCooldown(const std::string &skillId, float seconds) {
189 SkillSystem::setCooldownRemaining(this, skillId, seconds);
190}
191
192bool RPGActor::canCastSkill(const std::string &skillId) {
193 return SkillSystem::canCast(this, skillId, nullptr);
194}
195
196std::string RPGActor::canCastSkillReason(const std::string &skillId) {
197 std::string reason;
198 SkillSystem::canCast(this, skillId, &reason);
199 return reason;
200}
201
202bool RPGActor::beginCastSkill(const std::string &skillId, RPGActor *target) {
203 return SkillSystem::beginCast(this, skillId, target, nullptr);
204}
205
207
209
211
213
214} // namespace eve::rpg
int priority
std::string value
int h
int v
static std::string addModifier(RPGActor *actor, const std::string &attribute, const std::string &source, const std::string &op, double value, int priority=0)
添加一条修改器,返回自动生成的唯一 id(用于之后精确移除)。 空字符串 id 表示失败(actor 为空)。
static void setBase(RPGActor *actor, const std::string &attribute, double value)
设置基础值(不存在则创建该属性)。
static bool removeModifier(RPGActor *actor, const std::string &attribute, const std::string &modifierId)
按 id 精确移除;返回是否命中。
static bool hasAttribute(RPGActor *actor, const std::string &attribute)
static void modifyBase(RPGActor *actor, const std::string &attribute, double delta)
增量修改基础值(如自然回蓝、升级加点)。
static double getBase(RPGActor *actor, const std::string &attribute)
static int removeAllModifiersBySource(RPGActor *actor, const std::string &source)
按来源标签批量移除该 actor 所有属性上的修改器;返回移除数量。
static double getFinal(RPGActor *actor, const std::string &attribute)
计算并返回最终值(内部带缓存,属性未变化时不会重复计算)。
static int removeModifiersBySource(RPGActor *actor, const std::string &attribute, const std::string &source)
按来源标签批量移除某个属性上的修改器;返回移除数量。
属性 / 状态 / 技能三表合一的 ECS 实体。
Definition RPGActor.h:28
bool beginCastSkill(const std::string &skillId, RPGActor *target=nullptr)
开始读条释放;false 表示不满足条件。
Definition RPGActor.cpp:202
std::string getCastingSkillId()
Definition RPGActor.cpp:210
bool isCastingSkill()
读条状态查询。
Definition RPGActor.cpp:208
double getBaseAttribute(const std::string &attribute)
Definition RPGActor.cpp:54
bool removeBuff(int instanceId)
Definition RPGActor.cpp:139
bool removeStatus(int instanceId)
Definition RPGActor.cpp:94
static const std::vector< RPGActor * > & liveActors()
返回所有通过 createActor() 创建、且当前仍存活的 actor。 StatusSystem::update / SkillSystem::update 用它遍历所有 actor 逐帧推进。 ...
Definition RPGActor.cpp:33
int getBuffStacks(int index)
Definition RPGActor.cpp:159
std::string canCastSkillReason(const std::string &skillId)
不能释放的原因("" = 可以)。
Definition RPGActor.cpp:196
std::string getStatusEffectId(int index)
Definition RPGActor.cpp:114
int removeAttributeModifiersBySource(const std::string &attribute, const std::string &source)
Definition RPGActor.cpp:75
void cancelCastSkill()
取消读条。
Definition RPGActor.cpp:206
int removeAllAttributeModifiersBySource(const std::string &source)
Definition RPGActor.cpp:80
static RPGActor * createActor()
创建一个空白 actor(三张表均为空,按需惰性写入),并纳入 liveActors() 跟踪。
Definition RPGActor.cpp:23
int removeStatusByTag(const std::string &tag)
Definition RPGActor.cpp:104
void setSkillCooldown(const std::string &skillId, float seconds)
Definition RPGActor.cpp:188
int removeStatusBySource(const std::string &source)
Definition RPGActor.cpp:100
std::string getBuffEffectId(int index)
Definition RPGActor.cpp:157
bool hasAttribute(const std::string &attribute)
Definition RPGActor.cpp:62
bool forgetSkill(const std::string &skillId)
Definition RPGActor.cpp:182
float getCastProgress()
Definition RPGActor.cpp:212
void learnSkill(const std::string &skillId)
技能:学习 / 查询 / 冷却。
Definition RPGActor.cpp:178
int getBuffInstanceId(int index)
Definition RPGActor.cpp:163
std::string getBuffProp(int instanceId, const std::string &key, const std::string &fallback={})
Definition RPGActor.cpp:167
int applyBuff(const std::string &effectId, const std::string &source="")
Buff 别名(与 Status API 等价,便于游戏侧按习惯命名)。
Definition RPGActor.cpp:135
bool canCastSkill(const std::string &skillId)
Definition RPGActor.cpp:192
float getBuffRemaining(int index)
Definition RPGActor.cpp:161
void setBaseAttribute(const std::string &attribute, double value)
便捷成员方法(转发到 AttributeSystem / StatusSystem / SkillSystem)。 提供这一层薄封装主要是为了让脚本可以写 actor....
Definition RPGActor.cpp:50
int getStatusStacks(int index)
Definition RPGActor.cpp:118
void modifyBaseAttribute(const std::string &attribute, double delta)
Definition RPGActor.cpp:58
bool knowsSkill(const std::string &skillId)
Definition RPGActor.cpp:180
int removeBuffByEffect(const std::string &effectId)
Definition RPGActor.cpp:141
bool hasStatusTag(const std::string &tag)
Definition RPGActor.cpp:110
std::string addAttributeModifier(const std::string &attribute, const std::string &source, const std::string &op, double value, int priority=0)
Definition RPGActor.cpp:66
int removeStatusByEffect(const std::string &effectId)
Definition RPGActor.cpp:96
int removeBuffBySource(const std::string &source)
Definition RPGActor.cpp:145
bool setBuffProp(int instanceId, const std::string &key, const std::string &value)
Definition RPGActor.cpp:172
bool hasBuff(const std::string &effectId)
Definition RPGActor.cpp:151
int getStatusInstanceId(int index)
Definition RPGActor.cpp:122
bool setStatusProp(int instanceId, const std::string &key, const std::string &value)
Definition RPGActor.cpp:131
std::string getStatusSource(int index)
Definition RPGActor.cpp:124
bool hasBuffTag(const std::string &tag)
Definition RPGActor.cpp:153
double getFinalAttribute(const std::string &attribute)
Definition RPGActor.cpp:84
std::string getStatusProp(int instanceId, const std::string &key, const std::string &fallback={})
Definition RPGActor.cpp:126
float getStatusRemaining(int index)
Definition RPGActor.cpp:120
int applyEffect(const std::string &effectId, const std::string &source="")
状态(buff/debuff)—— Statuses ECS 组件的薄封装。
Definition RPGActor.cpp:90
bool hasEffect(const std::string &effectId)
Definition RPGActor.cpp:108
int removeBuffByTag(const std::string &tag)
Definition RPGActor.cpp:149
std::string getBuffSource(int index)
Definition RPGActor.cpp:165
float getSkillCooldown(const std::string &skillId)
Definition RPGActor.cpp:184
bool removeAttributeModifier(const std::string &attribute, const std::string &modifierId)
Definition RPGActor.cpp:71
static bool forget(RPGActor *actor, const std::string &skillId)
static void cancelCast(RPGActor *actor)
打断当前读条(不退还已扣除的消耗/冷却)。
static std::string getCastingSkillId(RPGActor *actor)
static float getCastProgress(RPGActor *actor)
0..1,非读条状态返回 0
static void setCooldownRemaining(RPGActor *actor, const std::string &skillId, float seconds)
static bool isCasting(RPGActor *actor)
static float getCooldownRemaining(RPGActor *actor, const std::string &skillId)
static void learn(RPGActor *actor, const std::string &skillId)
static bool knows(RPGActor *actor, const std::string &skillId)
static bool canCast(RPGActor *actor, const std::string &skillId, std::string *reason=nullptr)
检查冷却/消耗/学会状态/自定义条件,不产生任何副作用。
static bool beginCast(RPGActor *actor, const std::string &skillId, RPGActor *target=nullptr, std::string *reason=nullptr)
尝试释放技能:通过 canCast 检查后立即扣除消耗并进入冷却; castTime<=0 时立即结算(授予效果 + 产生 SkillCastEvent), 否则进入读条状态,由 update() 在读...
static bool setProp(RPGActor *actor, int instanceId, const std::string &key, const std::string &value)
static std::string getActiveSource(RPGActor *actor, int index)
static int apply(RPGActor *actor, const std::string &effectId, const std::string &source="")
施加一个效果。返回值: -1 — 失败(actor 为空 / 效果不存在 / 条件拒绝 / stackPolicy 拒绝重复) 0 — durationPolicy=="instant":已立即生效,无...
static int getActiveStacks(RPGActor *actor, int index)
static int getActiveInstanceId(RPGActor *actor, int index)
static bool hasEffect(RPGActor *actor, const std::string &effectId)
static bool hasTag(RPGActor *actor, const std::string &tag)
是否存在任一效果定义带有该 tag 的活动实例。
static int removeBySource(RPGActor *actor, const std::string &source)
移除该 actor 身上所有 source 匹配的实例;返回移除数量。
static bool remove(RPGActor *actor, int instanceId)
按实例 id 精确移除(撤销其属性修改器);返回是否命中。
static int getActiveCount(RPGActor *actor)
static std::string getProp(RPGActor *actor, int instanceId, const std::string &key, const std::string &fallback={})
按实例 id 读写 props;找不到实例时 get 返回 fallback,set 返回 false。
static int removeByEffect(RPGActor *actor, const std::string &effectId)
移除该 actor 身上所有 effectId 匹配的实例;返回移除数量。
static int removeByTag(RPGActor *actor, const std::string &tag)
移除该 actor 身上所有 tag 匹配(效果定义带该 tag)的实例;返回移除数量。
static std::string getActiveEffectId(RPGActor *actor, int index)
static float getActiveRemaining(RPGActor *actor, int index)
RPG 模块入口:属性 / 效果 / 状态 / 技能 / 结算五套系统的脚本绑定与帧调度点。