khanhdu

Open Shops

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