# encoding: utf-8
module Taeckle
@version = "0.5" #先更新下版本号_家庭作业
class Data
attr_reader :items
attr_reader :weapons
attr_reader :armors
def initialize
@items = {}
@weapons = {}
@armors = {}
end
#--------------------------------------------------------------------------
# ● 获取物品的所持数
# item_id : 物品 ID
#--------------------------------------------------------------------------
def item_number(item_id)
# 如果 hash 个数数值不存在就返回 0
return @items.include?(item_id) ? @items[item_id] : 0
end
# ● 获取(箱子内或身上)所有物品的数量之和 (Version 0.5的更新_家庭作业)
def item_number_all
@item_number_in_all=0
for i in 0...@items.values.size
@item_number_in_all +=@items.values[i]
end
return @item_number_in_all
end
#--------------------------------------------------------------------------
# ● 获取武器所持数
# weapon_id : 武器 ID
#--------------------------------------------------------------------------
def weapon_number(weapon_id)
# 如果 hash 个数数值不存在就返回 0
return @weapons.include?(weapon_id) ? @weapons[weapon_id] : 0
end
# ● 获取(箱子内或身上)所有武器的数量之和 (Version 0.5的更新_家庭作业)
def weapon_number_all
@weapon_number_in_all=0
for i in 0...@weapons.values.size
@weapon_number_in_all +=@weapons.values[i]
end
return @weapon_number_in_all
end
#--------------------------------------------------------------------------
# ● 获取防具所持数
# armor_id : 防具 ID
#--------------------------------------------------------------------------
def armor_number(armor_id)
# 如果 hash 个数数值不存在就返回 0
return @armors.include?(armor_id) ? @armors[armor_id] : 0
end
# ● 获取(箱子内或身上)所有防具的数量之和 (Version 0.5的更新_家庭作业)
def armor_number_all
@armor_number_in_all=0
for i in 0...@armors.values.size
@armor_number_in_all +=@armors.values[i]
end
return @armor_number_in_all
end
#--------------------------------------------------------------------------
# ● 增加物品 (减少)
# item_id : 物品 ID
# n : 个数
#--------------------------------------------------------------------------
def gain_item(item_id, n)
# 更新 hash 的个数数据
if item_id > 0
@items[item_id] = [[item_number(item_id) + n, 0].max, 99].min
end
end
#--------------------------------------------------------------------------
# ● 增加武器 (减少)
# weapon_id : 武器 ID
# n : 个数
#--------------------------------------------------------------------------
def gain_weapon(weapon_id, n)
# 更新 hash 的个数数据
if weapon_id > 0
@weapons[weapon_id] = [[weapon_number(weapon_id) + n, 0].max, 99].min
end
end
#--------------------------------------------------------------------------
# ● 增加防具 (减少)
# armor_id : 防具 ID
# n : 个数
#--------------------------------------------------------------------------
def gain_armor(armor_id, n)
# 更新 hash 的个数数据
if armor_id > 0
@armors[armor_id] = [[armor_number(armor_id) + n, 0].max, 99].min
end
end
end
class Window_Help < ::Window_Help
end
class Window_Menu < ::Window_Selectable
def initialize
super(0, 64, 640, 64)
@item_max = 3
@column_max = 3
@index = 0
@commands = ["存入", "取出", "离开"]
self.contents = Bitmap.new(width - 32, height - 32)
refresh
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
self.contents.clear
for i in 0...@item_max
draw_item(i)
end
end
#--------------------------------------------------------------------------
# ● 描绘项目
# index : 项目编号
#--------------------------------------------------------------------------
def draw_item(index)
self.contents.draw_text(index * 212 - 12, 0, 212, 32, @commands[index], 1)
end
def update_help
case @index
when 0 then text = "存入物品"
when 1 then text = "取出物品"
when 2 then text = "离开场景"
end
@help_window.set_text(text, 1)
end
end
class Window_Item< ::Window_Selectable
attr_accessor :bind_data
#--------------------------------------------------------------------------
# ● 获取物品
#--------------------------------------------------------------------------
def item
return @data[self.index]
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
if self.contents != nil
self.contents.dispose
self.contents = nil
end
@data = []
# 添加物品
for i in 1...$data_items.size
if @bind_data.item_number(i) > 0
@data.push($data_items[i])
end
end
# 在战斗中以外添加武器、防具
unless $game_temp.in_battle
for i in 1...$data_weapons.size
if @bind_data.weapon_number(i) > 0
@data.push($data_weapons[i])
end
end
for i in 1...$data_armors.size
if @bind_data.armor_number(i) > 0
@data.push($data_armors[i])
end
end
end
# Sort by description
if @data.first.respond_to?(:desc)
@data.sort! { |a, b| a.desc <=> b.desc }
end
# 如果项目数不是 0 就生成位图、重新描绘全部项目
@item_max = @data.size
if @item_max > 0
self.contents = Bitmap.new(width - 32, row_max * 32)
for i in 0...@item_max
draw_item(i)
end
end
end
#--------------------------------------------------------------------------
# ● 描绘项目
# index : 项目编号
#--------------------------------------------------------------------------
def draw_item(index)
item = @data[index]
case item
when RPG::Item
number = @bind_data.item_number(item.id)
when RPG::Weapon
number = @bind_data.weapon_number(item.id)
when RPG::Armor
number = @bind_data.armor_number(item.id)
end
if self.active
self.contents.font.color = normal_color
else
self.contents.font.color = disabled_color
end
x = 4
y = index * 32
rect = Rect.new(x, y, self.width / @column_max - 32, 32)
self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
bitmap = RPG::Cache.icon(item.icon_name)
opacity = self.contents.font.color == normal_color ? 255 : 128
self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
end
#--------------------------------------------------------------------------
# ● 刷新帮助文本
#--------------------------------------------------------------------------
def update_help
@help_window.set_text(self.item == nil ? "" : self.item.description)
end
end
class Scene_Storage
attr_reader :data
def initialize
@scene_class = $scene.class
if !$game_system.storage_data.is_a?(Data)
$game_system.storage_data = Data.new
end
@data = $game_system.storage_data
end
def main
create_windows
scene_loop
dispose_windows
end
# layout
# ----------------------------------------- #
# Help Window (640 x 64) #
# ----------------------------------------- #
# Menu Window (640 x 64) #
# --------------------+-------------------- #
# Item Window Party | Item Window Storage #
# (320 x 352) | (320 x 352) #
# --------------------+-------------------- #
def create_windows
# Help Window
@help_window = Window_Help.new
# Horizontal Command Window
@menu_window = Window_Menu.new
@menu_window.active = true
# Item Window left: Party items
@item_window_party = Window_Item.new(0, 128, 320, 352)
@item_window_party.bind_data = $game_party
@item_window_party.active = false
@item_window_party.index = -1
@item_window_party.refresh
# Item Window right: Storage items
@item_window_storage = Window_Item.new(320, 128, 320, 352)
@item_window_storage.bind_data = @data
@item_window_storage.active = false
@item_window_storage.index = -1
@item_window_storage.refresh
# binding help windows
@menu_window.help_window = @help_window
@item_window_party.help_window = @help_window
@item_window_storage.help_window = @help_window
end
def update
@help_window.update
# update active window
if @menu_window.active
@menu_window.update
if Input.trigger?(Input::C)
$game_system.se_play($data_system.decision_se)
case @menu_window.index
when 0
@item_window_party.active = true
@menu_window.active = false
if @item_window_party.index == -1
@item_window_party.index = 0
end
when 1
@item_window_storage.active = true
@menu_window.active = false
if @item_window_storage.index == -1
@item_window_storage.index = 0
end
when 2
$scene = @scene_class.new
return
end
@item_window_party.refresh
@item_window_storage.refresh
end
return
end
if @item_window_party.active
@item_window_party.update
if Input.trigger?(Input::B)
$game_system.se_play($data_system.cancel_se)
@item_window_party.active = false
@menu_window.active = true
@item_window_party.index = -1
@item_window_storage.index = -1
@item_window_party.refresh
@item_window_storage.refresh
end
if Input.trigger?(Input::C)
ret = store(@item_window_party.item)
if ret
$game_system.se_play($data_system.decision_se)
@item_window_party.refresh
@item_window_storage.refresh
else
$game_system.se_play($data_system.buzzer_se)
end
end
return
end
if @item_window_storage.active
@item_window_storage.update
if Input.trigger?(Input::B)
$game_system.se_play($data_system.cancel_se)
@item_window_storage.active = false
@menu_window.active = true
@item_window_party.index = -1
@item_window_storage.index = -1
@item_window_party.refresh
@item_window_storage.refresh
end
if Input.trigger?(Input::C)
ret = fetch(@item_window_storage.item)
if ret
$game_system.se_play($data_system.decision_se)
@item_window_party.refresh
@item_window_storage.refresh
else
$game_system.se_play($data_system.buzzer_se)
end
end
return
end
end
def scene_loop
Graphics.transition
# 主循环
loop do
# 刷新游戏画面
Graphics.update
# 刷新输入信息
Input.update
# 刷新画面
update
# 如果画面切换就中断循环
if $scene != self
break
end
end
# 准备过渡
Graphics.freeze
end
def dispose_windows
self.instance_variables.each do |name|
obj = self.instance_variable_get(name)
obj.dispose if obj.is_a?(Window)
end
end
def store(item, number = 1)
# RPG::Item
num_storage_item = @data.item_number_all #version 0.5的更新_家庭作业
num_storage_weapon = @data.weapon_number_all #version 0.5的更新_家庭作业
num_storage_armor = @data.armor_number_all #version 0.5的更新_家庭作业
if item.is_a?(RPG::Item)
num_party = $game_party.item_number(item.id)
num_storage = @data.item_number(item.id)
if can_store?(num_storage_item, num_storage_weapon, num_storage_armor,number) #version 0.5的更新_家庭作业
$game_party.lose_item(item.id, number)
@data.gain_item(item.id, number)
return true
else
box_limit= 2*$game_variables[1]
print "箱子内已经有#{box_limit}个东西了,已经存满了"
end
end
# RPG::Weapon
if item.is_a?(RPG::Weapon)
num_party = $game_party.weapon_number(item.id)
num_storage = @data.weapon_number(item.id)
if can_store?(num_storage_item, num_storage_weapon, num_storage_armor,number) #version 0.5的更新_家庭作业
$game_party.lose_weapon(item.id, number)
@data.gain_weapon(item.id, number)
return true
else
box_limit= 2*$game_variables[1]
print "箱子内已经有#{box_limit}个东西了,已经存满了"
end
end
# RPG::Armor
if item.is_a?(RPG::Armor)
num_party = $game_party.armor_number(item.id)
num_storage = @data.armor_number(item.id)
if can_store?(num_storage_item, num_storage_weapon, num_storage_armor,number) #version 0.5的更新_家庭作业
$game_party.lose_armor(item.id, number)
@data.gain_armor(item.id, number)
return true
else
box_limit= 2*$game_variables[1]
print "箱子内已经有#{box_limit}个东西了,已经存满了"
end
end
return false
end
def fetch(item, number = 1)
if item.is_a?(RPG::Item)
num_party = $game_party.item_number(item.id)
num_storage = @data.item_number(item.id)
$game_party.gain_item(item.id, number)
@data.gain_item(item.id, -number)
return true
end
# RPG::Weapon
if item.is_a?(RPG::Weapon)
num_party = $game_party.weapon_number(item.id)
num_storage = @data.weapon_number(item.id)
$game_party.gain_weapon(item.id, number)
@data.gain_weapon(item.id, -number)
return true
end
# RPG::Armor
if item.is_a?(RPG::Armor)
num_party = $game_party.armor_number(item.id)
num_storage = @data.armor_number(item.id)
$game_party.gain_armor(item.id, number)
@data.gain_armor(item.id, -number)
return true
end
return false
end
def can_store?(num_storage_item, num_storage_weapon, num_storage_armor,number=1) #version 0.5的更新_家庭作业
num_storage_item+num_storage_weapon+num_storage_armor+number <= 2*$game_variables[1] #箱子大小上限设置
end
end