赞 | 287 |
VIP | 11 |
好人卡 | 74 |
积分 | 226 |
经验 | 281171 |
最后登录 | 2024-11-14 |
在线时间 | 9413 小时 |
Lv5.捕梦者 (暗夜天使) 只有笨蛋才会看到
- 梦石
- 1
- 星屑
- 21621
- 在线时间
- 9413 小时
- 注册时间
- 2012-6-19
- 帖子
- 7118
|
本帖最后由 喵呜喵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这个方法同时需要获取对应物品注释的时候应该怎么办? |
评分
-
查看全部评分
|