跳到主要内容

game_server.game_buffs

简介

基于engine实现的buff功能模块

组件接口

FBuffBase.gs

buff的基础组件,基于class map实现

函数原型函数作用
map get_buffs()buffs数据保存的地方,使用该组件的对象需覆盖该函数
string alloc_buff_cookie()为一次apply buff分配一个cookie,在此对象范围内必须唯一(并且跟历史不重复)
void heartbeat_buffs(function filter_func = nil)定时心跳,处理buff心跳效果,清理过期buff
BuffBase query_buff(string buff_cookie)通过cookie查询对应的buff
array query_cookies(string buff_id, function filter_func = nil)通过buff类型,查询该类的所有buff的cookie
array query_buffs_by_id(string buff_id, function filter_func = nil)通过buff类型,查询该类型的所有buff
bool contains_buff(string buff_id, function filter_func = nil)判断某个类型的buff是否存在
string apply_buff(BuffBase buff)添加一个buff
void clear_buff(string buff_cookie)根据cookie清除一个buff
void clear_buffs_by_id(string buff_id, function filter_func = nil)清除一类buff
void clear_all_buffs(function filter_func = nil)清除所有buff

game_buffs.gs

buff系统

文件组织:

game_buffs: pkg.game_buffs的入口文件,提供了加载/获取buff特效功能模块、buff配置模板等接口

FBuffBase: buff功能基础组件,提供增加/移除buff等功能

FBuffEffectBase: 处理buff特效功能的基础组件

FEntityBuffBase: 基于engine和FBuffBase实现的实体对象buff功能的组件

BuffClassMap: 一些buff数据结构 - 内置了FBuffBase要求的buff数据基础class以及基于时间的buff数据class

几点说明:

  1. 使用game_buffs.load_all_entry加载buff特效功能模块

  2. 使用game_buffs.set_buff_template新增buff的模板设置

  3. BuffBase是buff数据结构的基础class,自定义的buff数据结构都应该继承BuffBase

  4. buff的模板不是必须的,buff缺少模板配置时将使用默认实现

函数原型函数作用
void set_buff_template(string buff_id, map buff_template)登记buff的一些配置信息
map get_buff_template(string buff_id)根据buff id获取预先登记的buff配置信息
object get_effect_ob(string buff_id)根据buff id获取buff的特效功能模块对象

样例

public void pkg_sample()
{
// game_buffs模块初始化
//
game_buffs.load_all_entry(__DIR__ "effect_mods/");

// 加载buff模板
map hp_inc_template = {
"effect_mod" : "common_effect",
};
game_buffs.set_buff_template("HP_INC", hp_inc_template);

map hp_dec_template = {
"effect_mod" : "common_effect",
"override" : true
};
game_buffs.set_buff_template("HP_DEC", hp_dec_template);

// 准备一个实体,并进行测试
map hero_dbase = {
"level" : 1,
};
object hero_ob = EntityFactory.create_entity(1, hero_dbase, this_domain());
printf("hero_ob: %M\n", hero_ob.desc());

string buff_cookie;
TimeBaseBuff buff;

// override
buff = TimeBaseBuff.new();
buff.id = "HP_DEC";
hero_ob.apply_buff(buff);

buff = TimeBaseBuff.new();
buff.id = "HP_DEC";
hero_ob.apply_buff(buff);

// 附加一个HP_INC的buff
buff = TimeBaseBuff.new();
buff.id = "HP_INC";
buff.set_heartbeat_interval(3 * 1000);
buff.set_duration(30 * 1000);
buff_cookie = hero_ob.apply_buff(buff);

// 模拟心跳
coroutine co = coroutine.create_with_domain(nil, domain.create(), parallel () {
int stop_time = time.time() + 10;
while (time.time() < stop_time)
{
hero_ob=>heartbeat_buffs();
coroutine.sleep(1);
}
});
co.wait();

// 移除buff
hero_ob.clear_all_buffs();
}