本帖最后由 英顺的马甲 于 2015-1-19 10:51 编辑
很多时候与其重定义method不如重定义变量
额...好吧,就放个例子
https://rpg.blue/forum.php?mod=r ... 990&pid=2575524
与其
class Game_Party def item_number(id) @items[@actors[0].id] ? (@items[@actors[0].id].include?(id) ? @items[@actors[0].id][id] : 0) : 0 end def weapon_number(id) @weapons[@actors[0].id] ? (@weapons[@actors[0].id].include?(id) ? @weapons[@actors[0].id][id] : 0) : 0 end def armor_number(id) @armors[@actors[0].id] ? (@armors[@actors[0].id].include?(id) ? @armors[@actors[0].id][id] : 0) : 0 end def gain_item(id, n) @items[@actors[0].id] = {} unless @items[@actors[0].id] if item_id > 0 @items[@actors[0].id][id] = [[item_number(id) + n, 0].max, 99].min end end def gain_weapon(id, n) @weapons[@actors[0].id] = {} unless @weapons[@actors[0].id] if id > 0 @weapons[@actors[0].id][id] = [[weapon_number(id) + n, 0].max, 99].min end end def gain_armor(id, n) @armors[@actors[0].id] = {} unless @armors[@actors[0].id] if id > 0 @armors[@actors[0].id][id] = [[armor_number(id) + n, 0].max, 99].min end end end
class Game_Party
def item_number(id)
@items[@actors[0].id] ? (@items[@actors[0].id].include?(id) ? @items[@actors[0].id][id] : 0) : 0
end
def weapon_number(id)
@weapons[@actors[0].id] ? (@weapons[@actors[0].id].include?(id) ? @weapons[@actors[0].id][id] : 0) : 0
end
def armor_number(id)
@armors[@actors[0].id] ? (@armors[@actors[0].id].include?(id) ? @armors[@actors[0].id][id] : 0) : 0
end
def gain_item(id, n)
@items[@actors[0].id] = {} unless @items[@actors[0].id]
if item_id > 0
@items[@actors[0].id][id] = [[item_number(id) + n, 0].max, 99].min
end
end
def gain_weapon(id, n)
@weapons[@actors[0].id] = {} unless @weapons[@actors[0].id]
if id > 0
@weapons[@actors[0].id][id] = [[weapon_number(id) + n, 0].max, 99].min
end
end
def gain_armor(id, n)
@armors[@actors[0].id] = {} unless @armors[@actors[0].id]
if id > 0
@armors[@actors[0].id][id] = [[armor_number(id) + n, 0].max, 99].min
end
end
end
不如class Bag def initialize @data = {} end def method_missing(name, *args, &block) @data[$game_party.actors[0].id] ||= {} begin @data[$game_party.actors[0].id].send(name, *args, &block) rescue Exception raise($!.class, $!.message, caller) end end end class Game_Party alias multi_bag_init initialize unless defined?(multi_bag_init) def initialize multi_bag_init @items = Bag.new @weapons = Bag.new @armor = Bag.new end end
class Bag
def initialize
@data = {}
end
def method_missing(name, *args, &block)
@data[$game_party.actors[0].id] ||= {}
begin
@data[$game_party.actors[0].id].send(name, *args, &block)
rescue Exception
raise($!.class, $!.message, caller)
end
end
end
class Game_Party
alias multi_bag_init initialize unless defined?(multi_bag_init)
def initialize
multi_bag_init
@items = Bag.new
@weapons = Bag.new
@armor = Bag.new
end
end
|