载入中...
搜索中...
未找到
AttributeSystem.h
浏览该文件的文档.
1#pragma once
2
3// 属性系统:面向 RPGActor 的操作入口 + 自定义 op 注册表。
4//
5// 灵活性/可定制点:
6// - 属性名是任意字符串,无需预先声明。
7// - 内置 op(add / mulAdd / mulMul / override / clampMin / clampMax)覆盖常见需求;
8// 通过 registerOp() 可以在 C++ 侧注册任意自定义运算(例如"取当前生命百分比的平方"),
9// 脚本侧只需按约定的字符串名字引用即可,无需重新编译引擎。
10
11#include "rpg/AttributeTypes.h"
12
13#include <functional>
14#include <string>
15#include <unordered_map>
16
17namespace eve::rpg {
18
19class RPGActor;
20
23public:
24 using Fn = std::function<double(double current, double value)>;
25
26 void registerOp(const std::string &name, Fn fn);
27 void unregisterOp(const std::string &name);
28 bool has(const std::string &name) const;
29 double apply(const std::string &name, double current, double value) const;
30
31private:
32 std::unordered_map<std::string, Fn> ops_;
33};
34
40public:
43
45 static void setBase(RPGActor *actor, const std::string &attribute, double value);
46 static double getBase(RPGActor *actor, const std::string &attribute);
47
49 static void modifyBase(RPGActor *actor, const std::string &attribute, double delta);
50
51 static bool hasAttribute(RPGActor *actor, const std::string &attribute);
52
57 static std::string addModifier(RPGActor *actor, const std::string &attribute,
58 const std::string &source, const std::string &op,
59 double value, int priority = 0);
60
62 static bool removeModifier(RPGActor *actor, const std::string &attribute,
63 const std::string &modifierId);
64
66 static int removeModifiersBySource(RPGActor *actor, const std::string &attribute,
67 const std::string &source);
68
70 static int removeAllModifiersBySource(RPGActor *actor, const std::string &source);
71
73 static double getFinal(RPGActor *actor, const std::string &attribute);
74
76 static void invalidate(RPGActor *actor, const std::string &attribute);
77
78private:
79 static std::string nextModifierId();
80};
81
82} // namespace eve::rpg
int priority
std::string value
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
自定义 op 注册表:opName -> f(currentResult, modifierValue) -> newResult。
bool has(const std::string &name) const
void unregisterOp(const std::string &name)
void registerOp(const std::string &name, Fn fn)
std::function< double(double current, double value)> Fn
double apply(const std::string &name, double current, double value) const
面向 RPGActor 的属性操作静态入口。数据本身存放在 RPGActor::Attributes 组件里, 这里只是围绕组件的一组无状态操作函数(+ 一张全局自定义 op 表)。
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 void invalidate(RPGActor *actor, const std::string &attribute)
标记某个属性需要在下次 getFinal 时重新计算(内部使用;一般不需要手动调用)。
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)
按来源标签批量移除某个属性上的修改器;返回移除数量。
static AttributeOpTable & customOps()
全局自定义 op 注册表(进程生命周期内有效)。
属性 / 状态 / 技能三表合一的 ECS 实体。
Definition RPGActor.h:28
RPG 模块入口:属性 / 效果 / 状态 / 技能 / 结算五套系统的脚本绑定与帧调度点。