attrib_calc2
简介
属性计算模块2.0版本
属性计算器
- 本模块拥有一个或者多个属性计算器(AttribCalculator)
- 属性计算器都有一个唯一的id
- 属性计算器负责计算一组属性(固定个数)的最终值
- 属性的最终值总是float类型或者int类型中的一种
- 属性计算器通过采集多个来源的属性加成数据表,计算出属性的最终值表
属性计算单元
- 属性计算器在未通过set_calc_func提供计算能力时,默认情况下由和属性计算器id同名的属性计算单元提供计算能力
- 属性计算单元由pkg.attrib_calc2通过load_all_entry()加载
- 属性计算单元需要包含组件pkg.attrib_calc2.FAttribCalculationUnit
- 一般地,在模块setup初始化前,应该先加载属性计算单元
模块初始化(setup)参数:
- total_calculator_attrib_dict - 必有,待创建的属性计算器表
- key: 属性计算器id
- value : 属性计算器的拥有的属性表(map类型, key是属性, value:属性的最终值是不是float类型)
- total_calculator_attrib_sources_dict - 可选,属性计算器的属性加成来源表
- key: 属性计算器id
- value: 属性计算器的采集来源数组(array类型)
常规使用流程:
- 本模块加载属性计算单元(pkg.attrib_calc2.load_all_entry)
- 本模块执行初始化(setup)
组件接口
AttribCalculator.gs
属性计算器 一般的,本对象不应该被继承! 每个属性计算器对应一个特定的计算类型 并且包含:
- 固定的属性列表
- 属性加成的收集方式
- 属性最终值的计算方式 属性计算过程:
- 收集各种来源的属性加成
- 根据属性加成计算最终值
函数原型 | 函数作用 |
---|---|
void set_id(string id) | 设置id |
string get_id() | 获取id |
void register_attrib(string attrib_id, bool flag_is_float) | 登记属性以及属性的数值类型 |
void batch_register_attrib(map batch_attrib_dict) | 批量登记属性以及属性的数值类型 |
void set_collect_func(string source, function func) | 设置属性加成的采集来源以及采集函数 |
void batch_set_collect_func(map batch_func_dict) | 批量设置属性加成的采集来源以及采集函数 |
void set_calc_func(function func) | 设置最终属性值表的计算函数 |
function get_calc_func() | 返回已设置的最终属性值表的计算函数 |
mixed get_source_improvement(string source, ...) | 采集指定来源的属性加成值 |
map get_total_improvement(...) | 获得所有采集来源的属性加成值 |
map calc(map total_improvement_dict, ...) | 通过所有采集来源的属性加成值计算属性最终值 |
map calc_directly(...) | 先采集所有来源的属性加成值再计算属性最终值 |
map new_final_attribs() | 生成一个只是初始值的属性最终值 |
void reset_final_attribs(map final_attribs) | 讲属性最终值重设为初始值 |
attrib_calc2.gs
函数原型 | 函数作用 |
---|---|
void setup(map total_calculator_attrib_dict, map total_calculator_attrib_sources_dict = nil) | 安装初始化配置 |
object new_calculator(string id) | 新建一个属性计算器 |
void free_calculator(string id) | 销毁一个属性计算器 |
void free_all_calculators() | 销 毁所有属性计算器 |
object get_calculator(string id) | 根据id查找属性计算器 |
mixed get_source_improvement(string calculator_id, string source, ...) | 通过指定属性计算器获得指定来源的属性加成 |
map get_total_improvement(string calculator_id, ...) | 通过指定属性计算器获得其所有来源的属性加成表 |
map calc(string calculator_id, map total_improvement_dict, ...) | 通过指定的属性计算器获得最终属性值 |
void dump() | 调试接口,输出本模块调试信息 |
FAttribCalc.gs
需要使用pkg.attrib_calc2进行属性最终值计算的对象,必须包含本组件; 对象必须覆盖get_attrib_calculator_id方法以便提供属性计算器的id 属性计算流程:
- 收集所有来源的属性加成值
- 根据属性加成值计算最终属性值 对象通过pkg.attrib_calc2.FAttribCalc进行属性计算的两种方式:
- 不使用属性计算器的方式: 通过重载FAttribCalc的虚函数get_source_improvement/get_total_improvement/calc_final_attribs来实现属性计算
- 使用属性计算器的方式: 不重载FAttribCalc的虚函数get_source_improvement/get_total_improvement/calc_final_attribs
函数原型 | 函数作用 |
---|---|
void refresh_attribs() | 强制刷新所有属性最终值,并将属性最终值通知给对象自身 |
void refresh_attrib(string source) | 强制刷新由某个特定来源的属性加成值,并将属性最终值变更发送给对象自身 |
mixed query_attrib(string attrib) | 获取特定属性的属性最终值 |
map query_attribs() | 获取所有的属性最终值 |
map query_improvements() | 获取所有的属性加成值 |
样例
public void pkg_sample()
{
// 数据准备
map hero_attrib_dict = {
"hp" : false, // int
"mp" : false, // int
"speed" : true, // float
};
array hero_attrib_sources_dict = [
"base",
"equipment"
];
map total_calculator_attrib_dict = {
"hero" : hero_attrib_dict
};
map total_calculator_attrib_sources_dict = {
"hero" : hero_attrib_sources_dict
};
// 模块载入属性计算单元
attrib_calc2.load_all_entry(__DIR__ "impl/");
// 模块初始化
attrib_calc2.setup(total_calculator_attrib_dict, total_calculator_attrib_sources_dict);
object hero_ob = new_object(GaHero, domain.create("hero1", false));
// hp = (100+200+1000+2000)*(1 + 0.1+0.2+0.4+0.8) = 3300 * 2.5 = 8250
hero_ob=>add_base_improvement("hp", 100, false);
hero_ob=>add_base_improvement("hp", 200, false);
hero_ob=>add_base_improvement("hp", 0.1, true);
hero_ob=>add_base_improvement("hp", 0.2, true);
hero_ob=>add_equipment_improvement("hp", 1000, false);
hero_ob=>add_equipment_improvement("hp", 2000, false);
hero_ob=>add_equipment_improvement("hp", 0.4, true);
hero_ob=>add_equipment_improvement("hp", 0.8, true);
printf("-----------REFRESH BASE--------------\n");
hero_ob=>refresh_attrib("base");
printf("%s attribs: %O\n", hero_ob.desc(), hero_ob.query_attribs());
printf("%s hp=%M\n", hero_ob.desc(), hero_ob.query_attrib("hp"));
printf("-----------REFRESH EQUIPMENT--------------\n");
hero_ob=>refresh_attrib("equipment");
printf("%s attribs: %O\n", hero_ob.desc(), hero_ob.query_attribs());
printf("%s hp=%M\n", hero_ob.desc(), hero_ob.query_attrib("hp"));
printf("-----------REFRESH ALL--------------\n");
hero_ob=>refresh_attribs();
printf("%s attribs: %O\n", hero_ob.desc(), hero_ob.query_attribs());
printf("%s hp=%M\n", hero_ob.desc(), hero_ob.query_attrib("hp"));
}