Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #------------------------------------------------------------------------------#
  2. #  Galv's Use Item on Event
  3. #------------------------------------------------------------------------------#
  4. #  For: RPGMAKER VX ACE
  5. #  Version 1.4
  6. #------------------------------------------------------------------------------#
  7. #  2012-12-13 - version 1.4 - added ability to set what happens when item is
  8. #                             used on nothing (or an event with no <key> tag).
  9. #                           - added a check for just category
  10. #  2012-12-07 - version 1.3 - bug where sometimes item doesn't register
  11. #  2012-12-07 - version 1.2 - fix indexing bug when changing categories
  12. #  2012-12-07 - version 1.1 - fixed bug when only using 1 category
  13. #  2012-12-07 - version 1.0 - release
  14. #------------------------------------------------------------------------------#
  15. #  This script allows you to bring up the "Use Key Item" box on the press of a
  16. #  button. When you select an item, it then activates the event directly in
  17. #  front of the player.
  18. #  Using EVENTING knowledge, you can set up conditional branches that contain
  19. #  'script' condition to tell the event what to do when the item the player
  20. #  selected was used on that event.
  21. #------------------------------------------------------------------------------#
  22. #  SCRIPTS for CONDITIONAL BRANCHES
  23. #------------------------------------------------------------------------------#
  24. #
  25. #  use_key?            # Returns true if you activated the event using an item.
  26. #
  27. #  key?(:category,item_id)    # Checks if that item was used.
  28. #                             # :category can be any from CATEGORIES below.
  29. #                             # item_id is the ID of the item from that category
  30. #
  31. #  keyc?(:category)           # Check if any item from that category was used
  32. #
  33. #------------------------------------------------------------------------------#
  34. #  SCRIPT call for ENDING an item-initiated event (IMPORTANT)
  35. #------------------------------------------------------------------------------#
  36. #
  37. #  end_key      # Use this instead of "Exit Event Processing" event command in
  38. #               # your item activated events to clear the item ID if you want
  39. #               # to end the event early like in the demo examples.
  40. #
  41. #------------------------------------------------------------------------------#
  42. #  COMMENT TAG FOR EVENTS
  43. #------------------------------------------------------------------------------#
  44. #
  45. #  <key>
  46. #
  47. #  Place a COMMENT in the event page with only this tag in it. Make sure the
  48. #  comment is BELOW all conditional branches checking for item used on the event
  49. #
  50. #  Any event that has this comment will be activated when using an item on it
  51. #  (just like if you used the action key). Any event without this tag will not
  52. #  be activated at all and will revert to the common event id (selected below).
  53. #
  54. #------------------------------------------------------------------------------#
  55.  
  56. ($imported ||= {})["Galvs_Use_Item_on_Event"] = true
  57. module Galv_Key
  58.  
  59. #------------------------------------------------------------------------------#
  60. #  SCRIPT SETTINGS
  61. #------------------------------------------------------------------------------#  
  62.  
  63.   DISABLE_SWITCH = 1
  64.  
  65.   BUTTON = :X       # Button to call Key Item window (:X is "a")
  66.  
  67.   VARIABLE = 1      # Stores item ID in this variable to use in conditions
  68.  
  69.   NO_TARGET = 1     # Common event ID. This is called when the player uses an
  70.                     # item on nothing and can be used to set what happens for
  71.                     # each item/category in this event.
  72.                     # IMPORTANT: Set to 0 if not used. Common event MUST have
  73.                     # the key_end script call at the end of the event and end
  74.                     # of any branch as normal.
  75.  
  76.   CATEGORIES = [                 # Categories of items that can be used on an
  77.                                  # event. Cycle through these categories with
  78.     [:key_item, "Chìa Khoá"],    # Q and W. Remove any you do not want to use
  79.     [:item, "Vật Phẩm"],            # in your game.
  80.     [:weapon, "Vũ Khí"],
  81.     [:armor, "Trang Bị"],
  82.    
  83.     ] # don't touch
  84.   Font.default_name = "Baloo Bhaijaan Regular"
  85.   CHANGE_CAT_LEFT = "S <  "      # Text before the heading
  86.   LEFT_BUTTON = :Y               # Button to swap category to the left
  87.   CHANGE_CAT_RIGHT = "  > D"     # Text after the heading
  88.   RIGHT_BUTTON = :Z              # Button to swap category to the right
  89.    
  90.    
  91. #------------------------------------------------------------------------------#
  92. #  END SCRIPT SETTINGS
  93. #------------------------------------------------------------------------------#  
  94.  
  95. end
  96.  
  97.  
  98. class Game_Interpreter
  99.  
  100.   alias galv_key_item_interpreter_execute_command execute_command
  101.   def execute_command
  102.     if !@list[@index].nil?
  103.       if @list[@index].parameters[0] == "<key>"
  104.         $game_variables[Galv_Key::VARIABLE] = 0
  105.       end
  106.     end
  107.     galv_key_item_interpreter_execute_command
  108.   end
  109.  
  110.   def end_key
  111.     @index = @list.size
  112.     $game_variables[Galv_Key::VARIABLE] = 0
  113.   end
  114.  
  115.   def use_key?
  116.     $game_variables[Galv_Key::VARIABLE] > 0
  117.   end
  118.  
  119.   def key?(cat,item_id)
  120.     if cat == $game_system.key_item_cat && $game_variables[Galv_Key::VARIABLE] == item_id
  121.       return true
  122.     else
  123.       return false
  124.     end
  125.   end
  126.  
  127.   def keyc?(cat)
  128.     if cat == $game_system.key_item_cat && $game_variables[Galv_Key::VARIABLE] > 0
  129.       return true
  130.     else
  131.       return false
  132.     end
  133.   end
  134.  
  135. end # Game_Interpreter
  136.  
  137.  
  138. class Game_Player < Game_Character
  139.  
  140.   alias galv_key_item_move_by_input move_by_input
  141.   def move_by_input
  142.     galv_key_item_move_by_input
  143.     if Input.trigger?(Galv_Key::BUTTON)
  144.       return if $game_switches[Galv_Key::DISABLE_SWITCH] || jumping?
  145.       $game_message.item_choice_variable_id = Galv_Key::VARIABLE if !$game_map.interpreter.running?
  146.     end
  147.   end
  148.  
  149.   def use_on_event
  150.     case @direction
  151.     when 2; dirx = 0; diry = 1
  152.     when 4; dirx = -1; diry = 0
  153.     when 6; dirx = 1; diry = 0
  154.     when 8; dirx = 0; diry = -1
  155.     end
  156.    
  157.     @enable_event = false
  158.     for event in $game_map.events_xy(@x+dirx, @y+diry)
  159.       event.list.count.times { |i|
  160.         if event.list[i].code == 108 && event.list[i].parameters[0] == "<key>"
  161.           @enable_event = true
  162.         end
  163.       }
  164.     end
  165.    
  166.     if @enable_event
  167.       event.start
  168.     else
  169.       if Galv_Key::NO_TARGET > 0
  170.         $game_temp.reserve_common_event(Galv_Key::NO_TARGET)
  171.       else
  172.         $game_variables[Galv_Key::VARIABLE] = 0
  173.       end
  174.     end
  175.   end
  176. end # Game_Player < Game_Character
  177.  
  178.  
  179. class Window_KeyItem < Window_ItemList
  180.  
  181.   alias galv_key_item_key_window_on_ok on_ok
  182.   def on_ok
  183.     galv_key_item_key_window_on_ok
  184.     $game_player.use_on_event
  185.     clear_heading
  186.   end
  187.  
  188.   alias galv_key_item_key_window_on_cancel on_cancel
  189.   def on_cancel
  190.     galv_key_item_key_window_on_cancel
  191.     clear_heading
  192.   end
  193.  
  194.   alias galv_key_item_key_window_start start
  195.   def start
  196.     if !$game_switches[Galv_Key::DISABLE_SWITCH]
  197.       self.category = $game_system.key_item_cat
  198.       draw_heading
  199.       update_placement
  200.       refresh
  201.       select(0)
  202.       open
  203.       activate
  204.     else
  205.       galv_key_item_key_window_start
  206.     end
  207.   end
  208.  
  209.   def draw_heading
  210.     return if @heading != nil
  211.     @heading = Sprite.new
  212.     @heading.bitmap = Bitmap.new(Graphics.width, 25)
  213.     @heading.bitmap.font.size = 25
  214.     @heading.bitmap.font.color.set(text_color(0))
  215.     @position = 0 if @position.nil?
  216.     if Galv_Key::CATEGORIES.count > 1
  217.       text = Galv_Key::CHANGE_CAT_LEFT + Galv_Key::CATEGORIES[@position][1] + Galv_Key::CHANGE_CAT_RIGHT
  218.     else
  219.       text = Galv_Key::CATEGORIES[@position][1]
  220.     end
  221.    
  222.     @heading.bitmap.draw_text(@heading.bitmap.rect, text, 1)
  223.    
  224.     if @message_window.y >= Graphics.height / 2
  225.       @heading.y = @message_window.height
  226.     else
  227.       @heading.y = Graphics.height - height + @message_window.height
  228.     end
  229.    
  230.     @heading.z = z + 1
  231.     @heading.opacity = 0
  232.   end
  233.  
  234.   def clear_heading
  235.     @heading.dispose
  236.     @heading.bitmap.dispose
  237.     @heading = nil
  238.   end
  239.  
  240.   def enable?(item)
  241.     if !$game_switches[Galv_Key::DISABLE_SWITCH]
  242.       true
  243.     else
  244.       super
  245.     end
  246.   end
  247.  
  248.   def update
  249.     super
  250.     @heading.opacity = openness if !@heading.nil?
  251.     if self.active && Galv_Key::CATEGORIES.count > 1
  252.       if Input.trigger?(Galv_Key::LEFT_BUTTON)
  253.         Galv_Key::CATEGORIES.each_with_index do |c,i|
  254.           @position = i if c[0] == $game_system.key_item_cat
  255.         end
  256.         @position -= 1
  257.         @position = Galv_Key::CATEGORIES.count - 1 if @position < 0
  258.         self.category = $game_system.key_item_cat = Galv_Key::CATEGORIES[@position][0]
  259.         clear_heading
  260.         draw_heading
  261.         select(0)
  262.       end
  263.       if Input.trigger?(Galv_Key::RIGHT_BUTTON)
  264.         Galv_Key::CATEGORIES.each_with_index do |c,i|
  265.           @position = i if c[0] == $game_system.key_item_cat
  266.         end
  267.         @position += 1
  268.         @position = 0 if @position >= Galv_Key::CATEGORIES.count
  269.         self.category = $game_system.key_item_cat = Galv_Key::CATEGORIES[@position][0]
  270.         clear_heading
  271.         draw_heading
  272.         select(0)
  273.       end
  274.     end
  275.   end
  276.  
  277. end # Window_KeyItem < Window_ItemList
  278.  
  279. class Game_System
  280.   attr_accessor :key_item_cat
  281.  
  282.   alias galv_key_item_system_initialize initialize
  283.   def initialize
  284.     galv_key_item_system_initialize
  285.     @key_item_cat = Galv_Key::CATEGORIES[0][0]
  286.   end
  287. end