gerkrt

RPGXP - ENG - Improved Shop

Sep 14th, 2011
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.01 KB | None | 0 0
  1. =begin
  2. #==============================================================================
  3. # Improved Shop
  4. # By gerkrt/gerrtunk
  5. # Version: 1
  6. # License: GPL, credits
  7. #==============================================================================
  8.  
  9. This script add a lost feature from old rpgmakers: the option to select if you
  10. wanted to buy, sell or both.
  11.  
  12. Also adds the option of modifing the price based in %. Buy and sell discount are
  13. separated, so you can make any combination.
  14.  
  15. --------INSTRUCTIONS---------
  16.  
  17. Before calling a shop, call this script:
  18.  
  19. $game_temp.type = 1
  20. $game_temp.buy_discount = 1.2
  21. $game_temp.sell_discount = 0.8
  22.  
  23. These is a call example.
  24. $game_temp.type : When 2 player can only sell.  When 1 player can only buy. When 0
  25. can do both.
  26. $game_temp.buy_discount : When you buy items the price is multiplied with this number.
  27. $game_temp.sell_discount : When you sell items the price is multiplied with this number.
  28.  
  29. Note that 0.2 , the . is like a ,.
  30.  
  31. After the sop call, these values are restored to default ones. This means that
  32. you can call a shop wiyhout adding the script call and that it will use:
  33.  
  34. Type = Buy & Sell
  35. Buy & Sell discounts = 1
  36.  
  37. =end
  38.  
  39.  
  40. class Game_Temp
  41.   attr_accessor :type                        # 2: Sell/1: Buy/0: Both
  42.   attr_accessor :buy_discount                # Buy discount +- %
  43.   attr_accessor :sell_discount               # Sell discount +- %
  44.   alias gt_init initialize
  45.   def initialize
  46.       gt_init
  47.       @type= 0
  48.       @buy_discount = 1
  49.       @sell_discount= 1
  50.   end
  51. end
  52.  
  53. class Scene_Shop
  54.   #--------------------------------------------------------------------------
  55.   # * Frame Update (when command window is active)
  56.   #--------------------------------------------------------------------------
  57.   def update_command
  58.     # If B button was pressed
  59.     if Input.trigger?(Input::B)
  60.       # Play cancel SE
  61.       $game_system.se_play($data_system.cancel_se)
  62.         # Reset shop variables
  63.         $game_temp.type = 0
  64.         $game_temp.buy_discount = 1
  65.         $game_temp.sell_discount = 1
  66.       # Switch to map screen
  67.       $scene = Scene_Map.new
  68.       return
  69.     end
  70.     # If C button was pressed
  71.     if Input.trigger?(Input::C)
  72.       # Branch by command window cursor position
  73.       case @command_window.index
  74.       when 0  # buy
  75.         if $game_temp.type== 0 or $game_temp.type== 1
  76.           # Play decision SE
  77.           $game_system.se_play($data_system.decision_se)
  78.           # Change windows to buy mode
  79.           @command_window.active = false
  80.           @dummy_window.visible = false
  81.           @buy_window.active = true
  82.           @buy_window.visible = true
  83.           @buy_window.refresh
  84.           @status_window.visible = true
  85.         else
  86.           $game_system.se_play($data_system.cancel_se)
  87.           return
  88.         end  
  89.       when 1  # sell
  90.        if $game_temp.type== 0 or $game_temp.type== 2
  91.         # Play decision SE
  92.         $game_system.se_play($data_system.decision_se)
  93.         # Change windows to sell mode
  94.         @command_window.active = false
  95.         @dummy_window.visible = false
  96.         @sell_window.active = true
  97.         @sell_window.visible = true
  98.         @sell_window.refresh
  99.       else
  100.         $game_system.se_play($data_system.cancel_se)
  101.         return
  102.       end
  103.       when 2  # quit
  104.         # Play decision SE
  105.         $game_system.se_play($data_system.decision_se)
  106.         # Reset shop variables
  107.         $game_temp.type = 0
  108.         $game_temp.buy_discount = 1
  109.         $game_temp.sell_discount = 1
  110.         # Switch to map screen
  111.         $scene = Scene_Map.new
  112.       end
  113.       return
  114.     end
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # * Frame Update (when buy window is active)
  118.   #--------------------------------------------------------------------------
  119.   def update_buy
  120.     # Set status window item
  121.     @status_window.item = @buy_window.item
  122.     # If B button was pressed
  123.     if Input.trigger?(Input::B)
  124.       # Play cancel SE
  125.       $game_system.se_play($data_system.cancel_se)
  126.       # Change windows to initial mode
  127.       @command_window.active = true
  128.       @dummy_window.visible = true
  129.       @buy_window.active = false
  130.       @buy_window.visible = false
  131.       @status_window.visible = false
  132.       @status_window.item = nil
  133.       # Erase help text
  134.       @help_window.set_text("")
  135.       return
  136.     end
  137.     # If C button was pressed
  138.     if Input.trigger?(Input::C)
  139.       # Get item
  140.       @item = @buy_window.item
  141.       # If item is invalid, or price is higher than money possessed
  142.       if @item == nil or @item.price > $game_party.gold
  143.         # Play buzzer SE
  144.         $game_system.se_play($data_system.buzzer_se)
  145.         return
  146.       end
  147.       # Get items in possession count
  148.       case @item
  149.       when RPG::Item
  150.         number = $game_party.item_number(@item.id)
  151.       when RPG::Weapon
  152.         number = $game_party.weapon_number(@item.id)
  153.       when RPG::Armor
  154.         number = $game_party.armor_number(@item.id)
  155.       end
  156.       # If 99 items are already in possession
  157.       if number == 99
  158.         # Play buzzer SE
  159.         $game_system.se_play($data_system.buzzer_se)
  160.         return
  161.       end
  162.       # Play decision SE
  163.       $game_system.se_play($data_system.decision_se)
  164.       # Calculate maximum amount possible to buy
  165.       rebaja = @item.price*$game_temp.buy_discount
  166.       max = @item.price == 0 ? 99 : $game_party.gold / rebaja.to_i
  167.       max = [max, 99 - number].min
  168.       # Change windows to quantity input mode
  169.       @buy_window.active = false
  170.       @buy_window.visible = false
  171.       descuento = @item.price * $game_temp.buy_discount
  172.       @number_window.set(@item, max, descuento.to_i)
  173.       @number_window.active = true
  174.       @number_window.visible = true
  175.     end
  176.   end
  177.   #--------------------------------------------------------------------------
  178.   # * Frame Update (when sell window is active)
  179.   #--------------------------------------------------------------------------
  180.   def update_sell
  181.     # If B button was pressed
  182.     if Input.trigger?(Input::B)
  183.       # Play cancel SE
  184.       $game_system.se_play($data_system.cancel_se)
  185.       # Change windows to initial mode
  186.       @command_window.active = true
  187.       @dummy_window.visible = true
  188.       @sell_window.active = false
  189.       @sell_window.visible = false
  190.       @status_window.item = nil
  191.       # Erase help text
  192.       @help_window.set_text("")
  193.       return
  194.     end
  195.     # If C button was pressed
  196.     if Input.trigger?(Input::C)
  197.       # Get item
  198.       @item = @sell_window.item
  199.       # Set status window item
  200.       @status_window.item = @item
  201.       # If item is invalid, or item price is 0 (unable to sell)
  202.       if @item == nil or @item.price == 0
  203.         # Play buzzer SE
  204.         $game_system.se_play($data_system.buzzer_se)
  205.         return
  206.       end
  207.       # Play decision SE
  208.       $game_system.se_play($data_system.decision_se)
  209.       # Get items in possession count
  210.       case @item
  211.       when RPG::Item
  212.         number = $game_party.item_number(@item.id)
  213.       when RPG::Weapon
  214.         number = $game_party.weapon_number(@item.id)
  215.       when RPG::Armor
  216.         number = $game_party.armor_number(@item.id)
  217.       end
  218.       # Maximum quanitity to sell = number of items in possession
  219.       max = number
  220.       # Change windows to quantity input mode
  221.       @sell_window.active = false
  222.       @sell_window.visible = false
  223.       descuento = (@item.price * $game_temp.vend_desc) / 2
  224.       @number_window.set(@item, max, descuento.to_i)
  225.       @number_window.active = true
  226.       @number_window.visible = true
  227.       @status_window.visible = true
  228.     end
  229.   end
  230.   #--------------------------------------------------------------------------
  231.   # * Frame Update (when quantity input window is active)
  232.   #--------------------------------------------------------------------------
  233.   def update_number
  234.     # If B button was pressed
  235.     if Input.trigger?(Input::B)
  236.       # Play cancel SE
  237.       $game_system.se_play($data_system.cancel_se)
  238.       # Set quantity input window to inactive / invisible
  239.       @number_window.active = false
  240.       @number_window.visible = false
  241.       # Branch by command window cursor position
  242.       case @command_window.index
  243.       when 0  # buy
  244.         # Change windows to buy mode
  245.         @buy_window.active = true
  246.         @buy_window.visible = true
  247.       when 1  # sell
  248.         # Change windows to sell mode
  249.         @sell_window.active = true
  250.         @sell_window.visible = true
  251.         @status_window.visible = false
  252.       end
  253.       return
  254.     end
  255.     # If C button was pressed
  256.     if Input.trigger?(Input::C)
  257.       # Play shop SE
  258.       $game_system.se_play($data_system.shop_se)
  259.       # Set quantity input window to inactive / invisible
  260.       @number_window.active = false
  261.       @number_window.visible = false
  262.       # Branch by command window cursor position
  263.       case @command_window.index
  264.       when 0  # buy
  265.         # Buy process
  266.         modificado = @number_window.number * @item.price * $game_temp.buy_discount
  267.         $game_party.lose_gold(modificado.to_i)
  268.         case @item
  269.         when RPG::Item
  270.           $game_party.gain_item(@item.id, @number_window.number)
  271.         when RPG::Weapon
  272.           $game_party.gain_weapon(@item.id, @number_window.number)
  273.         when RPG::Armor
  274.           $game_party.gain_armor(@item.id, @number_window.number)
  275.         end
  276.         # Refresh each window
  277.         @gold_window.refresh
  278.         @buy_window.refresh
  279.         @status_window.refresh
  280.         # Change windows to buy mode
  281.         @buy_window.active = true
  282.         @buy_window.visible = true
  283.       when 1  # sell
  284.         # Sell process
  285.         modificado = (@item.price * $game_temp.vend_desc) / 2
  286.         $game_party.gain_gold(@number_window.number * modificado.to_i)
  287.         case @item
  288.         when RPG::Item
  289.           $game_party.lose_item(@item.id, @number_window.number)
  290.         when RPG::Weapon
  291.           $game_party.lose_weapon(@item.id, @number_window.number)
  292.         when RPG::Armor
  293.           $game_party.lose_armor(@item.id, @number_window.number)
  294.         end
  295.         # Refresh each window
  296.         @gold_window.refresh
  297.         @sell_window.refresh
  298.         @status_window.refresh
  299.         # Change windows to sell mode
  300.         @sell_window.active = true
  301.         @sell_window.visible = true
  302.         @status_window.visible = false
  303.       end
  304.       return
  305.     end
  306.   end
  307.  
  308. end
  309.  
  310. #==============================================================================
  311. # ** Window_ShopCommand
  312. #------------------------------------------------------------------------------
  313. #  This window is used to choose your business on the shop screen.
  314. #==============================================================================
  315.  
  316. class Window_ShopCommand < Window_Selectable
  317.   #--------------------------------------------------------------------------
  318.   # * Object Initialization
  319.   #--------------------------------------------------------------------------
  320.   def initialize
  321.     super(0, 64, 480, 64)
  322.     self.contents = Bitmap.new(width - 32, height - 32)
  323.     @item_max = 3
  324.     @column_max = 3
  325.     #Excepciones de tipo
  326.     case $game_temp.type
  327.       when 0; @commands = ["Comprar", "Vender", "Salir"]
  328.       when 1; @commands = ["Comprar", "Vender", "Salir"]
  329.       when 2; @commands = ["Comprar", "Vender", "Salir"]
  330.     end
  331.     refresh
  332.     self.index = 0
  333.   end
  334.   #--------------------------------------------------------------------------
  335.   # * Refresh
  336.   #--------------------------------------------------------------------------
  337.   def refresh
  338.     self.contents.clear
  339.     for i in 0...@item_max
  340.       draw_item(i)
  341.     end
  342.   end
  343.   #--------------------------------------------------------------------------
  344.   # * Draw Item
  345.   #     index : item number
  346.   #--------------------------------------------------------------------------
  347.   def draw_item(index)
  348.     x = 4 + index * 160
  349.       #Excepciones de tipo
  350.     case $game_temp.type
  351.       when 1
  352.         #Canvio de color de fuente y retorno de la misma
  353.         if index == 1
  354.           self.contents.font.color = disabled_color
  355.         end
  356.        
  357.         if index == 2
  358.           self.contents.font.color = normal_color
  359.         end
  360.          
  361.        
  362.       when 2
  363.         #Canvio de color de fuente y retorno de la misma
  364.         if index == 0
  365.           self.contents.font.color = disabled_color
  366.         end
  367.         if index == 1...2
  368.           self.contents.font.color = normal_color
  369.         end
  370.     end
  371.    
  372.     self.contents.draw_text(x, 0, 128, 32, @commands[index])
  373.   end
  374. end
  375.  
  376. #==============================================================================
  377. # ** Window_ShopBuy
  378. #------------------------------------------------------------------------------
  379. #  This window displays buyable goods on the shop screen.
  380. #==============================================================================
  381.  
  382. class Window_ShopBuy < Window_Selectable
  383.   #--------------------------------------------------------------------------
  384.   # * Draw Item
  385.   #     index : item number
  386.   #--------------------------------------------------------------------------
  387.   def draw_item(index)
  388.     item = @data[index]
  389.     # Get items in possession
  390.     case item
  391.     when RPG::Item
  392.       number = $game_party.item_number(item.id)
  393.     when RPG::Weapon
  394.       number = $game_party.weapon_number(item.id)
  395.     when RPG::Armor
  396.       number = $game_party.armor_number(item.id)
  397.     end
  398.     # If price is less than money in possession, and amount in possession is
  399.     # not 99, then set to normal text color. Otherwise set to disabled color
  400.     if item.price * $game_temp.buy_discount  <= $game_party.gold and number < 99
  401.       self.contents.font.color = normal_color
  402.     else
  403.       self.contents.font.color = disabled_color
  404.     end
  405.     x = 4
  406.     y = index * 32
  407.     rect = Rect.new(x, y, self.width - 32, 32)
  408.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  409.     bitmap = RPG::Cache.icon(item.icon_name)
  410.     opacity = self.contents.font.color == normal_color ? 255 : 128
  411.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  412.     self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
  413.    
  414.     rebajado = item.price * $game_temp.buy_discount
  415.     rebajado = rebajado.to_i
  416.    
  417.     self.contents.draw_text(x + 240, y, 88, 32, rebajado.to_s, 2)
  418.   end
  419. end
  420. #==============================================================================
  421. # ** Window_ShopBuy
  422. #------------------------------------------------------------------------------
  423. #  This window displays buyable goods on the shop screen.
  424. #==============================================================================
  425.  
  426. class Window_ShopBuy < Window_Selectable
  427.   #--------------------------------------------------------------------------
  428.   # * Draw Item
  429.   #     index : item number
  430.   #--------------------------------------------------------------------------
  431.   def draw_item(index)
  432.     item = @data[index]
  433.     # Get items in possession
  434.     case item
  435.     when RPG::Item
  436.       number = $game_party.item_number(item.id)
  437.     when RPG::Weapon
  438.       number = $game_party.weapon_number(item.id)
  439.     when RPG::Armor
  440.       number = $game_party.armor_number(item.id)
  441.     end
  442.     # If price is less than money in possession, and amount in possession is
  443.     # not 99, then set to normal text color. Otherwise set to disabled color
  444.     if item.price * $game_temp.buy_discount  <= $game_party.gold and number < 99
  445.       self.contents.font.color = normal_color
  446.     else
  447.       self.contents.font.color = disabled_color
  448.     end
  449.     x = 4
  450.     y = index * 32
  451.     rect = Rect.new(x, y, self.width - 32, 32)
  452.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  453.     bitmap = RPG::Cache.icon(item.icon_name)
  454.     opacity = self.contents.font.color == normal_color ? 255 : 128
  455.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  456.     self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
  457.    
  458.     rebajado = item.price * $game_temp.buy_discount
  459.     rebajado = rebajado.to_i
  460.    
  461.     self.contents.draw_text(x + 240, y, 88, 32, rebajado.to_s, 2)
  462.   end
  463. end
Advertisement
Add Comment
Please, Sign In to add comment