Advertisement
roninator2

Actor inventory Gold addon - Trade Gold

Dec 18th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.61 KB | None | 0 0
  1. # ╔═════════════════════════════════════╦════════════════════╗
  2. # ║ Title: Actor Trade Gold             ║  Version: 1.00     ║
  3. # ║ Author: Roninator2                  ║                    ║
  4. # ╠═════════════════════════════════════╬════════════════════╣
  5. # ║ Function: For trading gold between  ║   Date Created     ║
  6. # ║actors. Requires Hime Actor Inventory╠════════════════════╣
  7. # ║   and Roninator2 Gold Inventory     ║    29 Mar 2022     ║
  8. # ╚═════════════════════════════════════╩════════════════════╝
  9. # ╔══════════════════════════════════════════════════════════╗
  10. # ║ Allows to Trade gold between actors                      ║
  11. # ╚══════════════════════════════════════════════════════════╝
  12. # ╔══════════════════════════════════════════════════════════╗
  13. # ║ Instructions:                                            ║
  14. # ║   Set the variable below to one you will not use         ║
  15. # ╚══════════════════════════════════════════════════════════╝
  16. # ╔══════════════════════════════════════════════════════════╗
  17. # ║ Terms of use:                                            ║
  18. # ║ Free for all uses in RPG Maker except nudity             ║
  19. # ╚══════════════════════════════════════════════════════════╝
  20.  
  21. module R2_Gold_Trade
  22.   Variable = 10
  23. end
  24.  
  25. class Scene_Menu < Scene_MenuBase
  26.   alias r2_menu_command_gold_trade  create_command_window
  27.   def create_command_window
  28.     r2_menu_command_gold_trade
  29.     @command_window.set_handler(:trade_gold,    method(:command_trade_gold))
  30.   end
  31.   def command_trade_gold
  32.     SceneManager.call(Scene_Trade_Gold)
  33.   end
  34. end
  35.  
  36. class Window_MenuCommand < Window_Command
  37.   alias r2_trade_gold_menu_command  add_original_commands
  38.   def add_original_commands
  39.     r2_trade_gold_menu_command
  40.     add_command("Trade Gold",   :trade_gold)
  41.   end
  42. end
  43.  
  44. class Window_Gold_Trade_Command < Window_HorzCommand
  45.   def initialize(x, y, width)
  46.     @window_width = width
  47.     super(x, y)
  48.     activate
  49.   end
  50.   def window_width
  51.     @window_width
  52.   end
  53.   def col_max
  54.     return 2
  55.   end
  56.   def make_command_list
  57.     add_command("Trade Gold", :trade)
  58.     add_command("Cancel",     :cancel)
  59.   end
  60. end
  61.  
  62. class Window_Number_Trade < Window_Selectable
  63.   def initialize(x, y, width, height)
  64.     super(x, y, width, height)
  65.     @number = 0
  66.     @digits_max = 1
  67.     @index = 0
  68.     start
  69.   end
  70.   def start
  71.     @digits_max = 7
  72.     @number = $game_variables[R2_Gold_Trade::Variable]
  73.     @number = [[@number, 0].max, 10 ** @digits_max - 1].min
  74.     @index = 6
  75.     create_contents
  76.   end
  77.   def cursor_right(wrap)
  78.     if @index < @digits_max - 1 || wrap
  79.       @index = (@index + 1) % @digits_max
  80.     end
  81.     deactivate
  82.   end
  83.   def cursor_left(wrap)
  84.     if @index > 0 || wrap
  85.       @index = (@index + @digits_max - 1) % @digits_max
  86.     end
  87.     deactivate
  88.   end
  89.   def update
  90.     super
  91.     process_cursor_move
  92.     process_digit_change
  93.     process_handling
  94.     update_cursor
  95.   end
  96.   def process_cursor_move
  97.     if !active
  98.       activate
  99.       return
  100.     end
  101.     return unless active
  102.     last_index = @index
  103.     cursor_right(Input.trigger?(:RIGHT)) if Input.repeat?(:RIGHT)
  104.     cursor_left(Input.trigger?(:LEFT))  if Input.repeat?(:LEFT)
  105.     Sound.play_cursor if @index != last_index
  106.   end
  107.   def process_digit_change
  108.     return unless active
  109.     if Input.repeat?(:UP) || Input.repeat?(:DOWN)
  110.       Sound.play_cursor
  111.       place = 10 ** (@digits_max - 1 - @index)
  112.       n = @number / place % 10
  113.       @number -= n * place
  114.       n = (n + 1) % 10 if Input.repeat?(:UP)
  115.       n = (n + 9) % 10 if Input.repeat?(:DOWN)
  116.       @number += n * place
  117.       refresh
  118.     end
  119.   end
  120.   def process_handling
  121.     return unless active
  122.     return process_ok     if Input.trigger?(:C)
  123.     return process_max    if Input.trigger?(:X)
  124.     return process_cancel if Input.trigger?(:B)
  125.   end
  126.   def process_ok
  127.     Sound.play_ok
  128.     $game_variables[R2_Gold_Trade::Variable] = @number
  129.     deactivate
  130.     close
  131.     SceneManager.scene.on_number_ok
  132.   end
  133.   def process_max
  134.     Sound.play_ok
  135.     @number = @actor.inventory_gold
  136.     refresh
  137.   end
  138.   def process_cancel
  139.     Sound.play_cancel
  140.     $game_variables[R2_Gold_Trade::Variable] = 0
  141.     deactivate
  142.     close
  143.     SceneManager.scene.on_number_cancel
  144.   end
  145.   def item_rect(index)
  146.     Rect.new(index * 20, 0, 20, line_height)
  147.   end
  148.   def refresh
  149.     contents.clear
  150.     change_color(normal_color)
  151.     if @number > @actor.inventory_gold
  152.       @number = @actor.inventory_gold
  153.     end
  154.     s = sprintf("%0*d", @digits_max, @number)
  155.     @digits_max.times do |i|
  156.       rect = item_rect(i)
  157.       rect.x += 1
  158.       draw_text(rect, s[i,1], 1)
  159.     end
  160.   end
  161.   def update_cursor
  162.     cursor_rect.set(item_rect(@index))
  163.   end
  164.   def actor=(actor)
  165.     return if @actor == actor
  166.     @actor = actor
  167.     refresh
  168.   end
  169. end
  170.  
  171. class Window_ActorGold < Window_Selectable
  172.   attr_reader :actor
  173.   def initialize(actor)
  174.     super(0, 0, window_width, window_height)
  175.     @actor = actor
  176.     refresh
  177.   end
  178.   def window_width
  179.     return 200
  180.   end
  181.   def window_height
  182.     return 150
  183.   end
  184.   def standard_padding
  185.     return 10
  186.   end
  187.   def actor=(actor)
  188.     return if @actor == actor
  189.     @actor = actor
  190.     refresh
  191.   end
  192.   def refresh
  193.     contents.clear
  194.     draw_text(0, 0, width, line_height, @actor.name)
  195.     draw_actor_face(@actor, 40, 30)
  196.     draw_actor_gold(@actor, 0, 0)
  197.     draw_left_arrow(0, 70)
  198.     draw_right_arrow(140, 70)
  199.   end
  200.   def value(id)
  201.     $game_actors[id].inventory_gold
  202.   end
  203.   def currency_unit
  204.     Vocab::currency_unit
  205.   end
  206.   def draw_actor_gold(actor, x, y)
  207.     change_color(normal_color)
  208.     draw_currency_value(value(@actor.id), currency_unit, x, y, contents.width - 8)
  209.   end
  210.   def draw_left_arrow(x, y)
  211.     change_color(system_color)
  212.     draw_text(x, y, 40, line_height, "L <-", 0)
  213.   end
  214.   def draw_right_arrow(x, y)
  215.     change_color(system_color)
  216.     draw_text(x, y, 40, line_height, "-> R", 2)
  217.   end
  218. end
  219.  
  220. class Scene_Trade_Gold < Scene_MenuBase
  221.   def start
  222.     super
  223.     create_scene_window
  224.     create_command_window
  225.     @actor1 = $game_party.battle_members[0]
  226.     @actor2 = $game_party.battle_members[1]
  227.     create_actor_select_window
  228.     create_actor_trade_window
  229.     create_number_window
  230.   end
  231.   def create_scene_window
  232.     @scene_gold_trade_window = Window_Base.new(0, 0, Graphics.width, Graphics.height)
  233.     @scene_gold_trade_window.viewport = @viewport
  234.     @scene_gold_trade_window.deactivate
  235.   end
  236.   def create_command_window
  237.     @command_window = Window_Gold_Trade_Command.new(150, 10, 250)
  238.     @command_window.viewport = @viewport
  239.     @command_window.activate
  240.     @command_window.set_handler(:trade,    method(:command_trade))
  241.     @command_window.set_handler(:cancel, method(:return_scene))
  242.   end
  243.   def create_actor_select_window
  244.     @actor_status_window = Window_ActorGold.new(@actor1)
  245.     @actor_status_window.viewport = @viewport
  246.     @actor_status_window.x = 30
  247.     @actor_status_window.y = 100
  248.     @actor_status_window.actor = @actor1
  249.     @actor_status_window.deactivate
  250.     @actor_status_window.hide
  251.     @actor_status_window.set_handler(:ok,     method(:on_actor_ok))
  252.     @actor_status_window.set_handler(:pageup,   method(:change_actor_before))
  253.     @actor_status_window.set_handler(:pagedown, method(:change_actor_after))
  254.     @actor_status_window.set_handler(:cancel, method(:on_actor_cancel))
  255.   end
  256.   def create_actor_trade_window
  257.     @trade_status_window = Window_ActorGold.new(@actor2)
  258.     @trade_status_window.viewport = @viewport
  259.     @trade_status_window.x = 300
  260.     @trade_status_window.y = 100
  261.     @trade_status_window.actor = @actor2
  262.     @trade_status_window.deactivate
  263.     @trade_status_window.hide
  264.     @trade_status_window.set_handler(:ok,     method(:on_trade_ok))
  265.     @trade_status_window.set_handler(:pageup,   method(:change_actor_before))
  266.     @trade_status_window.set_handler(:pagedown, method(:change_actor_after))
  267.     @trade_status_window.set_handler(:cancel, method(:on_trade_cancel))
  268.   end
  269.   def create_number_window
  270.     @number_window = Window_Number_Trade.new(200, 300, 170, 48)
  271.     @number_window.viewport = @viewport
  272.     @number_window.hide
  273.     @number_window.deactivate
  274.     @number_window.actor = @actor1
  275.     @number_window.set_handler(:ok,     method(:on_number_ok))
  276.     @number_window.set_handler(:max,    method(:max_number_ok))
  277.     @number_window.set_handler(:cancel, method(:on_number_cancel))
  278.   end
  279.   def command_trade
  280.     @command_window.deactivate
  281.     @actor_status_window.show
  282.     @actor_status_window.activate
  283.     @actor_status_window.select(0)
  284.   end
  285.   def on_actor_ok
  286.     @actor1 = @actor_status_window.actor
  287.     @actor_status_window.unselect
  288.     @actor_status_window.deactivate
  289.     @trade_status_window.show
  290.     @trade_status_window.activate
  291.     @trade_status_window.select(0)
  292.   end
  293.   def on_actor_cancel
  294.     @actor_status_window.deactivate
  295.     @actor_status_window.hide
  296.     @command_window.activate
  297.   end
  298.   def on_trade_ok
  299.     text = "Press A to select Max currency"
  300.     @scene_gold_trade_window.draw_text_ex(100, 348, text)
  301.     @actor2 = @trade_status_window.actor
  302.     @trade_status_window.unselect
  303.     @trade_status_window.deactivate
  304.     @number_window.show
  305.     @number_window.open
  306.     @number_window.activate
  307.   end
  308.   def on_trade_cancel
  309.     @trade_status_window.deactivate
  310.     @trade_status_window.hide
  311.     @actor_status_window.activate
  312.     @actor_status_window.select(0)
  313.   end
  314.   def on_number_ok
  315.     trade_gold($game_variables[R2_Gold_Trade::Variable])
  316.     end_number_input
  317.   end
  318.   def on_number_cancel
  319.     Sound.play_cancel
  320.     end_number_input
  321.   end
  322.   def max_number_ok
  323.   end
  324.   def trade_gold(number)
  325.     return if @trade_status_window.active
  326.     $game_actors[@actor1.id].lose_gold(number)
  327.     $game_actors[@actor2.id].gain_gold(number)
  328.     @actor_status_window.refresh
  329.     @trade_status_window.refresh
  330.   end
  331.   def end_number_input
  332.     @scene_gold_trade_window.create_contents
  333.     @number_window.close
  334.     @number_window.deactivate
  335.     @trade_status_window.activate
  336.     @trade_status_window.select(0)
  337.   end
  338.   def change_actor_before
  339.     if @chkwindow == 1
  340.       id = $game_party.members.index(@actor_status_window.actor)
  341.     else @chkwindow == 2
  342.       id = $game_party.members.index(@trade_status_window.actor)
  343.     end
  344.     size = $game_party.members.size
  345.     id -= 1
  346.     id = size - 1 if id < 0
  347.     if @chkwindow == 1
  348.       @actor1 = $game_party.members[id]
  349.       @actor_status_window.actor = @actor1
  350.       @actor_status_window.activate
  351.     elsif @chkwindow == 2
  352.       @actor2 = $game_party.members[id]
  353.       @trade_status_window.actor = @actor2
  354.       @trade_status_window.activate
  355.     end
  356.     @number_window.actor = @actor1
  357.   end
  358.   def change_actor_after
  359.     if @chkwindow == 1
  360.       id = $game_party.members.index(@actor_status_window.actor)
  361.     else @chkwindow == 2
  362.       id = $game_party.members.index(@trade_status_window.actor)
  363.     end
  364.     size = $game_party.members.size
  365.     id += 1
  366.     id = 0 if id > size - 1
  367.     if @chkwindow == 1
  368.       @actor1 = $game_party.members[id]
  369.       @actor_status_window.actor = @actor1
  370.       @actor_status_window.activate
  371.     elsif @chkwindow == 2
  372.       @actor2 = $game_party.members[id]
  373.       @trade_status_window.actor = @actor2
  374.       @trade_status_window.activate
  375.     end
  376.     @number_window.actor = @actor1
  377.   end
  378.   alias r2_trade_gold_update  update
  379.   def update
  380.     r2_trade_gold_update
  381.     if @command_window.active
  382.       @chkwindow = 0
  383.     elsif @actor_status_window.active
  384.       @chkwindow = 1
  385.     elsif @trade_status_window.active
  386.       @chkwindow = 2
  387.     end
  388.   end
  389. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement