Submit Your Article Guild Wars 2 Forum RPG Maker VX.com
 
RPG Maker
 

 Username:
 Password:
   Not a member? Register!



Home > RGSS Script Reference > Scene_Shop

Scene_Shop


Inherits from: None

Description: This class handles the shop screen and its constituant windows, responding to user input and updating their contents.

Code


class Scene_Shop
# ------------------------------------
  def main
    @help_window = Window_Help.new
    @command_window = Window_ShopCommand.new
    @gold_window = Window_Gold.new
    @gold_window.x = 480
    @gold_window.y = 64
    @dummy_window = Window_Base.new(0, 128, 640, 352)
    @buy_window = Window_ShopBuy.new($game_temp.shop_goods)
    @buy_window.active = false
    @buy_window.visible = false
    @buy_window.help_window = @help_window
    @sell_window = Window_ShopSell.new
    @sell_window.active = false
    @sell_window.visible = false
    @sell_window.help_window = @help_window
    @number_window = Window_ShopNumber.new
    @number_window.active = false
    @number_window.visible = false
    @status_window = Window_ShopStatus.new
    @status_window.visible = false
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @help_window.dispose
    @command_window.dispose
    @gold_window.dispose
    @dummy_window.dispose
    @buy_window.dispose
    @sell_window.dispose
    @number_window.dispose
    @status_window.dispose
  end
# ------------------------------------
  def update
    @help_window.update
    @command_window.update
    @gold_window.update
    @dummy_window.update
    @buy_window.update
    @sell_window.update
    @number_window.update
    @status_window.update
    if @command_window.active
      update_command
      return
    end
    if @buy_window.active
      update_buy
      return
    end
    if @sell_window.active
      update_sell
      return
    end
    if @number_window.active
      update_number
      return
    end
  end
# ------------------------------------
  def update_command
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
    if Input.trigger?(Input::C)
      case @command_window.index
      when 0
        $game_system.se_play($data_system.decision_se)
        @command_window.active = false
        @dummy_window.visible = false
        @buy_window.active = true
        @buy_window.visible = true
        @buy_window.refresh
        @status_window.visible = true
      when 1
        $game_system.se_play($data_system.decision_se)
        @command_window.active = false
        @dummy_window.visible = false
        @sell_window.active = true
        @sell_window.visible = true
        @sell_window.refresh
      when 2
        $game_system.se_play($data_system.decision_se)
        $scene = Scene_Map.new
      end
      return
    end
  end
# ------------------------------------ 
  def update_buy
    @status_window.item = @buy_window.item
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @command_window.active = true
      @dummy_window.visible = true
      @buy_window.active = false
      @buy_window.visible = false
      @status_window.visible = false
      @status_window.item = nil
      @help_window.set_text("")
      return
    end
    if Input.trigger?(Input::C)
      @item = @buy_window.item
      if @item == nil or @item.price > $game_party.gold
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      case @item
      when RPG::Item
        number = $game_party.item_number(@item.id)
      when RPG::Weapon
        number = $game_party.weapon_number(@item.id)
      when RPG::Armor
        number = $game_party.armor_number(@item.id)
      end
      if number == 99
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      $game_system.se_play($data_system.decision_se)
      max = @item.price == 0 ? 99 : $game_party.gold / @item.price
      max = [max, 99 - number].min
      @buy_window.active = false
      @buy_window.visible = false
      @number_window.set(@item, max, @item.price)
      @number_window.active = true
      @number_window.visible = true
    end
  end
# ------------------------------------
  def update_sell
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @command_window.active = true
      @dummy_window.visible = true
      @sell_window.active = false
      @sell_window.visible = false
      @status_window.item = nil
      @help_window.set_text("")
      return
    end
    if Input.trigger?(Input::C)
      @item = @sell_window.item
      @status_window.item = @item
      if @item == nil or @item.price == 0
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      $game_system.se_play($data_system.decision_se)
      case @item
      when RPG::Item
        number = $game_party.item_number(@item.id)
      when RPG::Weapon
        number = $game_party.weapon_number(@item.id)
      when RPG::Armor
        number = $game_party.armor_number(@item.id)
      end
      max = number
      @sell_window.active = false
      @sell_window.visible = false
      @number_window.set(@item, max, @item.price / 2)
      @number_window.active = true
      @number_window.visible = true
      @status_window.visible = true
    end
  end
