#===============================================================================
# ** Scene_Blacksmith
#===============================================================================
class Scene_Blacksmith
def initialize(type = 0, weapons = [], armors = [], items = [], level = nil)
# Set available goods for this shop based off passed argument.
@goods = []
@goods += weapons.collect {|id| $data_weapons[id] }
@goods += armors.collect {|id| $data_armors[id] }
@goods += items.collect {|id| $data_items[id] }
@goods.uniq!
# Configure the level variable
@level = (level == nil) ? Array.new(4, true) : (level + [true])
@enchants = Blacksmith::USE_ENCHANTMENTS
@type = type
end
#-----------------------------------------------------------------------------
def main
# Create a Proc to handle confirmation of choices
@confirm_proc = Proc.new {
@help_window.set_text('Are you sure?')
window = Window_Command.new(160, ['Confirm', 'Cancel'])
window.x, window.y, window.z = 224, 192, 9999
loop { Graphics.update; Input.update; window.update
if Input.trigger?(Input::C) || Input.trigger?(Input::B)
result = (Input.trigger?(Input::C) && window.index == 0)
$game_system.se_play($data_system.cancel_se) unless result
window.dispose
break(result)
end
}
}
# Initialize the needed windows.
@command_window = Window_BlacksmithCommand.new(@level, @type)
@forge_window = Window_BlacksmithForge.new(@goods)
@extract_window = Window_BlacksmithExtract.new
@materials_window = Window_BlacksmithMaterials.new
@enchant_window = Window_BlacksmithEnchant.new
@status_window = Window_BlacksmithStatus.new
@dummy_window = Window_Base.new(0, 128, 640, 352)
@gold_window = Window_Gold.new
@gold_window.x, @gold_window.y = 480, 64
@help_window = Window_Help.new
# Bind the help window to the other windows.
@forge_window.help_window = @extract_window.help_window = @help_window
@enchant_window.help_window = @help_window
# Set a windows array for easier handling of all windows.
@windows = [@command_window, @forge_window, @extract_window, @help_window,
@materials_window, @status_window, @dummy_window, @gold_window, @enchant_window]
# Create map sprite if configured to do so, and set window opacity
if Blacksmith::MAP_BACK
@spriteset = Spriteset_Map.new
@windows.each {|window| window.opacity = 160 }
end
# Execute the transition and start the main loop.
Graphics.transition
loop {Graphics.update; Input.update; update; break if $scene != self }
# Freeze the Graphics and dispose the windows
Graphics.freeze
@windows.each {|window| window.dispose }
if Blacksmith::MAP_BACK && @spriteset != nil
@spriteset.dispose
end
end
#-----------------------------------------------------------------------------
def update
# Update the windows
@windows.each {|window| window.update }
# Branch method depending on current action.
if @command_window.active
update_command
elsif @extract_window.active
update_extract
elsif @forge_window.active
update_forge
elsif @materials_window.active
update_materials
elsif @enchant_window.active
update_enchant
end
end
#-----------------------------------------------------------------------------
def back_to_map
# Play SE and return to the map.
$game_system.se_play($data_system.cancel_se)
$scene = Scene_Map.new
end
#-----------------------------------------------------------------------------
def update_command
# Set help text depending on the selected index.
help_text = case @command_window.at_index
when "Forge" then 'Use materials to forge new weapons, armors, and items.'
when "Extract" then 'Extract materials from weapons, armors, and items.'
when "Enchant" then 'Enchant weapons, armors, and items using other items.'
when "Exit" then 'Exit the shop.'
end
@help_window.set_text(help_text)
# Check for Input.
if Input.trigger?(Input::B)
back_to_map
elsif Input.trigger?(Input::C)
$game_system.se_play($data_system.decision_se)
# Branch depending on command index
case @command_window.at_index
when "Forge"
# Play SE and return if option is locked.
unless @level[0]
$game_system.se_play($data_system.buzzer_se)
return
end
# Shift scene to forge phase.
@dummy_window.visible = false
@forge_window.refresh(false)
@command_window.active = false
@forge_window.active = @forge_window.visible = true
@status_window.visible = true
when "Extract"
# Play SE and return if option is locked.
unless @level[1]
$game_system.se_play($data_system.buzzer_se)
return
end
# Shift scene to extract phase
@extract_window.refresh
@command_window.active = @dummy_window.visible = false
@extract_window.active = @extract_window.visible = true
@status_window.visible = true
when "Enchant"
# Play SE and return if option is locked.
if @enchants
unless @level[2]
$game_system.se_play($data_system.buzzer_se)
return
end
# Shift scene to enchant phase.
@forge_window.refresh(true)
@command_window.active = @dummy_window.visible = false
@forge_window.active = @forge_window.visible = true
@status_window.visible = true
else
back_to_map
end
when "Exit"
back_to_map
end
end
end
#-----------------------------------------------------------------------------
def update_forge
# Update for input when forge window is active.
@item = @status_window.item = @forge_window.item
if Input.trigger?(Input::B)
$game_system.se_play($data_system.cancel_se)
@command_window.active = @dummy_window.visible = true
@forge_window.active = @forge_window.visible = false
@status_window.visible = false
elsif Input.trigger?(Input::C)
$game_system.se_play($data_system.decision_se)
@forge_window.active = @forge_window.visible = false
if @command_window.at_index == "Forge"
@materials_window.refresh(@item, 0)
@materials_window.visible = @materials_window.active = true
else
@enchant_window.refresh
@enchant_window.visible = @enchant_window.active = true
end
end
end
#-----------------------------------------------------------------------------
def update_extract
# Update for input when extraction window is active.
@item = @status_window.item = @extract_window.item
if Input.trigger?(Input::B)
$game_system.se_play($data_system.cancel_se)
@command_window.active = @dummy_window.visible = true
@extract_window.active = @extract_window.visible = false
@status_window.visible = false
elsif Input.trigger?(Input::C)
$game_system.se_play($data_system.decision_se)
@materials_window.refresh(@item, 1)
@extract_window.active = @extract_window.visible = false
@materials_window.visible = @materials_window.active = true
end
end
#-----------------------------------------------------------------------------
def update_enchant
# Update input on the enchantment items screen.
if Input.trigger?(Input::B)
# Return to previous screen if cancel button is pressed.
$game_system.se_play($data_system.cancel_se)
@enchant_window.visible = @enchant_window.active = false
@forge_window.active = @forge_window.visible = true
elsif Input.trigger?(Input::C) && @confirm_proc.call
enchant_item
end
end
#-----------------------------------------------------------------------------
def enchant_item
# Apply enchantment to weapon/armor using item
$game_party.lose_item(@enchant_window.item.id, 1)
Blacksmith.create_item(@forge_window.item, @enchant_window.item)
# Play SE
$game_system.se_play(RPG::AudioFile.new(*Blacksmith::ENCHANT_SE))
# Refesh windows
[@enchant_window, @status_window].each {|window| window.refresh }
@forge_window.refresh(true)
# Return to previous screen
@enchant_window.visible = @enchant_window.active = false
@forge_window.active = @forge_window.visible = true
end
#-----------------------------------------------------------------------------
def update_materials
# Show help text.
text = 'Press the Action Button to proceed. Press Cancel to go back'
@help_window.set_text(text)
if Input.trigger?(Input::B)
# Return to previous screen if cancel button is pressed.
$game_system.se_play($data_system.cancel_se)
@materials_window.visible = @materials_window.active = false
if @command_window.at_index == "Forge"
@forge_window.active = @forge_window.visible = true
else
@extract_window.active = @extract_window.visible = true
end
elsif Input.trigger?(Input::C) && @confirm_proc.call
@command_window.at_index == "Forge" ? forge_item : extract_item
end
end
#-----------------------------------------------------------------------------
def forge_item
# Set local variables depending on item type.
case @item
when RPG::Weapon
quantity, type = $game_party.weapon_number(@item.id), 0
materials = Blacksmith.weapon_forges(@item.id)
price = Blacksmith.weapon_gold(@item.id)[0]
when RPG::Armor
quantity, type = $game_party.armor_number(@item.id), 1
materials = Blacksmith.armor_forges(@item.id)
price = Blacksmith.armor_gold(@item.id)[0]
when RPG::Item
quantity, type = $game_party.item_number(@item.id), 2
materials = Blacksmith.item_forges(@item.id)
price = Blacksmith.item_gold(@item.id)[0]
end
# If player doesn't have the materials or gold, play SE and end method.
unless Blacksmith.materials?(type, @item.id)
$game_system.se_play($data_system.buzzer_se)
return
end
# End method and play buzzer if inventory is full.
return $game_system.se_play($data_system.buzzer_se) if quantity == 99
# Play the defined SE used when forging.
$game_system.se_play(RPG::AudioFile.new(*Blacksmith::FORGE_SE))
# Remove required materials from inventory and subtract gold cost.
if materials != nil
materials.each {|material|
case material[0]
when 0 then $game_party.lose_weapon(material[1], material[2])
when 1 then $game_party.lose_armor(material[1], material[2])
when 2 then $game_party.lose_item(material[1], material[2])
end
}
end
$game_party.lose_gold(price)
# Add forged item
case @item
when RPG::Weapon then $game_party.gain_weapon(@item.id, 1)
when RPG::Armor then $game_party.gain_armor(@item.id, 1)
when RPG::Item then $game_party.gain_item(@item.id, 1)
end
# Reset windows.
@materials_window.visible = @materials_window.active = false
@forge_window.active = @forge_window.visible = true
# Refresh any windows that may have changed
[@status_window, @gold_window, @extract_window, @forge_window,
@enchant_window].each {|window| window.refresh }
end
#-----------------------------------------------------------------------------
def extract_item
# Set local variables depending on item type.
case @item
when RPG::Weapon
quantity = $game_party.weapon_number(@item.id)
materials = Blacksmith.weapon_extractions(@item.id)
price = Blacksmith.weapon_gold(@item.id)[1]
when RPG::Armor
quantity = $game_party.armor_number(@item.id)
materials = Blacksmith.armor_extractions(@item.id)
price = Blacksmith.armor_gold(@item.id)[1]
when RPG::Item
quantity = $game_party.item_number(@item.id)
materials = Blacksmith.item_extractions(@item.id)
price = Blacksmith.item_gold(@item.id)[1]
end
# If nothing is defined for the extraction, return.
if materials == nil || (materials.empty? && price == 0)
return $game_system.se_play($data_system.buzzer_se)
end
# Play extraction SE
$game_system.se_play(RPG::AudioFile.new(*Blacksmith::EXTRACT_SE))
# Perform extraction, adding materials and increasing gold.
materials.each {|material|
case material[0]
when 0 then $game_party.gain_weapon(material[1], material[2])
when 1 then $game_party.gain_armor(material[1], material[2])
when 2 then $game_party.gain_item(material[1], material[2])
end
}
$game_party.gain_gold(price)
# Remove extracted item from inventory
case @item
when RPG::Weapon then $game_party.lose_weapon(@item.id, 1)
when RPG::Armor then $game_party.lose_armor(@item.id, 1)
when RPG::Item then $game_party.lose_item(@item.id, 1)
end
# Reset windows.
@materials_window.visible = @materials_window.active = false
@extract_window.active = @extract_window.visible = true
# Refresh any windows that may have changed
[@status_window, @gold_window, @extract_window].each {|window| window.refresh }
end
end