加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 jeffice0325 于 2020-8-8 13:54 编辑
用重载实现的背包容量脚本,但当物品数量众多且进行以下操作:(依次加入1个不同物品,查询剩余容量)时效率很低,请问各位大佬如何优化该代码?
class Game_Party < Game_Unit def all_item_number ans = 0 @items.each{|idx|ans += idx[1]} @armors.each{|idx|ans += idx[1]} @weapons.each{|idx|ans += idx[1]} ans end def max_item_number(item) return 0 unless item_container(item.class) return max_all_item_number - all_item_number unless item_container(item.class)[item.id] return max_all_item_number - all_item_number + item_container(item.class)[item.id] end def max_all_item_number 99 end def item_max?(item) all_item_number >= max_all_item_number end def gain_item(item, amount, include_equip = false) container = item_container(item.class) return unless container container[item.id] = 0 unless container[item.id] container[item.id] += [amount,max_item_number(item) - container[item.id]].min container.delete(item.id) if container[item.id] <= 0 if include_equip && container[item] + amount < 0 discard_members_equip(item, -container[item] - amount) end $game_map.need_refresh = true end end class Window_ItemList < Window_Selectable def draw_item_number(rect, item) draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2) end end
class Game_Party < Game_Unit
def all_item_number
ans = 0
@items.each{|idx|ans += idx[1]}
@armors.each{|idx|ans += idx[1]}
@weapons.each{|idx|ans += idx[1]}
ans
end
def max_item_number(item)
return 0 unless item_container(item.class)
return max_all_item_number - all_item_number unless item_container(item.class)[item.id]
return max_all_item_number - all_item_number + item_container(item.class)[item.id]
end
def max_all_item_number
99
end
def item_max?(item)
all_item_number >= max_all_item_number
end
def gain_item(item, amount, include_equip = false)
container = item_container(item.class)
return unless container
container[item.id] = 0 unless container[item.id]
container[item.id] += [amount,max_item_number(item) - container[item.id]].min
container.delete(item.id) if container[item.id] <= 0
if include_equip && container[item] + amount < 0
discard_members_equip(item, -container[item] - amount)
end
$game_map.need_refresh = true
end
end
class Window_ItemList < Window_Selectable
def draw_item_number(rect, item)
draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)
end
end
|