# ------------------------------------ 
  def update_number
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @number_window.active = false
      @number_window.visible = false
      case @command_window.index
      when 0
        @buy_window.active = true
        @buy_window.visible = true
      when 1
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
      return
    end
    if Input.trigger?(Input::C)
      $game_system.se_play($data_system.shop_se)
      @number_window.active = false
      @number_window.visible = false
      case @command_window.index
      when 0
        $game_party.lose_gold(@number_window.number * @item.price)
        case @item
        when RPG::Item
          $game_party.gain_item(@item.id, @number_window.number)
        when RPG::Weapon
          $game_party.gain_weapon(@item.id, @number_window.number)
        when RPG::Armor
          $game_party.gain_armor(@item.id, @number_window.number)
        end
        @gold_window.refresh
        @buy_window.refresh
        @status_window.refresh
        @buy_window.active = true
        @buy_window.visible = true
      when 1
        $game_party.gain_gold(@number_window.number * (@item.price / 2))
        case @item
        when RPG::Item
          $game_party.lose_item(@item.id, @number_window.number)
        when RPG::Weapon
          $game_party.lose_weapon(@item.id, @number_window.number)
        when RPG::Armor
          $game_party.lose_armor(@item.id, @number_window.number)
        end
        @gold_window.refresh
        @sell_window.refresh
        @status_window.refresh
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
      return
    end
  end
end

Properties


Help_Window: A Window_Help object respresenting the window that shows item descriptions at the top of the shop screen.

Command_Window: A Window_ShopCommand object. This window shows the commands "Buy", "Sell", and "Exit".

Gold_Window: A Window_Gold object showing the amount of money remaining.

Dummy_Window: A Window_Base object. This window is just there to fill space when the user is selecting a command from the main menu.

Buy_Window: A Window_ShopBuy object in which the items available to buy are listed.

Sell_Window: A Window_ShopSell object in which the items in the party's possession that can be sold are listed.

Number_Window: A Window_ShopNumber object where the player selects the number of items to buy or sell, with the associated cost or sell value shown.

Status_Window: A Window_ShopStatus object. This shows the how many of the item being bought the party possesses, and when the party is buying something equippable, the stat change relative to the current equipment is changed.

Item: The current item being bought or sold.

Methods


Main

Arguments: None
Local Variables: None

How it Works: This is the main method for this class. It initializes the windows, then continuously updates the windows, then does garbage collection. First, the method initializes all the windows to their initial states. A note here about @dummy_window. This window doesn't conform to any defined window class, so the Window_Base class is used instead. The four parameters are (from left to right): x coordinate, y coordinate, wdith, height. The next part of the method is the main loop seen in all "Scene" classes. This loop updates the graphics, captures any new user input, and then updates the window contents. This continues until the value of $Scene changes, that is, the user exits the shop window. Once the user exits the shop window, the last part of the method disposes of all the window objects.

Update

Arguments: None
Local Variables: None

How it Works: This method updates the contents of all the windows, and decides which window's user input should be responded to, based on the state of the shop window. First, all eight windows' contents are updated. If the command window is active (the user is selecting one of the three main commands), then the Update_Command method is called to respond to user input from that window. If the buy window is active (the user is selecting an item to buy), then the Update_Buy method is called to respond to user input from the buy window. If the sell window is active (the user is selecting an item to sell), then the Update_Sell method is called to respond to user input from the sell window. Finally, if the number window is active (the user is selecting the quantity of items to buy or sell), then the Update_Number method is called to respond to user input from the number window.

Update_Command

Arguments: None
Local Variables: None

How it Works: This method responds to user input from the command window where the user selects from the three main shop menu options. If the user's input is "B" (cancel key), then the cancel sound effect is played and control is transferred back to the map. If the user's input is "C" (decision key), then the index of the command window is checked. If the index is 0 (buy), then the command window is made inactive and the dummy window is made invisible. Then, the buy window is made both visible and the active window. Then, the buy window is refreshed to show the items for sale, and the status window is made visible so the player can see the number of items possessed and the stat changes. If the index is 1 (Sell), then the command window is made inactive and the dummy window invisible, as before, but this time, the sell window is made visible and active. The sell window is then refreshed so that the party's inventory is shown. If the index is 2 (Exit), then the decision sound effect is played and control is transferred back to the map.

Update_Buy

Arguments: None
Local Variables:
Number: When an item is selected, this value is set to the number of that item currently possessed by the party.
Max: This value is set to the maximum number of items that can be bought given the party's current funds and given the 99-item limit.

