Advertisement
MrTrivel

MrTS_Open_Shops

Jun 10th, 2014
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.56 KB | None | 0 0
  1. # )--------------------------------------------------(
  2. # )--     Author:     Mr Trivel                    --(
  3. # )--     Name:       Open Shops                   --(
  4. # )--     Created:    2014-06-10                   --(
  5. # )--     Version:    1.0a                         --(
  6. # )--------------------------------------------------(
  7. # )--     Requires:   None                         --(
  8. # )--------------------------------------------------(
  9. # )--             Description                      --(
  10. # )--  Instead of talking to the store clerk, you  --(
  11. # )--  can just buy the item you see on the table. --(
  12. # )--------------------------------------------------(
  13. # )--             Instructions                     --(
  14. # )--  Call the item buy by using:                 --(
  15. # )--  SceneManager.call(MrTS_Open_Shop_Scene)     --(
  16. # )--  SceneManager.scene.prepare(type, id, price) --(
  17. # )--  type is: 0 - item, 1 - weapon, 2 - armor    --(
  18. # )--  id is item's ID in the database             --(
  19. # )--  price - at what value you want to sell the  --(
  20. # )--  item to the player. Don't enter it to use   --(
  21. # )--  default value.                              --(
  22. # )--  E.g: SceneManager.call(MrTS_Open_Shop_Scene)--(
  23. # )--       SceneManager.scene.prepare(1,24)       --(
  24. # )--  Will ask if you want to buy Tyrfang sword   --(
  25. # )--  for default price.                          --(
  26. # )--------------------------------------------------(
  27. # )--             LICENSE INFO                     --(
  28. # )--http://mrtrivelvx.wordpress.com/terms-of-use/ --(
  29. # )--------------------------------------------------(
  30.  
  31. module MrTS
  32.   module Open_Shop
  33.     # )---------------------------(
  34.     # )--  Opacity of the shop  --(
  35.     # )---------------------------(
  36.     OPACITY = 192
  37.   end
  38. end
  39.  
  40. # )-----------------------------------(
  41. # )--  Class: MrTS_Open_Shop_Scene  --(
  42. # )-----------------------------------(
  43. class MrTS_Open_Shop_Scene < Scene_Base
  44.  
  45.   # )---------------------(
  46.   # )--  Method: start  --(
  47.   # )---------------------(
  48.   def start
  49.     create_background
  50.     create_main_viewport
  51.   end
  52.  
  53.   # )-------------------------(
  54.   # )--  Method: terminate  --(
  55.   # )-------------------------(
  56.   def terminate
  57.     super
  58.     dispose_background
  59.     dispose_all_windows
  60.   end
  61.  
  62.   # )---------------------------------(
  63.   # )--  Method: create_background  --(
  64.   # )---------------------------------(
  65.   def create_background
  66.     @background_sprite = Sprite.new
  67.     @background_sprite.bitmap = SceneManager.background_bitmap
  68.   end
  69.  
  70.   # )----------------------------------(
  71.   # )--  Method: dispose_background  --(
  72.   # )----------------------------------(
  73.   def dispose_background
  74.     @background_sprite.dispose
  75.   end
  76.  
  77.   # )----------------------------------(
  78.   # )--  Method: create_all_windows  --(
  79.   # )----------------------------------(
  80.   def create_all_windows
  81.     create_gold_window
  82.     create_command_window
  83.     create_input_window
  84.     create_stat_window
  85.   end
  86.  
  87.   # )----------------------------------(
  88.   # )--  Method: create_stat_window  --(
  89.   # )----------------------------------(
  90.   def create_stat_window
  91.     wx = 0
  92.     wh =  @info_window.line_height*6 + @info_window.standard_padding*2
  93.     wy = Graphics.height - @info_window.height - wh
  94.     ww = 160
  95.     if @item.is_a?(RPG::EquipItem)
  96.       @stat_window = Window_Base.new(wx,wy,ww,wh)
  97.       6.times do |i|
  98.         @stat_window.change_color(@stat_window.system_color)
  99.         @stat_window.draw_text(0, @stat_window.line_height*i, 120, @stat_window.line_height, Vocab::param(i+2))
  100.         @stat_window.change_color(@stat_window.normal_color)
  101.         @stat_window.draw_text(0, @stat_window.line_height*i, @stat_window.contents.width, @stat_window.line_height, @item.params[i+2], 2)
  102.       end
  103.     end
  104.   end
  105.  
  106.   # )----------------------------------(
  107.   # )--  Method: create_gold_window  --(
  108.   # )----------------------------------(
  109.   def create_gold_window
  110.     @gold_window = Window_Gold.new
  111.     @gold_window.viewport = @viewport
  112.     @gold_window.x = Graphics.width - @gold_window.width
  113.     @gold_window.y = 0
  114.   end
  115.  
  116.   # )-----------------------(
  117.   # )--  Method: prepare  --(
  118.   # )-----------------------(
  119.   def prepare(type, index, price = -1)
  120.     case type
  121.     when 0
  122.       @item = $data_items[index]
  123.     when 1
  124.       @item = $data_weapons[index]
  125.     when 2
  126.       @item = $data_armors[index]
  127.     end
  128.     @info_window = MrTS_Open_Shop.new(type, index, price)
  129.     create_all_windows
  130.   end
  131.  
  132.   # )-------------------------------------(
  133.   # )--  Method: create_command_window  --(
  134.   # )-------------------------------------(
  135.   def create_command_window
  136.     @command_window = MrTS_Open_Shop_Command.new
  137.     @command_window.set_handler(:on_buy,   method(:on_buy))
  138.     @command_window.set_handler(:on_leave, method(:on_leave))
  139.     @command_window.set_handler(:cancel,   method(:return_scene))
  140.     @command_window.start
  141.   end
  142.  
  143.   # )----------------------(
  144.   # )--  Method: on_buy  --(
  145.   # )----------------------(
  146.   def on_buy
  147.     @input_window.open
  148.     @input_window.activate
  149.     @input_window.set_item(@item)
  150.   end
  151.  
  152.   # )------------------------(
  153.   # )--  Method: on_leave  --(
  154.   # )------------------------(
  155.   def on_leave
  156.     return_scene
  157.   end
  158.  
  159.   # )-----------------------------------(
  160.   # )--  Method: create_input_window  --(
  161.   # )-----------------------------------(
  162.   def create_input_window
  163.     wx = Graphics.width - 164 - @command_window.width
  164.     ww = 164
  165.     wh = @info_window.line_height + @info_window.standard_padding*2
  166.     wy = Graphics.height - @info_window.height - wh
  167.     @input_window = MrTS_Open_Shop_Input.new(wx, wy, ww, wh)
  168.     @input_window.openness = 0
  169.     @input_window.set_handler(:ok,   method(:on_buy_ok))
  170.     @input_window.set_handler(:cancel,   method(:on_buy_cancel))
  171.   end
  172.  
  173.   # )-------------------------(
  174.   # )--  Method: on_buy_ok  --(
  175.   # )-------------------------(
  176.   def on_buy_ok
  177.     total_sum = @input_window.get_number * @item.price
  178.     if $game_party.gold > total_sum
  179.       Sound.play_shop
  180.       $game_party.lose_gold(total_sum)
  181.       $game_party.gain_item(@item, @input_window.get_number)
  182.       return_scene
  183.     else
  184.       on_buy_cancel
  185.       Sound.play_buzzer
  186.     end
  187.   end
  188.  
  189.   # )-----------------------------(
  190.   # )--  Method: on_buy_cancel  --(
  191.   # )-----------------------------(
  192.   def on_buy_cancel
  193.     @input_window.close
  194.     @input_window.deactivate
  195.     @command_window.activate
  196.   end
  197. end
  198.  
  199. # )-----------------------------(
  200. # )--  Class: MrTS_Open_Shop  --(
  201. # )-----------------------------(
  202. class MrTS_Open_Shop < Window_Base
  203.  
  204.   # )-----------------------------(
  205.   # )--  Method: initialize  --(
  206.   # )-----------------------------(
  207.   def initialize(type, index, price = -1)
  208.     dh = line_height*4.5 + standard_padding*2
  209.     y = Graphics.height - dh
  210.     width = Graphics.width
  211.     super(0, y, width, dh)
  212.    
  213.     case type
  214.     when 0
  215.       @item = $data_items[index]
  216.     when 1
  217.       @item = $data_weapons[index]
  218.     when 2
  219.       @item = $data_armors[index]
  220.     end
  221.    
  222.     @price = price == -1 ? @item.price : price
  223.    
  224.     self.back_opacity = MrTS::Open_Shop::OPACITY
  225.    
  226.     create_contents
  227.     refresh
  228.   end
  229.  
  230.   # )-----------------------(
  231.   # )--  Method: refresh  --(
  232.   # )-----------------------(
  233.   def refresh
  234.     contents.clear
  235.     draw_item_name(@item, 0, 0)
  236.     draw_currency_value(@price, Vocab::currency_unit, 172, 0, 160)
  237.     change_color(normal_color)
  238.     pose = Vocab::Possession + ": "
  239.     pose_width = text_size(pose).width+32
  240.     draw_text(172+160+16, 0, pose_width, line_height, pose)
  241.     draw_text(172+160+16+pose_width, 0, 64, line_height, $game_party.item_number(@item))
  242.     draw_text_ex(0, line_height, @item.description)
  243.    
  244.     i = 0
  245.     if @item.is_a?(RPG::EquipItem)
  246.       $game_party.members.each do |member|
  247.         if member.equippable?(@item)
  248.           draw_actor_graphic(member, 16+96*i, 32+line_height*3)
  249.           draw_actor_equip_info(32+96*i, line_height*3, member)
  250.         else
  251.           i -= 1
  252.         end #if
  253.       i += 1
  254.       end #do
  255.     end#if
  256.   end
  257.  
  258.   # )-------------------------------------(
  259.   # )--  Method: draw_actor_equip_info  --(
  260.   # )-------------------------------------(
  261.   def draw_actor_equip_info(x, y, actor)
  262.     enabled = actor.equippable?(@item)
  263.     change_color(normal_color, enabled)
  264.     item1 = current_equipped_item(actor, @item.etype_id)
  265.     draw_actor_param_change(x, y+line_height/2, actor, item1) if enabled
  266.   end
  267.  
  268.   # )-------------------------------------(
  269.   # )--  Method: current_equipped_item  --(
  270.   # )-------------------------------------(
  271.   def current_equipped_item(actor, etype_id)
  272.     list = []
  273.     actor.equip_slots.each_with_index do |slot_etype_id, i|
  274.       list.push(actor.equips[i]) if slot_etype_id == etype_id
  275.     end
  276.     list.min_by {|item| item ? item.params[param_id] : 0 }
  277.   end
  278.  
  279.   # )---------------------------------------(
  280.   # )--  Method: draw_actor_param_change  --(
  281.   # )---------------------------------------(
  282.   def draw_actor_param_change(x, y, actor, item1)
  283.     rect = Rect.new(x, y, contents.width - 4 - x, line_height)
  284.     change = @item.params[param_id] - (item1 ? item1.params[param_id] : 0)
  285.     change_color(param_change_color(change))
  286.     draw_text(rect, sprintf("%+d", change))
  287.   end
  288.  
  289.   # )------------------------(
  290.   # )--  Method: param_id  --(
  291.   # )------------------------(
  292.   def param_id
  293.     @item.is_a?(RPG::Weapon) ? 2 : 3
  294.   end  
  295. end
  296.  
  297. # )-------------------------------------(
  298. # )--  Class: MrTS_Open_Shop_Command  --(
  299. # )-------------------------------------(
  300. class MrTS_Open_Shop_Command < Window_Command
  301.  
  302.   # )--------------------------(
  303.   # )--  Method: initialize  --(
  304.   # )--------------------------(
  305.   def initialize
  306.     super(Graphics.width-window_width, Graphics.height-line_height*6-standard_padding*4)
  307.     self.back_opacity = MrTS::Open_Shop::OPACITY
  308.   end
  309.  
  310.   # )---------------------(
  311.   # )--  Method: start  --(
  312.   # )---------------------(
  313.   def start
  314.     refresh
  315.     select(0)
  316.     activate
  317.   end
  318.  
  319.   # )---------------------------------(
  320.   # )--  Method: make_command_list  --(
  321.   # )---------------------------------(
  322.   def make_command_list
  323.     super
  324.     add_command("Buy",    :on_buy)
  325.     add_command("Leave",  :on_leave)
  326.   end
  327. end
  328.  
  329. # )-----------------------------------(
  330. # )--  Class: MrTS_Open_Shop_Input  --(
  331. # )-------------------------------------(
  332. class MrTS_Open_Shop_Input < Window_Selectable
  333.  
  334.   # )--------------------------(
  335.   # )--  Method: initialize  --(
  336.   # )--------------------------(
  337.   def initialize(*args)
  338.     super(*args)
  339.     @number = 1
  340.   end
  341.  
  342.   # )------------------------(
  343.   # )--  Method: set_item  --(
  344.   # )------------------------(
  345.   def set_item(item, price=-1)
  346.     @item = item
  347.     @price = price == -1 ? item.price : price
  348.     @max = $game_party.max_item_number(@item) - $game_party.item_number(@item)
  349.     @number = @max if @number > @max
  350.     refresh
  351.   end
  352.  
  353.   # )-----------------------(
  354.   # )--  Method: col_max  --(
  355.   # )-----------------------(
  356.   def col_max
  357.     return 1
  358.   end
  359.  
  360.   # )----------------------(
  361.   # )--  Method: update  --(
  362.   # )----------------------(
  363.   def update
  364.     super
  365.     if active
  366.       last_number = @number
  367.       update_number
  368.       if @number != last_number
  369.         Sound.play_cursor
  370.         refresh
  371.       end
  372.     end
  373.   end
  374.  
  375.   # )-----------------------------(
  376.   # )--  Method: update_number  --(
  377.   # )-----------------------------(
  378.   def update_number
  379.     change_number(1)   if Input.repeat?(:RIGHT)
  380.     change_number(-1)  if Input.repeat?(:LEFT)
  381.     change_number(10)  if Input.repeat?(:UP)
  382.     change_number(-10) if Input.repeat?(:DOWN)
  383.   end
  384.  
  385.   # )-----------------------------(
  386.   # )--  Method: change_number  --(
  387.   # )-----------------------------(
  388.   def change_number(amount)
  389.     @number += amount
  390.     @number = 1 if @number < 1
  391.     @number = @max if @number > @max
  392.   end
  393.  
  394.   # )---------------------------(
  395.   # )--  Method: draw_number  --(
  396.   # )---------------------------(
  397.   def draw_number
  398.     change_color(normal_color)
  399.     draw_text(12, 0, 64, line_height, @number)
  400.   end
  401.  
  402.   # )-----------------------(
  403.   # )--  Method: refresh  --(
  404.   # )-----------------------(
  405.   def refresh
  406.     contents.clear
  407.     draw_text(0, 0, 64, line_height, "x")
  408.     t_price = @price * @number
  409.     draw_text(contents.width-120, 0, 120, line_height, t_price.to_s + Vocab::currency_unit, 2)
  410.     draw_number
  411.   end
  412.  
  413.   # )--------------------------(
  414.   # )--  Method: get_number  --(
  415.   # )--------------------------(
  416.   def get_number
  417.     return @number
  418.   end
  419. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement