载入中...
搜索中...
未找到
VitalsSystem.cpp
浏览该文件的文档.
1#include "rpg/VitalsSystem.h"
3
5#include "rpg/RPGActor.h"
6
7#include <algorithm>
8
9namespace eve::rpg {
10
11namespace {
12std::vector<VitalsEvent>& vitalsEvents() { return detail::simulationEvents().vitals; }
13
14void emit(RPGActor *actor, std::string resource, std::string action, double amount,
15 std::string source, double current, double max) {
16 VitalsEvent ev;
17 ev.actor = actor;
18 ev.resource = std::move(resource);
19 ev.action = std::move(action);
20 ev.amount = amount;
21 ev.source = std::move(source);
22 ev.current = current;
23 ev.max = max;
24 vitalsEvents().push_back(std::move(ev));
25}
26} // namespace
27
28double VitalsSystem::getCurrent(RPGActor *actor, const std::string &resource) {
29 if (!actor) return 0.0;
30 auto it = actor->vitals()->current.find(resource);
31 return it == actor->vitals()->current.end() ? 0.0 : it->second;
32}
33
34double VitalsSystem::getMax(RPGActor *actor, const std::string &resource) {
35 return actor ? AttributeSystem::getFinal(actor, resource) : 0.0;
36}
37
38void VitalsSystem::setCurrent(RPGActor *actor, const std::string &resource, double value) {
39 if (!actor) return;
40 const double max = getMax(actor, resource);
41 actor->vitals()->current[resource] = std::clamp(value, 0.0, max);
42}
43
44double VitalsSystem::takeDamage(RPGActor *actor, const std::string &resource, double amount,
45 const std::string &source) {
46 if (!actor || amount <= 0.0) return 0.0;
47 auto &current = actor->vitals()->current;
48 const double max = getMax(actor, resource);
49 const double cur = getCurrent(actor, resource);
50 if (cur <= 0.0) return 0.0; // 已死,不再受伤
51 const double applied = std::min(amount, cur);
52 current[resource] = cur - applied;
53 emit(actor, resource, "damage", applied, source, cur - applied, max);
54 if (cur - applied <= 0.0) {
55 emit(actor, resource, "death", applied, source, 0.0, max);
56 }
57 return applied;
58}
59
60double VitalsSystem::heal(RPGActor *actor, const std::string &resource, double amount) {
61 if (!actor || amount <= 0.0) return 0.0;
62 auto &current = actor->vitals()->current;
63 const double max = getMax(actor, resource);
64 const double cur = getCurrent(actor, resource);
65 const double applied = std::min(amount, max - cur);
66 if (applied <= 0.0) return 0.0;
67 current[resource] = cur + applied;
68 emit(actor, resource, "heal", applied, "", cur + applied, max);
69 return applied;
70}
71
72void VitalsSystem::publishSettledChange(RPGActor *actor, const std::string &resource,
73 const std::string &action, double amount,
74 const std::string &source) {
75 if (!actor || amount <= 0.0 || (action != "damage" && action != "heal")) return;
76 const double current = getCurrent(actor, resource);
77 const double maximum = getMax(actor, resource);
78 emit(actor, resource, action, amount, source, current, maximum);
79 if (action == "damage" && current <= 0.0)
80 emit(actor, resource, "death", amount, source, current, maximum);
81}
82
83void VitalsSystem::revive(RPGActor *actor, const std::string &resource, double amount) {
84 if (!actor) return;
85 auto &current = actor->vitals()->current;
86 const double max = getMax(actor, resource);
87 const double value = amount >= 0.0 ? std::min(amount, max) : max;
89 emit(actor, resource, "revive", value, "", value, max);
90}
91
92bool VitalsSystem::isDead(RPGActor *actor, const std::string &resource) {
93 const double max = getMax(actor, resource);
94 return max > 0.0 && getCurrent(actor, resource) <= 0.0;
95}
96
97void VitalsSystem::pollEvents(std::vector<VitalsEvent> &out) {
98 out.clear();
99 out = std::move(vitalsEvents());
100 vitalsEvents().clear();
101}
102
103} // namespace eve::rpg
RPG's thin compatibility facade over the attributes module.
bool applied
float maximum[3]
std::string action
Definition PlayHost.cpp:118
Object::Ptr value
ScriptStructure out
std::string_view source
double current
std::string resource
当前资源(hp/mana...)的受伤 / 治疗 / 复活 / 死亡。
static double getFinal(RPGActor *actor, const std::string &attribute)
Compute the final value through the canonical attributes engine.
属性 / 状态 / 技能三表合一的 ECS 实体。
Definition RPGActor.h:34
static double getCurrent(RPGActor *actor, const std::string &resource)
当前值(无 vitals/资源时 0)。
static double takeDamage(RPGActor *actor, const std::string &resource, double amount, const std::string &source="")
造成伤害:current -= amount,下限 0;归零时发 death 事件。
static void revive(RPGActor *actor, const std::string &resource, double amount=-1.0)
复活:把资源设为 amount(默认当前上限),并清掉死亡状态。
static void setCurrent(RPGActor *actor, const std::string &resource, double value)
直接设定当前值,夹在 [0, max]。
static void pollEvents(std::vector< VitalsEvent > &out)
取出并清空自上次调用以来累积的资源事件(push/poll 风格)。
static double heal(RPGActor *actor, const std::string &resource, double amount)
治疗:current += amount,上限 max。返回实际治疗量。
static double getMax(RPGActor *actor, const std::string &resource)
上限:取 actor 同名最终属性(getFinalAttribute),无则 0。
static void publishSettledChange(RPGActor *actor, const std::string &resource, const std::string &action, double amount, const std::string &source="")
Publish resource events for a state change already committed by Settlement.
static bool isDead(RPGActor *actor, const std::string &resource)
是否死亡:max > 0 且 current <= 0。
SimulationEvents & simulationEvents()
Borrow the current thread's event owner for the immediate synchronous operation.
RPG 模块的基础实体:把属性 / 状态 / 技能三张表挂到一个 ECS 实体上。
std::vector< VitalsEvent > vitals