Advertisement
ezmash

Super Simple Mouse Script - All In One (VX Ace)

Sep 12th, 2013
12,840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 39.84 KB | None | 0 0
  1. #============================================================================
  2. # SUPER SIMPLE MOUSE SCRIPT
  3. # v1.10 by Shaz
  4. #----------------------------------------------------------------------------
  5. # This is a conversion of the XP Mouse script by Near Fantastica and
  6. # SephirothSpawn modified by Amaranth Games, to run under VX Ace.
  7. #----------------------------------------------------------------------------
  8. # To Install:
  9. # Copy and paste into a new slot in materials, below all other scripts
  10. #----------------------------------------------------------------------------
  11. # To Customize:
  12. # Add keyword icon index pairs to the ICON hash (below this documentation).  
  13. # Each of the keywords can be used in an event comment to make the mouse
  14. # cursor change into that icon when hovering over the event.
  15. #----------------------------------------------------------------------------
  16. # To Use:
  17. # Add the following comment to an event page:
  18. #   <mouse icon [x y] [name]>
  19. #   where icon is the keyword from the ICON hash below
  20. #   x and y are the offsets to override player movement (optional)
  21. #   name is the text to display next to the icon when hovering over the event (optional)
  22. #
  23. # Examples:
  24. # <mouse fight>
  25. #   will change the cursor into the 'fight' icon when over the event
  26. # <mouse touch 0 1>
  27. #   will change the cursor into the 'touch' icon when over the event, and
  28. #   make the player walk to the tile below the event when the mouse button is
  29. #   clicked
  30. # <mouse talk Gloria>
  31. #   will change the cursor into the 'talk' icon and display the name Gloria
  32. # <mouse talk 0 2 Henry Smith>
  33. #   will change the cursor into the 'talk' icon and display the name Henry Smith,
  34. #   and when the mouse button is clicked, the player will walk to the tile
  35. #   two below the event (good to use for shops where there's a counter in between)
  36. #
  37. # To force pathfinding on the player or an event, simply add a move route with
  38. # the player or event as the subject, with a Script command, and call
  39. # find_path(x, y) where x and y are the coordinates of the tile you want to move to
  40. # Examples:
  41. # Set Move Route (Player): Script: find_path(5, 8)
  42. #   will make the player find a path to tile 5, 8
  43. # Set Move Route (This Event): Script: find_path(10, 5)
  44. #   will make the event find a path to tile 10, 5
  45. #
  46. # NOTE: The path will be ATTEMPTED.  If there is no path TO that exact tile,
  47. # a path to an adjacent tile will be attempted.  If no path is found there
  48. # either, no movement will occur.
  49. # If a route is found, the player or event will begin moving towards it.  But
  50. # if their path is blocked while they are moving, movement will be cancelled.
  51. #----------------------------------------------------------------------------
  52. # Author's Notes:
  53. # This script should work with any RTP script.
  54. # I do not guarantee that it will work with ANY other script (especially anything
  55. # that overrides player or event movement, such as pixel movement scripts, or
  56. # custom window scripts).
  57. #
  58. # Script OVERWRITES the following methods:
  59. # Game_Map.setup_starting_map_event
  60. # Game_Map.setup_autorun_common_event
  61. #
  62. # If you have other scripts that ALIAS these methods, this mouse script should
  63. # be placed above them.
  64. #----------------------------------------------------------------------------
  65. # Terms:
  66. # Use in free and commercial games
  67. # Credit: Near Fantastica, SephirothSpawn, Amaranth Games, Shaz
  68. #----------------------------------------------------------------------------
  69. # Versions:
  70. # 1.0  -  6 Sept 2013 - initial release
  71. # 1.02 -  7 Sept 2013 - fixed crash when loading games saved prior to adding script
  72. #                     - fixed player gets stuck on impassable area on world map
  73. #                       when clicking while leaving air ship
  74. # 1.03 -  8 Sept 2013 - fixed actor moving to diagonal tile instead of adjacent
  75. # 1.04 - 10 Sept 2013 - fixed vehicle pathfinding on world map
  76. #                     - fixed event trigger when no path found
  77. # 1.05 - 14 Sept 2013 - tweaked accessing of tilemap offset
  78. # 1.06 -  3 Nov  2013 - disabled mouse movement when waiting for NPC move route
  79. #                     - fixed events not triggering after player finishes walking
  80. # 1.07 -  6 Nov  2013 - slow down mouse scrolling, and don't loop save files
  81. # 1.08 - 24 Nov  2013 - cater for YEA Core large resolution with too-small maps
  82. #                     - fixed early event activation bug introduced in 1.06
  83. #                     - replaced calc of Windows_Selectable boundaries with item_rect
  84. #                     - added ability to completely disable mouse
  85. # 1.09 - 21 Dec  2013 - fixed mouse re-enable when calling common events
  86. # 1.10 -  6 Apr  2014 - add interaction for top part of > 32pixel high event
  87. #                     - activate an event without walking up to it
  88. #                       (add <autoactivate> comment at top of event page)
  89. #                     - arrow keys override mouse movement when pathfinding
  90. #                     - ignore mouse in menus when using keyboard
  91. #                     - make player walk to counter opposite shopkeepers
  92. #============================================================================
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. #============================================================================
  100. # SUPER SIMPLE MOUSE SCRIPT
  101. # Mouse Sprite
  102. #============================================================================
  103.  
  104.  
  105. # Add/remove/change icon names here.  The icon name is what will be used in the
  106. # event <mouse ...> command to show a different mouse icon when hovering over
  107. # the event.  These MUST be in lower case here!
  108. ICON = {'arrow' => 386, 'talk' => 4, 'look' => 3, 'fight' => 116,
  109.         'touch' => 491, 'exit' => 121}
  110. DEFAULT_ICON = 'arrow'
  111.  
  112. class Sprite_Mouse < Sprite
  113.   #--------------------------------------------------------------------------
  114.   # * Initialization
  115.   #--------------------------------------------------------------------------
  116.   def initialize
  117.     super
  118.     self.z = 10100
  119.     self.ox = 4
  120.     update
  121.     @dummy = Bitmap.new(32, 32)
  122.     self.bitmap = Bitmap.new(32, 32)
  123.     @enabled = true
  124.     @ignored = false
  125.   end
  126.   #--------------------------------------------------------------------------
  127.   # * Frame Update
  128.   #--------------------------------------------------------------------------
  129.   def update
  130.     return if !@enabled
  131.     super
  132.     if !SceneManager.scene.nil?
  133.       if !Mouse.position.nil?
  134.         mx, my = *Mouse.position
  135.         if @cursor == DEFAULT_ICON
  136.           self.x = mx unless mx.nil?
  137.         else
  138.           self.x = [mx, Graphics.width - self.bitmap.width].min unless mx.nil?
  139.         end
  140.         self.y = my unless my.nil?
  141.       end
  142.       if @scene != SceneManager.scene.class || Mouse.trigger?
  143.         @scene = SceneManager.scene.class
  144.         set_bitmap
  145.       end
  146.     end
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   # * Set Bitmap
  150.   #--------------------------------------------------------------------------
  151.   def set_bitmap(cursor = DEFAULT_ICON, text = nil)
  152.     if @ignored
  153.       cursor = DEFAULT_ICON
  154.       text = nil
  155.     end
  156.      
  157.     if @cursor != cursor || @text != text
  158.       @cursor = cursor
  159.       @text = text
  160.       item_cursor = ICON[cursor]
  161.       rect = Rect.new(item_cursor % 16 * 24, item_cursor / 16 * 24, 24, 24)
  162.       if @text.nil?
  163.         self.bitmap = Bitmap.new(24, 32)
  164.         self.bitmap.blt(0, 0, Cache.system('Iconset'), rect)
  165.       else
  166.         w = @dummy.text_size(@text).width
  167.         h = @dummy.font.size
  168.         bitmap = Bitmap.new(26 + w, [32, h+2].max)
  169.         bitmap.font.size = @dummy.font.size
  170.         bitmap.font.shadow = true
  171.         if self.x + 26 + w > Graphics.width
  172.           bitmap.draw_text(0, 0, w, h, @text)
  173.           bitmap.blt(w, 0, Cache.system('Iconset'), rect)
  174.         else
  175.           bitmap.blt(0, 0, Cache.system('Iconset'), rect)
  176.           bitmap.draw_text(26, 0, w, h, @text)
  177.         end
  178.         self.bitmap = bitmap
  179.       end
  180.     end
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   # * Update Event Cursors
  184.   #--------------------------------------------------------------------------
  185.   def update_event_cursors
  186.     # Remove mouse icon and text if we're off the grid
  187.     if Mouse.grid.nil?
  188.       set_bitmap
  189.       return
  190.     end
  191.     # Set cursor and text according to event
  192.     x, y = *Mouse.grid
  193.     event = $game_map.lowest_mouse_event_xy(x, y)
  194.     unless event.nil? && y < 410
  195.       if !event.mouse_icon.nil? || !event.mouse_text.nil?
  196.         set_bitmap(event.mouse_icon, event.mouse_text)
  197.         return
  198.       end
  199.     end
  200.     # default bitmap if not over an event
  201.     set_bitmap
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # * Enable Mouse
  205.   #--------------------------------------------------------------------------
  206.   def enabled=(value)
  207.     @enabled = value
  208.     self.visible = value
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * Mouse Enabled?
  212.   #--------------------------------------------------------------------------
  213.   def enabled?
  214.     @enabled
  215.   end
  216.   #--------------------------------------------------------------------------
  217.   # * Ignore Mouse
  218.   #--------------------------------------------------------------------------
  219.   def ignored=(value)
  220.     @ignored = value
  221.   end
  222.   #--------------------------------------------------------------------------
  223.   # * Mouse Ignored?
  224.   #--------------------------------------------------------------------------
  225.   def ignored?
  226.     @ignored
  227.   end
  228. end
  229.  
  230. $mouse = Sprite_Mouse.new
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. #============================================================================
  238. # SUPER SIMPLE MOUSE SCRIPT
  239. # Mouse Module
  240. #============================================================================
  241.  
  242.  
  243. #==============================================================================
  244. # ** Mouse Module
  245. #------------------------------------------------------------------------------
  246. #  by Near Fantastica and SephirothSpawn
  247. #  adapted and converted to VX Ace by Shaz
  248. #==============================================================================
  249. module Mouse
  250.   #--------------------------------------------------------------------------
  251.   # * Mouse to Input Triggers
  252.   #   key => Input::KeyCONSTANT (key: 0 - left, 1 - middle, 2 - right)
  253.   #--------------------------------------------------------------------------
  254.   Mouse_to_Input_Triggers = {0 => Input::C, 1 => Input::B, 2 => Input::A}
  255.   #--------------------------------------------------------------------------
  256.   # * API Declarations
  257.   #--------------------------------------------------------------------------
  258.   GAKS = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i')
  259.   GSM = Win32API.new('user32', 'GetSystemMetrics', 'i', 'i')
  260.   Cursor_Pos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  261.   Scr2cli = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
  262.   Client_rect = Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
  263.   Findwindow = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')
  264.   Readini = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
  265.   ShowCursor = Win32API.new('user32', 'ShowCursor', 'i', 'l')
  266.   #--------------------------------------------------------------------------
  267.   # * Module Variables
  268.   #--------------------------------------------------------------------------
  269.   @triggers = [[0, 1], [0, 2], [0, 4]]
  270.   @old_pos = 0
  271.   @pos_i = 0
  272.   @sys_cursor_visible = false
  273.   #--------------------------------------------------------------------------
  274.   # * Mouse Grid Position
  275.   #--------------------------------------------------------------------------
  276.   def self.grid
  277.     return nil if @pos.nil?
  278.     mx, my = SceneManager.scene.instance_variable_get(:@spriteset).tilemap_offset
  279.     x = (@pos[0] + mx) / 32
  280.     y = (@pos[1] + my) / 32
  281.     return [x, y]
  282.   end
  283.   #--------------------------------------------------------------------------
  284.   # * Mouse Position
  285.   #--------------------------------------------------------------------------
  286.   def self.position
  287.     return @pos.nil? ? [0, 0] : @pos
  288.   end
  289.   #--------------------------------------------------------------------------
  290.   # * Mouse Global Position
  291.   #--------------------------------------------------------------------------
  292.   def self.global_pos
  293.     pos = [0, 0].pack('ll')
  294.     return Cursor_Pos.call(pos) == 0 ? nil : pos.unpack('ll')
  295.   end
  296.   #--------------------------------------------------------------------------
  297.   # * Screen to Client
  298.   #--------------------------------------------------------------------------
  299.   def self.screen_to_client(x=0, y=0)
  300.     pos = [x, y].pack('ll')
  301.     return Scr2cli.call(self.hwnd, pos) == 0 ? nil : pos.unpack('ll')
  302.   end
  303.   #--------------------------------------------------------------------------
  304.   # * Mouse Position
  305.   #--------------------------------------------------------------------------
  306.   def self.pos
  307.     gx, gy = global_pos
  308.     x, y = screen_to_client(gx, gy)
  309.    
  310.     # Test boundaries
  311.     begin
  312.       if (x >= 0 && y >= 0 && x <= Graphics.width && y <= Graphics.height)
  313.         return x, y
  314.       else
  315.         return -20, -20
  316.       end
  317.     rescue
  318.       return 0, 0
  319.     end
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   # * Update Mouse Position
  323.   #--------------------------------------------------------------------------
  324.   def self.update
  325.     old_pos = @pos
  326.     @pos = self.pos
  327.    
  328.     # Has mouse been moved?
  329.     if old_pos != @pos
  330.       Input.method = :mouse
  331.     end
  332.    
  333.     # Which mouse to show - custom, or system?
  334.     if $mouse.enabled? == @sys_cursor_visible
  335.       @sys_cursor_visible = !@sys_cursor_visible
  336.       ShowCursor.call(@sys_cursor_visible ? 1 : 0)
  337.     end
  338.    
  339.     return if !$mouse.enabled?
  340.    
  341.     # Leaving / Entering Range?
  342.     if old_pos != [-20, -20] && @pos == [-20, -20] # leaving range
  343.       ShowCursor.call(1)
  344.     elsif old_pos == [-20, -20] && @pos != [-20, -20] # entering range
  345.       ShowCursor.call(0)
  346.     end
  347.    
  348.     # Update Triggers
  349.     for i in @triggers
  350.       n = GAKS.call(i[1])
  351.       if [0, 1].include?(n)
  352.         i[0] = (i[0] > 0 ? i[0] * -1 : 0)
  353.       else
  354.         i[0] = (i[0] > 0 ? i[0] + 1 : 1)
  355.       end
  356.     end
  357.   end
  358.   #--------------------------------------------------------------------------
  359.   # * Trigger?
  360.   #   id : 0:Left, 1:Right, 2:Center
  361.   #--------------------------------------------------------------------------
  362.   def self.trigger?(id = 0)
  363.     if pos != [-20, -20]
  364.       return @triggers[id][0] == 1
  365.     end
  366.     return false
  367.   end
  368.   #--------------------------------------------------------------------------
  369.   # * Repeat?
  370.   #   id : 0:Left, 1:Right, 2:Center
  371.   #--------------------------------------------------------------------------
  372.   def self.repeat?(id = 0)
  373.     return @triggers[id][0] > 0 && @triggers[id][0] % 5 == 1
  374.   end
  375.   #--------------------------------------------------------------------------
  376.   # * Hwnd
  377.   #--------------------------------------------------------------------------
  378.   def self.hwnd
  379.     if @hwnd.nil?
  380.       title = "\0" * 256
  381.       Readini.call('Game', 'Title', '', title, 255, '.\\Game.ini')
  382.       title.delete!("\0")
  383.       @hwnd = Findwindow.call('RGSS Player', title)
  384.       ShowCursor.call(0)
  385.     end
  386.     return @hwnd
  387.   end
  388.   #--------------------------------------------------------------------------
  389.   # * Client Size
  390.   #--------------------------------------------------------------------------
  391.   def self.client_size
  392.     rect = [0, 0, 0, 0].pack('l4')
  393.     Client_rect.call(self.hwnd, rect)
  394.     return rect.unpack('l4')[2..3]
  395.   end
  396. end
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403. #============================================================================
  404. # SUPER SIMPLE MOUSE SCRIPT
  405. # Input
  406. #============================================================================
  407.  
  408.  
  409. class << Input
  410.   #--------------------------------------------------------------------------
  411.   # * Public Instance Variables
  412.   #--------------------------------------------------------------------------
  413.   attr_accessor :method
  414.   #--------------------------------------------------------------------------
  415.   # * Alias Listings
  416.   #--------------------------------------------------------------------------
  417.   alias :seph_mouse_input_update :update
  418.   alias :seph_mouse_input_trigger? :trigger?
  419.   alias :seph_mouse_input_repeat? :repeat?
  420.   #--------------------------------------------------------------------------
  421.   # * Frame Update
  422.   #--------------------------------------------------------------------------
  423.   def update
  424.     $mouse.update
  425.     Mouse.update
  426.     seph_mouse_input_update
  427.     # Are we using the mouse or the keyboard?
  428.     @method = :keyboard if dir4 != 0 || dir8 != 0
  429.   end
  430.   #--------------------------------------------------------------------------
  431.   # * Trigger? Test
  432.   #--------------------------------------------------------------------------
  433.   def trigger?(constant)
  434.     return true if seph_mouse_input_trigger?(constant)
  435.     if $mouse.enabled? && !Mouse.pos.nil?
  436.       if Mouse::Mouse_to_Input_Triggers.has_value?(constant)
  437.         return true if Mouse.trigger?(Mouse::Mouse_to_Input_Triggers.index(constant))
  438.       end
  439.     end
  440.     return false
  441.   end
  442.   #--------------------------------------------------------------------------
  443.   # * Repeat? Test
  444.   #--------------------------------------------------------------------------
  445.   def repeat?(constant)
  446.     return true if seph_mouse_input_repeat?(constant)
  447.     if $mouse.enabled? && !Mouse.pos.nil?
  448.       if Mouse::Mouse_to_Input_Triggers.has_value?(constant)
  449.         return true if Mouse.repeat?(Mouse::Mouse_to_Input_Triggers.index(constant))
  450.       end
  451.     end
  452.     return false
  453.   end
  454. end
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461. #============================================================================
  462. # SUPER SIMPLE MOUSE SCRIPT
  463. # Map
  464. #============================================================================
  465.  
  466.  
  467. class Spriteset_Map
  468.   #--------------------------------------------------------------------------
  469.   # * Tilemap Offset
  470.   #--------------------------------------------------------------------------
  471.   def tilemap_offset
  472.     if $imported && $imported["YEA-CoreEngine"]
  473.       [@tilemap.ox - @viewport1.rect.x, @tilemap.oy - @viewport1.rect.y]
  474.     else
  475.       [@tilemap.ox, @tilemap.oy]
  476.     end
  477.   end
  478. end
  479.  
  480. class Game_Map
  481.   #--------------------------------------------------------------------------
  482.   # * Detect/Set Up Starting Map Event
  483.   #--------------------------------------------------------------------------
  484.   def setup_starting_map_event
  485.     event = @events.values.find {|event| event.starting }
  486.     event.clear_starting_flag if event
  487.     @interpreter.setup(event.list, event.id, event.trigger_in?([0,1,2,3])) if event
  488.     event
  489.   end
  490.   #--------------------------------------------------------------------------
  491.   # * Detect/Set Up Autorun Common Event
  492.   #--------------------------------------------------------------------------
  493.   def setup_autorun_common_event
  494.     event = $data_common_events.find do |event|
  495.       event && event.autorun? && $game_switches[event.switch_id]
  496.     end
  497.     @interpreter.setup(event.list, 0, true) if event
  498.     event
  499.   end
  500.   #--------------------------------------------------------------------------
  501.   # * Get ID of Lowest Mouse-enabled Event at Designated Coordinates
  502.   #--------------------------------------------------------------------------
  503.   def lowest_mouse_event_xy(x, y)
  504.     list = events_xy(x, y) + events_xy(x, y+1)
  505.     list.sort! {|a, b| b.y - a.y}
  506.     evt = nil
  507.     list.each do |event|
  508.       if (event.pos?(x, y) || (event.pos?(x, y+1) && event.height > 32)) &&
  509.         (evt.nil? || event.y > evt.y)
  510.         evt = event
  511.         break
  512.       end
  513.     end
  514.     return evt
  515.   end
  516. end
  517.  
  518. class Scene_Map
  519.   #--------------------------------------------------------------------------
  520.   # * Frame Update
  521.   #--------------------------------------------------------------------------
  522.   alias shaz_mouse_scene_map_update update
  523.   def update
  524.     $mouse.update_event_cursors
  525.     shaz_mouse_scene_map_update
  526.   end
  527. end
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534. #============================================================================
  535. # SUPER SIMPLE MOUSE SCRIPT
  536. # Event
  537. #============================================================================
  538.  
  539.  
  540. module RPG
  541.   class Event
  542.     class Page
  543.       #--------------------------------------------------------------------
  544.       # * Public Instance Variables
  545.       #--------------------------------------------------------------------
  546.       attr_reader :mouse_icon
  547.       attr_reader :mouse_text
  548.       attr_reader :mouse_position
  549.       attr_reader :mouse_autoactivate
  550.       #--------------------------------------------------------------------
  551.       # * Build Stats
  552.       #--------------------------------------------------------------------
  553.       def build_stats
  554.         # Mouse icons (icon mandatory, others optional)
  555.         # <mouse icon destx desty name>
  556.         @mouse_icon = nil
  557.         @mouse_text = nil
  558.         @mouse_position = [0, 0]
  559.         @mouse_autoactivate = false
  560.         # look for mouse instructions
  561.         list.each do |command|
  562.           if [108, 408].include?(command.code)
  563.             comment = command.parameters[0]
  564.               case comment
  565.               when /<mouse/i
  566.                 params = /<mouse (.*)>/i.match(comment)[1].split(' ')
  567.                 @mouse_icon = params.shift
  568.                 if params.size > 1 && params[0] =~ /\d+/ && params[1] =~ /\d+/
  569.                   @mouse_position = [params.shift.to_i, params.shift.to_i]
  570.                 end
  571.                 if params.size > 0
  572.                   @mouse_text = params.join(' ')
  573.                 end
  574.               when /<autoactivate>/
  575.                 @mouse_autoactivate = true
  576.               end
  577.           end #if
  578.         end #do
  579.       end #def
  580.     end
  581.   end
  582. end
  583.  
  584. class Game_Event < Game_Character
  585.   #--------------------------------------------------------------------------
  586.   # * Public Instance Variables
  587.   #--------------------------------------------------------------------------
  588.   attr_reader   :mouse_icon
  589.   attr_reader   :mouse_text
  590.   attr_reader   :mouse_position
  591.   attr_reader   :mouse_autoactivate
  592.   #--------------------------------------------------------------------------
  593.   # * Start Event
  594.   #--------------------------------------------------------------------------
  595.   alias shaz_mouse_game_event_start start
  596.   def start
  597.     $game_player.start_event(@id) if !empty?
  598.     shaz_mouse_game_event_start
  599.   end
  600.   #--------------------------------------------------------------------------
  601.   # * Clear Event Page Settings
  602.   #--------------------------------------------------------------------------
  603.   alias shaz_mouse_game_event_clear_page_settings clear_page_settings
  604.   def clear_page_settings
  605.     shaz_mouse_game_event_clear_page_settings
  606.     @mouse_icon = nil
  607.     @mouse_text = nil
  608.     @mouse_position = [0, 0]
  609.     @mouse_autoactivate = false
  610.     @height = 0
  611.   end
  612.   #--------------------------------------------------------------------------
  613.   # * Set Up Event Page Settings
  614.   #--------------------------------------------------------------------------
  615.   alias shaz_mouse_game_event_setup_page_settings setup_page_settings
  616.   def setup_page_settings
  617.     shaz_mouse_game_event_setup_page_settings
  618.     @page.build_stats
  619.     @mouse_icon         = @page.mouse_icon
  620.     @mouse_text         = @page.mouse_text
  621.     @mouse_position     = @page.mouse_position
  622.     @mouse_autoactivate = @page.mouse_autoactivate
  623.     set_size
  624.   end
  625. end
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632. #============================================================================
  633. # SUPER SIMPLE MOUSE SCRIPT
  634. # Character
  635. #============================================================================
  636.  
  637.  
  638. class Game_CharacterBase
  639.   attr_reader :height                            # Height of character bitmap
  640.   #--------------------------------------------------------------------------
  641.   # * Initialize Public Member Variables
  642.   #--------------------------------------------------------------------------
  643.   alias shaz_mouse_game_characterbase_init_public_members init_public_members
  644.   def init_public_members
  645.     shaz_mouse_game_characterbase_init_public_members
  646.     @height = 0
  647.   end
  648.   #--------------------------------------------------------------------------
  649.   # * Change Graphics
  650.   #     character_name  : new character graphic filename
  651.   #     character_index : new character graphic index
  652.   #--------------------------------------------------------------------------
  653.   alias shaz_mouse_game_characterbase_set_graphic set_graphic
  654.   def set_graphic(character_name, character_index)
  655.     shaz_mouse_game_characterbase_set_graphic(character_name, character_index)
  656.     set_size
  657.   end
  658.   #--------------------------------------------------------------------------
  659.   # * Set character width/height size
  660.   #--------------------------------------------------------------------------
  661.   def set_size
  662.     bw = Cache.character(@character_name).width
  663.     bh = Cache.character(@character_name).height
  664.     sign = @character_name[/^[\!\$]./]
  665.     if sign && sign.include?('$')
  666.       @width = bw / 3
  667.       @height = bh / 4
  668.     else
  669.       @width = bw / 12
  670.       @height = bh / 8
  671.     end
  672.   end
  673.   #--------------------------------------------------------------------------
  674.   # * Detect Collision with Event
  675.   #--------------------------------------------------------------------------
  676.   def collide_with_events?(x, y)
  677.     $game_map.events_xy_nt(x, y).any? do |event|
  678.       self != event && (event.normal_priority? || self.is_a?(Game_Event))
  679.     end
  680.   end
  681.   #--------------------------------------------------------------------------
  682.   # * Detect Collision with Vehicle
  683.   #--------------------------------------------------------------------------
  684.   def collide_with_vehicles?(x, y)
  685.     !self.is_a?(Game_Player) && ($game_map.boat.pos_nt?(x, y) || $game_map.ship.pos_nt?(x, y))
  686.   end
  687.   #--------------------------------------------------------------------------
  688.   # * Frame Update
  689.   #--------------------------------------------------------------------------
  690.   alias shaz_mouse_game_characterbase_update update
  691.   def update
  692.     run_path if @runpath
  693.     shaz_mouse_game_characterbase_update
  694.   end
  695.   #--------------------------------------------------------------------------
  696.   # * Run Path
  697.   #--------------------------------------------------------------------------
  698.   def run_path
  699.     return if moving?
  700.     @step = @map.nil? || @map[@x, @y].nil? ? 0 : @map[@x, @y] - 1
  701.     if @step < 1
  702.       clear_path
  703.     else
  704.       x, y = @x, @y
  705.       dirs = []
  706.       dirs.push(6) if @map[@x+1, @y] == @step && passable?(@x, @y, 6)
  707.       dirs.push(2) if @map[@x, @y+1] == @step && passable?(@x, @y, 2)
  708.       dirs.push(4) if @map[@x-1, @y] == @step && passable?(@x, @y, 4)
  709.       dirs.push(8) if @map[@x, @y-1] == @step && passable?(@x, @y, 8)
  710.       while dirs.size > 0
  711.         dir = dirs.delete_at(rand(dirs.size))
  712.         move_straight(dir)
  713.         break if x != @x || y != @y
  714.       end
  715.       # clear the path if we couldn't move
  716.       clear_path if x == @x && y == @y
  717.     end
  718.   end
  719.   #--------------------------------------------------------------------------
  720.   # * Find Path
  721.   #--------------------------------------------------------------------------
  722.   def find_path(x, y)
  723.     sx, sy = @x, @y
  724.     @tx, @ty = x, y
  725.     result = setup_map(sx, sy)
  726.     @runpath = result[0]
  727.     @map = result[1]
  728.     @map[sx, sy] = result[2] if result[2] != nil
  729.   end
  730.   #--------------------------------------------------------------------------
  731.   # * Clear Path
  732.   #--------------------------------------------------------------------------
  733.   def clear_path
  734.     @map = nil
  735.     @runpath = false
  736.   end
  737.   #--------------------------------------------------------------------------
  738.   # * Setup Map
  739.   #--------------------------------------------------------------------------
  740.   def setup_map(sx, sy)
  741.     map = Table.new($game_map.width, $game_map.height)
  742.     update_counter = 0
  743.     map[@tx, @ty] = 1
  744.     old_positions = [[@tx, @ty]]
  745.     new_positions = []
  746.    
  747.     # if tile is impassable, but CAN move to adjacent tiles, use the adjacent tiles instead
  748.     if (!passable?(@tx, @ty, 2) && !passable?(@tx, @ty, 4) &&
  749.       !passable?(@tx, @ty, 6) && !passable?(@tx, @ty, 8)) ||
  750.       $game_map.events_xy_nt(@tx, @ty).any? { |evt| evt.normal_priority? && evt != self }
  751.       old_positions = []
  752.      
  753.       # Can we move from the destination tile in any direction?
  754.       if map_passable?(@tx, @ty, 2)
  755.         map[@tx, @ty+1] = 1
  756.         old_positions.push([@tx, @ty+1])
  757.       end
  758.       if map_passable?(@tx, @ty, 8)
  759.         map[@tx, @ty-1] = 1
  760.         old_positions.push([@tx, @ty-1])
  761.       end
  762.       if map_passable?(@tx, @ty, 4)
  763.         map[@tx-1, @ty] = 1
  764.         old_positions.push([@tx-1, @ty])
  765.       end
  766.       if map_passable?(@tx, @ty, 6)
  767.         map[@tx+1, @ty] = 1
  768.         old_positions.push([@tx+1, @ty])
  769.       end
  770.      
  771.       # If not, can we at least move up to the destination tile?
  772.       if old_positions.size == 0
  773.         if map_passable?(@tx-1,@ty,6)
  774.           map[@tx-1,@ty] = 1
  775.           old_positions.push([@tx-1,@ty])
  776.         end
  777.         if map_passable?(@tx+1,@ty,4)
  778.           map[@tx+1,@ty] = 1
  779.           old_positions.push([@tx+1,@ty])
  780.         end
  781.         if map_passable?(@tx,@ty-1,2)
  782.           map[@tx,@ty-1] = 1
  783.           old_positions.push([@tx,@ty-1])
  784.         end
  785.         if map_passable?(@tx,@ty+1,8)
  786.           map[@tx,@ty+1] = 1
  787.           old_positions.push([@tx,@ty+1])
  788.         end
  789.       end
  790.     end
  791.    
  792.     # If there are any counters, can we move to the tile on the other side?
  793.     if map_passable?(@tx-2,@ty,6) && $game_map.counter?(@tx-1,@ty)
  794.       map[@tx-2,@ty] = 1
  795.       old_positions.push([@tx-2,@ty])
  796.     end
  797.     if map_passable?(@tx+2,@ty,4) && $game_map.counter?(@tx+1,@ty)
  798.       map[@tx+2,@ty] = 1
  799.       old_positions.push([@tx+2,@ty])
  800.     end
  801.     if map_passable?(@tx,@ty-2,2) && $game_map.counter?(@tx,@ty-1)
  802.       map[@tx,@ty-2] = 1
  803.       old_positions.push([@tx,@ty-2])
  804.     end
  805.     if map_passable?(@tx,@ty+2,2) && $game_map.counter?(@tx,@ty+1)
  806.       map[@tx,@ty+2] = 1
  807.       old_positions.push([@tx,@ty+2])
  808.     end
  809.    
  810.    
  811.     depth = 2
  812.     depth.upto(100) { |step|
  813.       break if old_positions[0].nil?
  814.       @step = step
  815.       loop do
  816.         break if old_positions[0].nil?
  817.         x, y = old_positions.shift
  818.         return [true, map, @step-1] if x == sx && y == sy
  819.         if map[x, y + 1] == 0 && passable?(x, y, 2)
  820.           map[x, y + 1] = @step
  821.           new_positions.push([x, y + 1])
  822.         end
  823.         if map[x - 1, y] == 0 && passable?(x, y, 4)
  824.           map[x - 1, y] = @step
  825.           new_positions.push([x - 1, y])
  826.         end
  827.         if map[x + 1, y] == 0 && passable?(x, y, 6)
  828.           map[x + 1, y] = @step
  829.           new_positions.push([x + 1, y])
  830.         end
  831.         if map[x, y - 1] == 0 && passable?(x, y, 8)
  832.           map[x, y - 1] = @step
  833.           new_positions.push([x, y - 1])
  834.         end
  835.         # Update graphics? (to reduce lag)
  836.         update_counter += 1
  837.         if update_counter > 50
  838.           Graphics.update
  839.           update_counter = 0
  840.         end
  841.       end
  842.       old_positions = new_positions
  843.       new_positions = []
  844.     }
  845.     return [false, nil, nil]
  846.   end
  847. end  
  848.  
  849. class Game_Character < Game_CharacterBase
  850.   #--------------------------------------------------------------------------
  851.   # * Force Move Route
  852.   #--------------------------------------------------------------------------
  853.   alias shaz_mouse_game_character_force_move_route force_move_route
  854.   def force_move_route(move_route)
  855.     clear_path
  856.     shaz_mouse_game_character_force_move_route(move_route)
  857.   end
  858. end
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865. #============================================================================
  866. # SUPER SIMPLE MOUSE SCRIPT
  867. # Player
  868. #============================================================================
  869.  
  870.  
  871. class Game_Player < Game_Character
  872.   #--------------------------------------------------------------------------
  873.   # * Trigger Map Event
  874.   #     triggers : Trigger array
  875.   #     normal   : Is priority set to [Same as Characters] ?
  876.   #--------------------------------------------------------------------------
  877.   alias shaz_mouse_game_player_start_map_event start_map_event
  878.   def start_map_event(x, y, triggers, normal)
  879.     @started_events = []
  880.     shaz_mouse_game_player_start_map_event(x, y, triggers, normal)
  881.   end
  882.   #--------------------------------------------------------------------------
  883.   # * Start Event
  884.   #--------------------------------------------------------------------------
  885.   def start_event(event_id)
  886.     @started_events = [] if @started_events.nil?
  887.     @started_events.push(event_id)
  888.   end
  889.   #--------------------------------------------------------------------------
  890.   # * Processing of Movement via Input from Directional Buttons
  891.   #--------------------------------------------------------------------------
  892.   alias shaz_mouse_game_player_move_by_input move_by_input
  893.   def move_by_input
  894.     if Input.dir4 > 0
  895.       clear_path
  896.       shaz_mouse_game_player_move_by_input
  897.     else
  898.       # Move by mouse input
  899.       if !$game_message.busy? && !$game_message.visible && !@move_route_forcing &&
  900.         !@vehicle_getting_on && !@vehicle_getting_off &&
  901.         Mouse.trigger?(0) && !Mouse.grid.nil? && !$mouse.ignored?
  902.         mx, my = *Mouse.grid
  903.         # turn in direction
  904.         if (@x - mx).abs >= (@y - my).abs
  905.           set_direction(@x > mx ? 4 : 6)
  906.         else
  907.           set_direction(@y > my ? 8 : 2)
  908.         end
  909.         # find path
  910.         @event = $game_map.lowest_mouse_event_xy(mx, my)
  911.         if @event.nil?
  912.           find_path(mx, my)
  913.         elsif @event.mouse_autoactivate
  914.           @event.start
  915.           @started_events = []
  916.           clear_path
  917.         else
  918.           find_path(@event.x + @event.mouse_position[0],
  919.             @event.y + @event.mouse_position[1])
  920.         end
  921.       end
  922.     end
  923.   end
  924.   #--------------------------------------------------------------------------
  925.   # * Frame Update
  926.   #--------------------------------------------------------------------------
  927.   alias shaz_mouse_game_player_update update
  928.   def update
  929.     shaz_mouse_game_player_update
  930.     update_pathfinding if !@event.nil? && !moving?
  931.   end
  932.   #--------------------------------------------------------------------------
  933.   # * Check event after pathfinding
  934.   #--------------------------------------------------------------------------
  935.   def update_pathfinding
  936.     if @map.nil? || @map[@x, @y] <= 1
  937.       dir = @x < @event.x ? 6 : @x > @event.x ? 4 : @y < @event.y ? 2 : @y > @event.y ? 8 : 0
  938.       # Face event and trigger it (only if not triggered by start_map_event)
  939.       turn_toward_character(@event) if !@event.pos?(@x, @y)
  940.       if !@started_events.include?(@event.id) && !@map.nil? && !in_airship?
  941.         @event.start
  942.         @started_events = []
  943.       end
  944.       clear_path
  945.     end
  946.   end
  947.   #--------------------------------------------------------------------------
  948.   # * Clear Path
  949.   #--------------------------------------------------------------------------
  950.   def clear_path
  951.     @event = nil
  952.     super
  953.   end
  954. end
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961. #============================================================================
  962. # SUPER SIMPLE MOUSE SCRIPT
  963. # Interpreter
  964. #============================================================================
  965.  
  966.  
  967. class Game_Interpreter
  968.   #--------------------------------------------------------------------------
  969.   # * Event Setup
  970.   #--------------------------------------------------------------------------
  971.   alias shaz_mouse_game_interpreter_setup setup
  972.   def setup(list, event_id = 0, lock_player = false)
  973.     shaz_mouse_game_interpreter_setup(list, event_id)
  974.     @lock_player = lock_player
  975.   end
  976.   #--------------------------------------------------------------------------
  977.   # * Execute
  978.   #--------------------------------------------------------------------------
  979.   alias shaz_mouse_game_interpreter_run run
  980.   def run
  981.     $mouse.ignored = true if @lock_player
  982.     shaz_mouse_game_interpreter_run
  983.     $mouse.ignored = false if @lock_player
  984.   end
  985. end
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992. #============================================================================
  993. # SUPER SIMPLE MOUSE SCRIPT
  994. # Windows
  995. #============================================================================
  996.  
  997.  
  998. class Window_Selectable < Window_Base
  999.   #--------------------------------------------------------------------------
  1000.   # * Frame Update
  1001.   #--------------------------------------------------------------------------
  1002.   alias shaz_mouse_window_selectable_update update
  1003.   def update
  1004.     shaz_mouse_window_selectable_update
  1005.     process_mouse_handling if Input.method == :mouse
  1006.   end
  1007.   #--------------------------------------------------------------------------
  1008.   # * Mouse Movement Processing
  1009.   #--------------------------------------------------------------------------
  1010.   def process_mouse_handling
  1011.     return unless $mouse.enabled? && cursor_movable?
  1012.     # Add a delay to prevent too-fast scrolling
  1013.     @delay = @delay ? @delay + 1 : 0
  1014.     return if @delay % 3 > 0
  1015.    
  1016.     mx, my = *Mouse.position
  1017.     vx = self.viewport ? self.x - self.viewport.ox + self.viewport.rect.x : self.x
  1018.     vy = self.viewport ? self.y - self.viewport.oy + self.viewport.rect.y : self.y
  1019.     if mx.between?(vx, vx + self.width) &&
  1020.       my.between?(vy, vy + self.height)
  1021.       mx -= vx
  1022.       mx -= padding
  1023.       my -= vy
  1024.       my -= padding
  1025.       my += oy
  1026.       for i in 0 ... item_max
  1027.         rect = item_rect(i)
  1028.         if mx.between?(rect.x, rect.x + rect.width) &&
  1029.           my.between?(rect.y, rect.y + rect.height)
  1030.           last_index = @index
  1031.           select(i)
  1032.           if @index != last_index
  1033.             Sound.play_cursor
  1034.           end
  1035.           break
  1036.         end
  1037.       end
  1038.     end
  1039.   end
  1040. end  
  1041.  
  1042. class Window_NameInput < Window_Selectable
  1043.   #--------------------------------------------------------------------------
  1044.   # * Mouse Movement Processing
  1045.   #--------------------------------------------------------------------------
  1046.   def process_mouse_handling
  1047.     return unless $mouse.enabled?
  1048.     # Add a delay to prevent too-fast scrolling
  1049.     @delay = @delay ? @delay + 1 : 0
  1050.     return if @delay % 3 > 0
  1051.    
  1052.     mx, my = *Mouse.position
  1053.     vx = (self.viewport ? self.x - self.viewport.ox + self.viewport.rect.x : self.x) + padding
  1054.     vy = (self.viewport ? self.y - self.viewport.oy + self.viewport.rect.y : self.y) + padding
  1055.     if mx.between?(vx, vx + self.width - padding * 2) &&
  1056.       my.between?(vy, vy + self.height - padding * 2)
  1057.       mx -= vx
  1058.       my -= vy
  1059.       x = (mx > 5*32+16 ? mx-16 : mx) / 32
  1060.       y = my / line_height
  1061.       last_index = @index
  1062.       @index = y * 10 + x
  1063.       Sound.play_cursor if @index != last_index
  1064.     end
  1065.   end
  1066. end
  1067.  
  1068. class Scene_File < Scene_MenuBase
  1069.   #--------------------------------------------------------------------------
  1070.   # * Update Cursor
  1071.   #--------------------------------------------------------------------------
  1072.   alias shaz_mouse_scene_file_update_cursor update_cursor
  1073.   def update_cursor
  1074.     shaz_mouse_scene_file_update_cursor
  1075.     process_mouse_handling if Input.method == :mouse
  1076.   end
  1077.   #--------------------------------------------------------------------------
  1078.   # * Mouse Movement Processing
  1079.   #--------------------------------------------------------------------------
  1080.   def process_mouse_handling
  1081.     return unless $mouse.enabled?
  1082.     # Add a delay to prevent too-fast scrolling
  1083.     @delay = @delay ? @delay + 1 : 0
  1084.     return if @delay % 3 > 0
  1085.    
  1086.     mx, my = *Mouse.position
  1087.     vx = @savefile_viewport.ox + mx
  1088.     vy = @savefile_viewport.oy + my
  1089.     last_index = @index
  1090.     new_index = vy / savefile_height
  1091.     if @index != new_index
  1092.       if new_index > @index
  1093.         cursor_down(false)
  1094.       else
  1095.         cursor_up(false)
  1096.       end
  1097.       Sound.play_cursor
  1098.       @savefile_windows[last_index].selected = false
  1099.       @savefile_windows[@index].selected = true
  1100.     end
  1101.   end
  1102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement