TheoAllen

Amaranth's Mouse Script (edited)

Jan 24th, 2015
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 40.05 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. #============================================================================
  231. # SUPER SIMPLE MOUSE SCRIPT
  232. # Mouse Module
  233. #============================================================================
  234.  
  235.  
  236. #==============================================================================
  237. # ** Mouse Module
  238. #------------------------------------------------------------------------------
  239. #  by Near Fantastica and SephirothSpawn
  240. #  adapted and converted to VX Ace by Shaz
  241. #==============================================================================
  242. module Mouse
  243.   #--------------------------------------------------------------------------
  244.   # * Mouse to Input Triggers
  245.   #   key => Input::KeyCONSTANT (key: 0 - left, 1 - middle, 2 - right)
  246.   #--------------------------------------------------------------------------
  247.   Mouse_to_Input_Triggers = {0 => Input::C, 1 => Input::B, 2 => Input::A}
  248.   #--------------------------------------------------------------------------
  249.   # * API Declarations
  250.   #--------------------------------------------------------------------------
  251.   GAKS = Win32API.new('user32', 'GetAsyncKeyState', 'i', 'i')
  252.   GSM = Win32API.new('user32', 'GetSystemMetrics', 'i', 'i')
  253.   Cursor_Pos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  254.   Scr2cli = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
  255.   Client_rect = Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
  256.   Findwindow = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')
  257.   Readini = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
  258.   ShowCursor = Win32API.new('user32', 'ShowCursor', 'i', 'l')
  259.   #--------------------------------------------------------------------------
  260.   # * Module Variables
  261.   #--------------------------------------------------------------------------
  262.   @triggers = [[0, 1], [0, 2], [0, 4]]
  263.   @old_pos = 0
  264.   @pos_i = 0
  265.   @sys_cursor_visible = false
  266.   #--------------------------------------------------------------------------
  267.   # * Mouse Grid Position
  268.   #--------------------------------------------------------------------------
  269.   def self.grid
  270.     return nil if @pos.nil?
  271.     mx, my = SceneManager.scene.instance_variable_get(:@spriteset).tilemap_offset
  272.     x = (@pos[0] + mx) / 32
  273.     y = (@pos[1] + my) / 32
  274.     return [x, y]
  275.   end
  276.   #--------------------------------------------------------------------------
  277.   # * Mouse Position
  278.   #--------------------------------------------------------------------------
  279.   def self.position
  280.     return @pos.nil? ? [0, 0] : @pos
  281.   end
  282.   #--------------------------------------------------------------------------
  283.   # * Mouse Global Position
  284.   #--------------------------------------------------------------------------
  285.   def self.global_pos
  286.     pos = [0, 0].pack('ll')
  287.     return Cursor_Pos.call(pos) == 0 ? nil : pos.unpack('ll')
  288.   end
  289.   #--------------------------------------------------------------------------
  290.   # * Screen to Client
  291.   #--------------------------------------------------------------------------
  292.   def self.screen_to_client(x=0, y=0)
  293.     pos = [x, y].pack('ll')
  294.     return Scr2cli.call(self.hwnd, pos) == 0 ? nil : pos.unpack('ll')
  295.   end
  296.   #--------------------------------------------------------------------------
  297.   # * Mouse Position
  298.   #--------------------------------------------------------------------------
  299.   def self.pos
  300.     gx, gy = global_pos
  301.     x, y = screen_to_client(gx, gy)
  302.    
  303.     # Test boundaries
  304.     begin
  305.       if (x >= 0 && y >= 0 && x <= Graphics.width && y <= Graphics.height)
  306.         return x, y
  307.       else
  308.         return -20, -20
  309.       end
  310.     rescue
  311.       return 0, 0
  312.     end
  313.   end
  314.   #--------------------------------------------------------------------------
  315.   # * Update Mouse Position
  316.   #--------------------------------------------------------------------------
  317.   def self.update
  318.     old_pos = @pos
  319.     @pos = self.pos
  320.    
  321.     # Has mouse been moved?
  322.     if old_pos != @pos
  323.       Input.method = :mouse
  324.     end
  325.    
  326.     # Which mouse to show - custom, or system?
  327.     if Mouse.get_mouse.enabled? == @sys_cursor_visible
  328.       @sys_cursor_visible = !@sys_cursor_visible
  329.       ShowCursor.call(@sys_cursor_visible ? 1 : 0)
  330.     end
  331.    
  332.     return if !Mouse.get_mouse.enabled?
  333.    
  334.     # Leaving / Entering Range?
  335.     if old_pos != [-20, -20] && @pos == [-20, -20] # leaving range
  336.       ShowCursor.call(1)
  337.     elsif old_pos == [-20, -20] && @pos != [-20, -20] # entering range
  338.       ShowCursor.call(0)
  339.     end
  340.    
  341.     # Update Triggers
  342.     for i in @triggers
  343.       n = GAKS.call(i[1])
  344.       if [0, 1].include?(n)
  345.         i[0] = (i[0] > 0 ? i[0] * -1 : 0)
  346.       else
  347.         i[0] = (i[0] > 0 ? i[0] + 1 : 1)
  348.       end
  349.     end
  350.   end
  351.   #--------------------------------------------------------------------------
  352.   # * Trigger?
  353.   #   id : 0:Left, 1:Right, 2:Center
  354.   #--------------------------------------------------------------------------
  355.   def self.trigger?(id = 0)
  356.     if pos != [-20, -20]
  357.       return @triggers[id][0] == 1
  358.     end
  359.     return false
  360.   end
  361.   #--------------------------------------------------------------------------
  362.   # * Repeat?
  363.   #   id : 0:Left, 1:Right, 2:Center
  364.   #--------------------------------------------------------------------------
  365.   def self.repeat?(id = 0)
  366.     return @triggers[id][0] > 0 && @triggers[id][0] % 5 == 1
  367.   end
  368.   #--------------------------------------------------------------------------
  369.   # * Hwnd
  370.   #--------------------------------------------------------------------------
  371.   def self.hwnd
  372.     if @hwnd.nil?
  373.       title = "\0" * 256
  374.       Readini.call('Game', 'Title', '', title, 255, '.\\Game.ini')
  375.       title.delete!("\0")
  376.       @hwnd = Findwindow.call('RGSS Player', title)
  377.       ShowCursor.call(0)
  378.     end
  379.     return @hwnd
  380.   end
  381.   #--------------------------------------------------------------------------
  382.   # * Client Size
  383.   #--------------------------------------------------------------------------
  384.   def self.client_size
  385.     rect = [0, 0, 0, 0].pack('l4')
  386.     Client_rect.call(self.hwnd, rect)
  387.     return rect.unpack('l4')[2..3]
  388.   end
  389. end
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396. #============================================================================
  397. # SUPER SIMPLE MOUSE SCRIPT
  398. # Input
  399. #============================================================================
  400.  
  401.  
  402. class << Input
  403.   #--------------------------------------------------------------------------
  404.   # * Public Instance Variables
  405.   #--------------------------------------------------------------------------
  406.   attr_accessor :method
  407.   #--------------------------------------------------------------------------
  408.   # * Alias Listings
  409.   #--------------------------------------------------------------------------
  410.   alias :seph_mouse_input_update :update
  411.   alias :seph_mouse_input_trigger? :trigger?
  412.   alias :seph_mouse_input_repeat? :repeat?
  413.   #--------------------------------------------------------------------------
  414.   # * Frame Update
  415.   #--------------------------------------------------------------------------
  416.   def update
  417.     Mouse.get_mouse.update
  418.     Mouse.update
  419.     seph_mouse_input_update
  420.     # Are we using the mouse or the keyboard?
  421.     @method = :keyboard if dir4 != 0 || dir8 != 0
  422.   end
  423.   #--------------------------------------------------------------------------
  424.   # * Trigger? Test
  425.   #--------------------------------------------------------------------------
  426.   def trigger?(constant)
  427.     return true if seph_mouse_input_trigger?(constant)
  428.     if Mouse.get_mouse.enabled? && !Mouse.pos.nil?
  429.       if Mouse::Mouse_to_Input_Triggers.has_value?(constant)
  430.         return true if Mouse.trigger?(Mouse::Mouse_to_Input_Triggers.index(constant))
  431.       end
  432.     end
  433.     return false
  434.   end
  435.   #--------------------------------------------------------------------------
  436.   # * Repeat? Test
  437.   #--------------------------------------------------------------------------
  438.   def repeat?(constant)
  439.     return true if seph_mouse_input_repeat?(constant)
  440.     if Mouse.get_mouse.enabled? && !Mouse.pos.nil?
  441.       if Mouse::Mouse_to_Input_Triggers.has_value?(constant)
  442.         return true if Mouse.repeat?(Mouse::Mouse_to_Input_Triggers.index(constant))
  443.       end
  444.     end
  445.     return false
  446.   end
  447. end
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. #============================================================================
  455. # SUPER SIMPLE MOUSE SCRIPT
  456. # Map
  457. #============================================================================
  458.  
  459.  
  460. class Spriteset_Map
  461.   #--------------------------------------------------------------------------
  462.   # * Tilemap Offset
  463.   #--------------------------------------------------------------------------
  464.   def tilemap_offset
  465.     if $imported && $imported["YEA-CoreEngine"]
  466.       [@tilemap.ox - @viewport1.rect.x, @tilemap.oy - @viewport1.rect.y]
  467.     else
  468.       [@tilemap.ox, @tilemap.oy]
  469.     end
  470.   end
  471. end
  472.  
  473. class Game_Map
  474.   #--------------------------------------------------------------------------
  475.   # * Detect/Set Up Starting Map Event
  476.   #--------------------------------------------------------------------------
  477.   def setup_starting_map_event
  478.     event = @events.values.find {|event| event.starting }
  479.     event.clear_starting_flag if event
  480.     @interpreter.setup(event.list, event.id, event.trigger_in?([0,1,2,3])) if event
  481.     event
  482.   end
  483.   #--------------------------------------------------------------------------
  484.   # * Detect/Set Up Autorun Common Event
  485.   #--------------------------------------------------------------------------
  486.   def setup_autorun_common_event
  487.     event = $data_common_events.find do |event|
  488.       event && event.autorun? && $game_switches[event.switch_id]
  489.     end
  490.     @interpreter.setup(event.list, 0, true) if event
  491.     event
  492.   end
  493.   #--------------------------------------------------------------------------
  494.   # * Get ID of Lowest Mouse-enabled Event at Designated Coordinates
  495.   #--------------------------------------------------------------------------
  496.   def lowest_mouse_event_xy(x, y)
  497.     list = events_xy(x, y) + events_xy(x, y+1)
  498.     list.sort! {|a, b| b.y - a.y}
  499.     evt = nil
  500.     list.each do |event|
  501.       if (event.pos?(x, y) || (event.pos?(x, y+1) && event.height > 32)) &&
  502.         (evt.nil? || event.y > evt.y)
  503.         evt = event
  504.         break
  505.       end
  506.     end
  507.     return evt
  508.   end
  509. end
  510.  
  511. class Scene_Map
  512.   #--------------------------------------------------------------------------
  513.   # * Frame Update
  514.   #--------------------------------------------------------------------------
  515.   alias shaz_mouse_scene_map_update update
  516.   def update
  517.     Mouse.get_mouse.update_event_cursors
  518.     shaz_mouse_scene_map_update
  519.   end
  520. end
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527. #============================================================================
  528. # SUPER SIMPLE MOUSE SCRIPT
  529. # Event
  530. #============================================================================
  531.  
  532.  
  533. module RPG
  534.   class Event
  535.     class Page
  536.       #--------------------------------------------------------------------
  537.       # * Public Instance Variables
  538.       #--------------------------------------------------------------------
  539.       attr_reader :mouse_icon
  540.       attr_reader :mouse_text
  541.       attr_reader :mouse_position
  542.       attr_reader :mouse_autoactivate
  543.       #--------------------------------------------------------------------
  544.       # * Build Stats
  545.       #--------------------------------------------------------------------
  546.       def build_stats
  547.         # Mouse icons (icon mandatory, others optional)
  548.         # <mouse icon destx desty name>
  549.         @mouse_icon = nil
  550.         @mouse_text = nil
  551.         @mouse_position = [0, 0]
  552.         @mouse_autoactivate = false
  553.         # look for mouse instructions
  554.         list.each do |command|
  555.           if [108, 408].include?(command.code)
  556.             comment = command.parameters[0]
  557.               case comment
  558.               when /<mouse/i
  559.                 params = /<mouse (.*)>/i.match(comment)[1].split(' ')
  560.                 @mouse_icon = params.shift
  561.                 if params.size > 1 && params[0] =~ /\d+/ && params[1] =~ /\d+/
  562.                   @mouse_position = [params.shift.to_i, params.shift.to_i]
  563.                 end
  564.                 if params.size > 0
  565.                   @mouse_text = params.join(' ')
  566.                 end
  567.               when /<autoactivate>/
  568.                 @mouse_autoactivate = true
  569.               end
  570.           end #if
  571.         end #do
  572.       end #def
  573.     end
  574.   end
  575. end
  576.  
  577. class Game_Event < Game_Character
  578.   #--------------------------------------------------------------------------
  579.   # * Public Instance Variables
  580.   #--------------------------------------------------------------------------
  581.   attr_reader   :mouse_icon
  582.   attr_reader   :mouse_text
  583.   attr_reader   :mouse_position
  584.   attr_reader   :mouse_autoactivate
  585.   #--------------------------------------------------------------------------
  586.   # * Start Event
  587.   #--------------------------------------------------------------------------
  588.   alias shaz_mouse_game_event_start start
  589.   def start
  590.     $game_player.start_event(@id) if !empty?
  591.     shaz_mouse_game_event_start
  592.   end
  593.   #--------------------------------------------------------------------------
  594.   # * Clear Event Page Settings
  595.   #--------------------------------------------------------------------------
  596.   alias shaz_mouse_game_event_clear_page_settings clear_page_settings
  597.   def clear_page_settings
  598.     shaz_mouse_game_event_clear_page_settings
  599.     @mouse_icon = nil
  600.     @mouse_text = nil
  601.     @mouse_position = [0, 0]
  602.     @mouse_autoactivate = false
  603.     @height = 0
  604.   end
  605.   #--------------------------------------------------------------------------
  606.   # * Set Up Event Page Settings
  607.   #--------------------------------------------------------------------------
  608.   alias shaz_mouse_game_event_setup_page_settings setup_page_settings
  609.   def setup_page_settings
  610.     shaz_mouse_game_event_setup_page_settings
  611.     @page.build_stats
  612.     @mouse_icon         = @page.mouse_icon
  613.     @mouse_text         = @page.mouse_text
  614.     @mouse_position     = @page.mouse_position
  615.     @mouse_autoactivate = @page.mouse_autoactivate
  616.     set_size
  617.   end
  618. end
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625. #============================================================================
  626. # SUPER SIMPLE MOUSE SCRIPT
  627. # Character
  628. #============================================================================
  629.  
  630.  
  631. class Game_CharacterBase
  632.   attr_reader :height                            # Height of character bitmap
  633.   #--------------------------------------------------------------------------
  634.   # * Initialize Public Member Variables
  635.   #--------------------------------------------------------------------------
  636.   alias shaz_mouse_game_characterbase_init_public_members init_public_members
  637.   def init_public_members
  638.     shaz_mouse_game_characterbase_init_public_members
  639.     @height = 0
  640.   end
  641.   #--------------------------------------------------------------------------
  642.   # * Change Graphics
  643.   #     character_name  : new character graphic filename
  644.   #     character_index : new character graphic index
  645.   #--------------------------------------------------------------------------
  646.   alias shaz_mouse_game_characterbase_set_graphic set_graphic
  647.   def set_graphic(character_name, character_index)
  648.     shaz_mouse_game_characterbase_set_graphic(character_name, character_index)
  649.     set_size
  650.   end
  651.   #--------------------------------------------------------------------------
  652.   # * Set character width/height size
  653.   #--------------------------------------------------------------------------
  654.   def set_size
  655.     bw = Cache.character(@character_name).width
  656.     bh = Cache.character(@character_name).height
  657.     sign = @character_name[/^[\!\$]./]
  658.     if sign && sign.include?('$')
  659.       @width = bw / 3
  660.       @height = bh / 4
  661.     else
  662.       @width = bw / 12
  663.       @height = bh / 8
  664.     end
  665.   end
  666.   #--------------------------------------------------------------------------
  667.   # * Detect Collision with Event
  668.   #--------------------------------------------------------------------------
  669.   def collide_with_events?(x, y)
  670.     $game_map.events_xy_nt(x, y).any? do |event|
  671.       self != event && (event.normal_priority? || self.is_a?(Game_Event))
  672.     end
  673.   end
  674.   #--------------------------------------------------------------------------
  675.   # * Detect Collision with Vehicle
  676.   #--------------------------------------------------------------------------
  677.   def collide_with_vehicles?(x, y)
  678.     !self.is_a?(Game_Player) && ($game_map.boat.pos_nt?(x, y) || $game_map.ship.pos_nt?(x, y))
  679.   end
  680.   #--------------------------------------------------------------------------
  681.   # * Frame Update
  682.   #--------------------------------------------------------------------------
  683.   alias shaz_mouse_game_characterbase_update update
  684.   def update
  685.     run_path if @runpath
  686.     shaz_mouse_game_characterbase_update
  687.   end
  688.   #--------------------------------------------------------------------------
  689.   # * Run Path
  690.   #--------------------------------------------------------------------------
  691.   def run_path
  692.     return if moving?
  693.     @step = @map.nil? || @map[@x, @y].nil? ? 0 : @map[@x, @y] - 1
  694.     if @step < 1
  695.       clear_path
  696.     else
  697.       x, y = @x, @y
  698.       dirs = []
  699.       dirs.push(6) if @map[@x+1, @y] == @step && passable?(@x, @y, 6)
  700.       dirs.push(2) if @map[@x, @y+1] == @step && passable?(@x, @y, 2)
  701.       dirs.push(4) if @map[@x-1, @y] == @step && passable?(@x, @y, 4)
  702.       dirs.push(8) if @map[@x, @y-1] == @step && passable?(@x, @y, 8)
  703.       while dirs.size > 0
  704.         dir = dirs.delete_at(rand(dirs.size))
  705.         move_straight(dir)
  706.         break if x != @x || y != @y
  707.       end
  708.       # clear the path if we couldn't move
  709.       clear_path if x == @x && y == @y
  710.     end
  711.   end
  712.   #--------------------------------------------------------------------------
  713.   # * Find Path
  714.   #--------------------------------------------------------------------------
  715.   def find_path(x, y)
  716.     sx, sy = @x, @y
  717.     @tx, @ty = x, y
  718.     result = setup_map(sx, sy)
  719.     @runpath = result[0]
  720.     @map = result[1]
  721.     @map[sx, sy] = result[2] if result[2] != nil
  722.   end
  723.   #--------------------------------------------------------------------------
  724.   # * Clear Path
  725.   #--------------------------------------------------------------------------
  726.   def clear_path
  727.     @map = nil
  728.     @runpath = false
  729.   end
  730.   #--------------------------------------------------------------------------
  731.   # * Setup Map
  732.   #--------------------------------------------------------------------------
  733.   def setup_map(sx, sy)
  734.     map = Table.new($game_map.width, $game_map.height)
  735.     update_counter = 0
  736.     map[@tx, @ty] = 1
  737.     old_positions = [[@tx, @ty]]
  738.     new_positions = []
  739.    
  740.     # if tile is impassable, but CAN move to adjacent tiles, use the adjacent tiles instead
  741.     if (!passable?(@tx, @ty, 2) && !passable?(@tx, @ty, 4) &&
  742.       !passable?(@tx, @ty, 6) && !passable?(@tx, @ty, 8)) ||
  743.       $game_map.events_xy_nt(@tx, @ty).any? { |evt| evt.normal_priority? && evt != self }
  744.       old_positions = []
  745.      
  746.       # Can we move from the destination tile in any direction?
  747.       if map_passable?(@tx, @ty, 2)
  748.         map[@tx, @ty+1] = 1
  749.         old_positions.push([@tx, @ty+1])
  750.       end
  751.       if map_passable?(@tx, @ty, 8)
  752.         map[@tx, @ty-1] = 1
  753.         old_positions.push([@tx, @ty-1])
  754.       end
  755.       if map_passable?(@tx, @ty, 4)
  756.         map[@tx-1, @ty] = 1
  757.         old_positions.push([@tx-1, @ty])
  758.       end
  759.       if map_passable?(@tx, @ty, 6)
  760.         map[@tx+1, @ty] = 1
  761.         old_positions.push([@tx+1, @ty])
  762.       end
  763.      
  764.       # If not, can we at least move up to the destination tile?
  765.       if old_positions.size == 0
  766.         if map_passable?(@tx-1,@ty,6)
  767.           map[@tx-1,@ty] = 1
  768.           old_positions.push([@tx-1,@ty])
  769.         end
  770.         if map_passable?(@tx+1,@ty,4)
  771.           map[@tx+1,@ty] = 1
  772.           old_positions.push([@tx+1,@ty])
  773.         end
  774.         if map_passable?(@tx,@ty-1,2)
  775.           map[@tx,@ty-1] = 1
  776.           old_positions.push([@tx,@ty-1])
  777.         end
  778.         if map_passable?(@tx,@ty+1,8)
  779.           map[@tx,@ty+1] = 1
  780.           old_positions.push([@tx,@ty+1])
  781.         end
  782.       end
  783.     end
  784.    
  785.     # If there are any counters, can we move to the tile on the other side?
  786.     if map_passable?(@tx-2,@ty,6) && $game_map.counter?(@tx-1,@ty)
  787.       map[@tx-2,@ty] = 1
  788.       old_positions.push([@tx-2,@ty])
  789.     end
  790.     if map_passable?(@tx+2,@ty,4) && $game_map.counter?(@tx+1,@ty)
  791.       map[@tx+2,@ty] = 1
  792.       old_positions.push([@tx+2,@ty])
  793.     end
  794.     if map_passable?(@tx,@ty-2,2) && $game_map.counter?(@tx,@ty-1)
  795.       map[@tx,@ty-2] = 1
  796.       old_positions.push([@tx,@ty-2])
  797.     end
  798.     if map_passable?(@tx,@ty+2,2) && $game_map.counter?(@tx,@ty+1)
  799.       map[@tx,@ty+2] = 1
  800.       old_positions.push([@tx,@ty+2])
  801.     end
  802.    
  803.    
  804.     depth = 2
  805.     depth.upto(100) { |step|
  806.       break if old_positions[0].nil?
  807.       @step = step
  808.       loop do
  809.         break if old_positions[0].nil?
  810.         x, y = old_positions.shift
  811.         return [true, map, @step-1] if x == sx && y == sy
  812.         if map[x, y + 1] == 0 && passable?(x, y, 2)
  813.           map[x, y + 1] = @step
  814.           new_positions.push([x, y + 1])
  815.         end
  816.         if map[x - 1, y] == 0 && passable?(x, y, 4)
  817.           map[x - 1, y] = @step
  818.           new_positions.push([x - 1, y])
  819.         end
  820.         if map[x + 1, y] == 0 && passable?(x, y, 6)
  821.           map[x + 1, y] = @step
  822.           new_positions.push([x + 1, y])
  823.         end
  824.         if map[x, y - 1] == 0 && passable?(x, y, 8)
  825.           map[x, y - 1] = @step
  826.           new_positions.push([x, y - 1])
  827.         end
  828.         # Update graphics? (to reduce lag)
  829.         update_counter += 1
  830.         if update_counter > 50
  831.           Graphics.update
  832.           update_counter = 0
  833.         end
  834.       end
  835.       old_positions = new_positions
  836.       new_positions = []
  837.     }
  838.     return [false, nil, nil]
  839.   end
  840. end  
  841.  
  842. class Game_Character < Game_CharacterBase
  843.   #--------------------------------------------------------------------------
  844.   # * Force Move Route
  845.   #--------------------------------------------------------------------------
  846.   alias shaz_mouse_game_character_force_move_route force_move_route
  847.   def force_move_route(move_route)
  848.     clear_path
  849.     shaz_mouse_game_character_force_move_route(move_route)
  850.   end
  851. end
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858. #============================================================================
  859. # SUPER SIMPLE MOUSE SCRIPT
  860. # Player
  861. #============================================================================
  862.  
  863.  
  864. class Game_Player < Game_Character
  865.   #--------------------------------------------------------------------------
  866.   # * Trigger Map Event
  867.   #     triggers : Trigger array
  868.   #     normal   : Is priority set to [Same as Characters] ?
  869.   #--------------------------------------------------------------------------
  870.   alias shaz_mouse_game_player_start_map_event start_map_event
  871.   def start_map_event(x, y, triggers, normal)
  872.     @started_events = []
  873.     shaz_mouse_game_player_start_map_event(x, y, triggers, normal)
  874.   end
  875.   #--------------------------------------------------------------------------
  876.   # * Start Event
  877.   #--------------------------------------------------------------------------
  878.   def start_event(event_id)
  879.     @started_events = [] if @started_events.nil?
  880.     @started_events.push(event_id)
  881.   end
  882.   #--------------------------------------------------------------------------
  883.   # * Processing of Movement via Input from Directional Buttons
  884.   #--------------------------------------------------------------------------
  885.   alias shaz_mouse_game_player_move_by_input move_by_input
  886.   def move_by_input
  887.     if Input.dir4 > 0
  888.       clear_path
  889.       shaz_mouse_game_player_move_by_input
  890.     else
  891.       # Move by mouse input
  892.       if !$game_message.busy? && !$game_message.visible && !@move_route_forcing &&
  893.         !@vehicle_getting_on && !@vehicle_getting_off &&
  894.         Mouse.trigger?(0) && !Mouse.grid.nil? && !Mouse.get_mouse.ignored?
  895.         mx, my = *Mouse.grid
  896.         # turn in direction
  897.         if (@x - mx).abs >= (@y - my).abs
  898.           set_direction(@x > mx ? 4 : 6)
  899.         else
  900.           set_direction(@y > my ? 8 : 2)
  901.         end
  902.         # find path
  903.         @event = $game_map.lowest_mouse_event_xy(mx, my)
  904.         if @event.nil?
  905.           find_path(mx, my)
  906.         elsif @event.mouse_autoactivate
  907.           @event.start
  908.           @started_events = []
  909.           clear_path
  910.         else
  911.           find_path(@event.x + @event.mouse_position[0],
  912.             @event.y + @event.mouse_position[1])
  913.         end
  914.       end
  915.     end
  916.   end
  917.   #--------------------------------------------------------------------------
  918.   # * Frame Update
  919.   #--------------------------------------------------------------------------
  920.   alias shaz_mouse_game_player_update update
  921.   def update
  922.     shaz_mouse_game_player_update
  923.     update_pathfinding if !@event.nil? && !moving?
  924.   end
  925.   #--------------------------------------------------------------------------
  926.   # * Check event after pathfinding
  927.   #--------------------------------------------------------------------------
  928.   def update_pathfinding
  929.     if @map.nil? || @map[@x, @y] <= 1
  930.       dir = @x < @event.x ? 6 : @x > @event.x ? 4 : @y < @event.y ? 2 : @y > @event.y ? 8 : 0
  931.       # Face event and trigger it (only if not triggered by start_map_event)
  932.       turn_toward_character(@event) if !@event.pos?(@x, @y)
  933.       if !@started_events.include?(@event.id) && !@map.nil? && !in_airship?
  934.         @event.start
  935.         @started_events = []
  936.       end
  937.       clear_path
  938.     end
  939.   end
  940.   #--------------------------------------------------------------------------
  941.   # * Clear Path
  942.   #--------------------------------------------------------------------------
  943.   def clear_path
  944.     @event = nil
  945.     super
  946.   end
  947. end
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954. #============================================================================
  955. # SUPER SIMPLE MOUSE SCRIPT
  956. # Interpreter
  957. #============================================================================
  958.  
  959.  
  960. class Game_Interpreter
  961.   #--------------------------------------------------------------------------
  962.   # * Event Setup
  963.   #--------------------------------------------------------------------------
  964.   alias shaz_mouse_game_interpreter_setup setup
  965.   def setup(list, event_id = 0, lock_player = false)
  966.     shaz_mouse_game_interpreter_setup(list, event_id)
  967.     @lock_player = lock_player
  968.   end
  969.   #--------------------------------------------------------------------------
  970.   # * Execute
  971.   #--------------------------------------------------------------------------
  972.   alias shaz_mouse_game_interpreter_run run
  973.   def run
  974.     Mouse.get_mouse.ignored = true if @lock_player
  975.     shaz_mouse_game_interpreter_run
  976.     Mouse.get_mouse.ignored = false if @lock_player
  977.   end
  978. end
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985. #============================================================================
  986. # SUPER SIMPLE MOUSE SCRIPT
  987. # Windows
  988. #============================================================================
  989.  
  990.  
  991. class Window_Selectable < Window_Base
  992.   #--------------------------------------------------------------------------
  993.   # * Frame Update
  994.   #--------------------------------------------------------------------------
  995.   alias shaz_mouse_window_selectable_update update
  996.   def update
  997.     shaz_mouse_window_selectable_update
  998.     process_mouse_handling if Input.method == :mouse
  999.   end
  1000.   #--------------------------------------------------------------------------
  1001.   # * Mouse Movement Processing
  1002.   #--------------------------------------------------------------------------
  1003.   def process_mouse_handling
  1004.     return unless Mouse.get_mouse.enabled? && cursor_movable?
  1005.     # Add a delay to prevent too-fast scrolling
  1006.     @delay = @delay ? @delay + 1 : 0
  1007.     return if @delay % 3 > 0
  1008.    
  1009.     mx, my = *Mouse.position
  1010.     vx = self.viewport ? self.x - self.viewport.ox + self.viewport.rect.x : self.x
  1011.     vy = self.viewport ? self.y - self.viewport.oy + self.viewport.rect.y : self.y
  1012.     if mx.between?(vx, vx + self.width) &&
  1013.       my.between?(vy, vy + self.height)
  1014.       mx -= vx
  1015.       mx -= padding
  1016.       my -= vy
  1017.       my -= padding
  1018.       my += oy
  1019.       for i in 0 ... item_max
  1020.         rect = item_rect(i)
  1021.         if mx.between?(rect.x, rect.x + rect.width) &&
  1022.           my.between?(rect.y, rect.y + rect.height)
  1023.           last_index = @index
  1024.           select(i)
  1025.           if @index != last_index
  1026.             Sound.play_cursor
  1027.           end
  1028.           break
  1029.         end
  1030.       end
  1031.     end
  1032.   end
  1033. end  
  1034.  
  1035. class Window_NameInput < Window_Selectable
  1036.   #--------------------------------------------------------------------------
  1037.   # * Mouse Movement Processing
  1038.   #--------------------------------------------------------------------------
  1039.   def process_mouse_handling
  1040.     return unless Mouse.get_mouse.enabled?
  1041.     # Add a delay to prevent too-fast scrolling
  1042.     @delay = @delay ? @delay + 1 : 0
  1043.     return if @delay % 3 > 0
  1044.    
  1045.     mx, my = *Mouse.position
  1046.     vx = (self.viewport ? self.x - self.viewport.ox + self.viewport.rect.x : self.x) + padding
  1047.     vy = (self.viewport ? self.y - self.viewport.oy + self.viewport.rect.y : self.y) + padding
  1048.     if mx.between?(vx, vx + self.width - padding * 2) &&
  1049.       my.between?(vy, vy + self.height - padding * 2)
  1050.       mx -= vx
  1051.       my -= vy
  1052.       x = (mx > 5*32+16 ? mx-16 : mx) / 32
  1053.       y = my / line_height
  1054.       last_index = @index
  1055.       @index = y * 10 + x
  1056.       Sound.play_cursor if @index != last_index
  1057.     end
  1058.   end
  1059. end
  1060.  
  1061. class Scene_File < Scene_MenuBase
  1062.   #--------------------------------------------------------------------------
  1063.   # * Update Cursor
  1064.   #--------------------------------------------------------------------------
  1065.   alias shaz_mouse_scene_file_update_cursor update_cursor
  1066.   def update_cursor
  1067.     shaz_mouse_scene_file_update_cursor
  1068.     process_mouse_handling if Input.method == :mouse
  1069.   end
  1070.   #--------------------------------------------------------------------------
  1071.   # * Mouse Movement Processing
  1072.   #--------------------------------------------------------------------------
  1073.   def process_mouse_handling
  1074.     return unless Mouse.get_mouse.enabled?
  1075.     # Add a delay to prevent too-fast scrolling
  1076.     @delay = @delay ? @delay + 1 : 0
  1077.     return if @delay % 3 > 0
  1078.    
  1079.     mx, my = *Mouse.position
  1080.     vx = @savefile_viewport.ox + mx
  1081.     vy = @savefile_viewport.oy + my
  1082.     last_index = @index
  1083.     new_index = vy / savefile_height
  1084.     if @index != new_index
  1085.       if new_index > @index
  1086.         cursor_down(false)
  1087.       else
  1088.         cursor_up(false)
  1089.       end
  1090.       Sound.play_cursor
  1091.       @savefile_windows[last_index].selected = false
  1092.       @savefile_windows[@index].selected = true
  1093.     end
  1094.   end
  1095. end
  1096.  
  1097. module Mouse
  1098.   def self.get_mouse
  1099.     if @mouse.nil? || @mouse.disposed?
  1100.       @mouse = Sprite_Mouse.new
  1101.     end
  1102.     @mouse
  1103.   end
  1104. end
Advertisement
Add Comment
Please, Sign In to add comment