How it Works: This method responds to user input on the window where the player selects an item to buy. The @status_window.item = @buy_window.item statement sets the item that will be profiled in the status window to the item currently selected in the buy window. Then, the user's input is checked. If the user's input is "B" (cancel key), then the cancel sound effect is played, and control is transferred back to the shop command window. This entails making the command window active, making the dummy window visible, making the buy window inactive and invisible, and setting the help window text to a null string. If the user's input is "C" (decision key), then the value of @item is set to the item currently selected in the shop window for further processing. The first thing that is checked is to make sure the item isn't nil (which might happen due to an error), and that the party has enough money to buy the item. If one of these isn't the case, then the buzzer sound effect is played, and no further processing is done on the item. If this check is passed, then the script next checks to see if the party already has 99 of the selected item. If the party does, then the buzzer sound is played and no further processing is done. If the party can buy the item, the decision sound effect is played. Then, the method computes the maximum number of the selected item that can be bought by the party. There are two factors that influence this value. First is the party's money. The value of max is set to either 99 (if the item's price is 0) or the party's money divided by the price of the item, rounded down. The second factor that affects this maximum number is the 99-item limit. The final value of max is the lesser of the the number of copies of the item the party can afford, and 99 minus the number of this item currently possessed. Control is then transferred to the number window. The buy window is made inactive and invisible. The number window is initialized with the item in question, the maximum number of items that can be bought, and the item's price. The number window is then made active and visible.

Update_Sell

Arguments: None
Local Variables:
Number: When an item is selected, this value is set to the number of that item currently possessed by the party.
Max: This value is set to the maximum number of items that can be sold, equal to the number possessed by the party.

How it Works: This method responds to user input on the window where the player selects an item to sell. If the user's input is "B" (cancel key), then the cancel sound effect is played, and control is transferred back to the shop command window. This is done by making the command window active, the dummy window visible, the buy window inactive and invisible, and setting the help window text to a null string. If the user's input is "C" (decision key), then the value of @item is set to the item selected to be sold and then processed. The first thing that is checked is to make sure the item isn't nil and that the item price isn't 0 (a sentry value that marks the item as unable to be sold). If one of these conditions isn't met, then the buzzer sound effect is played, and the method returns. If the check succeeds, then the value of number is set to the number of that item that the party possessed. Then, the value of max is set to this number. Control is then passed to the number window. The sell window is first made inactive and invisible. The number window is initialized with the item being sold, the maximum number that can be sold, and the item's price, divided by 2 (since that's how much you get for selling). The number window and status window are then made visible, and the number window is made active.

Update_Number

Arguments: None
Local Variables: None

How it Works: This method responds to user input from the number window, where the player chooses how many copies of an item to buy or sell. If the user's input is "B" (cancel key), then the cancel sound effect is played. The number window is then made invisible and inactive. What happens next depends on the index of the main command window. If the index is 0 (Buy), then control is transferred to the buy window. If the index is 1 (Sell), then control is transferred to the sell window. In either case, the method then returns. If the user's input is "C" (decision key), then the shop sound effect is played. Again, what happens next depends on the index of the main command window. If the index is 0 (Buy), then the party loses gold equal to the number of items selected multiplied by the item's price, then that number of copies of the item is added to the party's stock with the appropriate method from Game_Party. The gold window, buy window, and status window are all refreshed to reflect the changes resulting from buying the items. Control is then transferred back to the buy window. If the index is 1 (Sell), then the party gains gold equal to the number of items selected multiplied by (the item's price divided by 2). The items are then removed from the party's inventory using the appropriate method from Game_Party. The gold window, sell window, and status window are all refreshed to reflect the changes from selling the items. Control is then transferred back to the sell window. 
Syntax
@
@@
$
alias
[array index]
attr_accessor
attr_reader
attr_writer
class
def
do
ensure
for
if
[iterator]
key => value
new
next
nil
redo
require
return
rescue
self
super
undef
unless
until
while
yield

Classes
Arrow_Actor
Arrow_Base
Arrow_Enemy
Game_Actor
Game_Actors
Game_BattleAct
Game_Battler
Game_Character
Game_Common
Game_Enemy
Game_Event
Game_Map
Game_Party
Game_Picture
Game_Player
Game_Screen
Game_SlfSwitch
Game_Switches
Game_System
Game_Troop
Game_Variables
Interpreter
Scene_Debug
Scene_End
Scene_Equip
Scene_File
Scene_Gameover
Scene_Item
Scene_Load
Scene_Map
Scene_Menu
Scene_Name
Scene_Save
Scene_Shop
Scene_Skill
Scene_Status
Scene_Title
Sprite_Battler
Sprite_Character
Sprite_Picture
Sprite_Timer
Spriteset_Battle
Spriteset_Map
Window_Base
Window_Battleresult
Window_Battlestatus
Window_Command
Window_DebugLeft
Window_DebugRight
Window_EquipItem
Window_EquipLeft
Window_EquipRight
Window_Gold
Window_Help
Window_InputNumb
Window_Item
Window_MenuStatus
Window_Message
Window_NameEdit
Window_NameInput
Window_PartyCom
Window_PlayTime
Window_SaveFile
Window_Selectable
Window_ShopBuy
Window_ShopCom
Window_ShopNum
Window_ShopSell
Window_ShopStatus
Window_Skill
Window_SkillStatus
Window_Status
Window_Steps
Window_Target

Other
Class Hierarchy
Global Variables


RPG RPG Revolution
RPG RPG Revolution is your #1 stop for game development and console RPG games, as well as those created by people like you. Link to us to support us, so we may grow to be better website community for you.

RPG RPG Revolution is an Privacy Policy and Legal