Project1
标题: 关于Game_Battler脚本的一个疑问 [打印本页]
作者: tan12345 时间: 2014-10-31 22:07
标题: 关于Game_Battler脚本的一个疑问
在Game_Battler脚本下有个函数:
#--------------------------------------------------------------------------
# ● 应用“状态附加”效果:普通
#--------------------------------------------------------------------------
def item_effect_add_state_normal(user, item, effect)
chance = effect.value1
chance *= state_rate(effect.data_id) if opposite?(user)
chance *= luk_effect_rate(user) if opposite?(user)
if rand < chance
add_state(effect.data_id)
@result.success = true
end
end
#--------------------------------------------------------------------------
# ● 应用“状态附加”效果:普通
#--------------------------------------------------------------------------
def item_effect_add_state_normal(user, item, effect)
chance = effect.value1
chance *= state_rate(effect.data_id) if opposite?(user)
chance *= luk_effect_rate(user) if opposite?(user)
if rand < chance
add_state(effect.data_id)
@result.success = true
end
end
整个函数内容好像被没有使用item这个参数,那么,传递这个参数给item_effect_add_state_normal有什么意义呢?
作者: 喵呜喵5 时间: 2014-10-31 22:25
本帖最后由 喵呜喵5 于 2014-10-31 22:38 编辑
Game_Battler:- #--------------------------------------------------------------------------
- # ● 应用使用效果
- #--------------------------------------------------------------------------
- def item_effect_apply(user, item, effect)
- method_table = {
- EFFECT_RECOVER_HP => :item_effect_recover_hp,
- EFFECT_RECOVER_MP => :item_effect_recover_mp,
- EFFECT_GAIN_TP => :item_effect_gain_tp,
- EFFECT_ADD_STATE => :item_effect_add_state,
- EFFECT_REMOVE_STATE => :item_effect_remove_state,
- EFFECT_ADD_BUFF => :item_effect_add_buff,
- EFFECT_ADD_DEBUFF => :item_effect_add_debuff,
- EFFECT_REMOVE_BUFF => :item_effect_remove_buff,
- EFFECT_REMOVE_DEBUFF => :item_effect_remove_debuff,
- EFFECT_SPECIAL => :item_effect_special,
- EFFECT_GROW => :item_effect_grow,
- EFFECT_LEARN_SKILL => :item_effect_learn_skill,
- EFFECT_COMMON_EVENT => :item_effect_common_event,
- }
- method_name = method_table[effect.code]
- send(method_name, user, item, effect) if method_name
- end
复制代码 在method_table中读取方法名称,之后使用send调用对应名称的方法,并传递三个参数:user, item, effect
如果这里把
def item_effect_add_state_normal(user, item, effect)
改成
def item_effect_add_state_normal(user, effect)
的话
上面那个方法就只能改成成这样:- def item_effect_apply(user, item, effect)
- method_table = {
- EFFECT_RECOVER_HP => :item_effect_recover_hp,
- EFFECT_RECOVER_MP => :item_effect_recover_mp,
- EFFECT_GAIN_TP => :item_effect_gain_tp,
- EFFECT_ADD_STATE => :item_effect_add_state,
- EFFECT_REMOVE_STATE => :item_effect_remove_state,
- EFFECT_ADD_BUFF => :item_effect_add_buff,
- EFFECT_ADD_DEBUFF => :item_effect_add_debuff,
- EFFECT_REMOVE_BUFF => :item_effect_remove_buff,
- EFFECT_REMOVE_DEBUFF => :item_effect_remove_debuff,
- EFFECT_SPECIAL => :item_effect_special,
- EFFECT_GROW => :item_effect_grow,
- EFFECT_LEARN_SKILL => :item_effect_learn_skill,
- EFFECT_COMMON_EVENT => :item_effect_common_event,
- }
- method_name = method_table[effect.code]
- if effect.code == EFFECT_ADD_STATE
- item_effect_add_state_normal(user, effect)
- else
- send(method_name, user, item, effect) if method_name
- end
- end
复制代码 顺带一提,其实我自己觉得将这个方法改成case when也没啥大问题,并且也不用传递多余的参数了……
def item_effect_apply(user, item, effect)
case effect.code
when EFFECT_RECOVER_HP then item_effect_recover_hp(user, item, effect)
when 对应的Code then 对应的方法(方法参数)
end
end
def item_effect_apply(user, item, effect)
case effect.code
when EFFECT_RECOVER_HP then item_effect_recover_hp(user, item, effect)
when 对应的Code then 对应的方法(方法参数)
end
end
但是如果之后其他人需要猴补item_effect_apply这个方法同时需要获取对应物品注释的时候应该怎么办?
作者: tan12345 时间: 2014-10-31 22:33
喵呜喵5 发表于 2014-10-31 22:25
Game_Battler:在method_table中读取方法名称,之后使用send调用对应名称的方法,并传递三个参数:user, it ...
OK,了解,之前没有查到源头,所以这段正好没看到……
欢迎光临 Project1 (https://rpg.blue/) |
Powered by Discuz! X3.1 |