Advertisement
FlipelyFlip

Jet's Mouse Script (off switch to on switch

Apr 21st, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.73 KB | None | 0 0
  1. #===============================================================================
  2. # Mouse System
  3. # By Jet10985(Jet)
  4. # Some code by Daniel Martin
  5. #===============================================================================
  6. # This script will allow full use of the mouse inside of Ace for various
  7. # purposes.
  8. # This script has: 11 customization options.
  9. #===============================================================================
  10. # Overwritten Methods:
  11. # Game_Player: move_by_input
  12. # Window_NameInput: item_max
  13. #-------------------------------------------------------------------------------
  14. # Aliased methods:
  15. # Scene_Map: update, terminate, update_transfer_player
  16. # Input: update, trigger?, press?, repeat?, dir8/dir4
  17. # Window_Selectable: update
  18. # Scene_File: update, top_index=
  19. # Game_Event: update, setup_page
  20. # Game_Player: check_action_event, get_on_off_vehicle
  21. # Game_System: initialize
  22. #===============================================================================
  23. =begin
  24. Showing text above event when mouse hovers:
  25.  
  26. If you want a message to appear over an event's head if the mouse is hovering
  27. over the event, put this comment in the event:
  28.  
  29. MOUSE TEXT MESSAGE HERE
  30.  
  31. everything after TEXT will be the hovering display.
  32. --------------------------------------------------------------------------------
  33. Change mouse picture above event when mouse hovers:
  34.  
  35. If you want the mouse's picture to temporarily change whne over an event, put
  36. this comment in the event
  37.  
  38. MOUSE PIC NAME/NUMBER
  39.  
  40. if you put a name, the mouse will become that picture, but if you put a number
  41. then the mouse will become the icon that is the id number
  42. --------------------------------------------------------------------------------
  43. Specific mouse click movement routes:
  44.  
  45. If you want the player to land specifically in a square around an event when
  46. they click to move on the event, put one of these comments in the event:
  47.  
  48. MOUSE MOVE UP/LEFT/RIGHT/DOWN
  49.  
  50. only put the direction that you want the player to land on.
  51. --------------------------------------------------------------------------------
  52. Click to activate:
  53.  
  54. If you want an event to automatically start when it is clicked on, place
  55. this in an event comment:
  56.  
  57. MOUSE CLICK
  58. --------------------------------------------------------------------------------
  59. Ignore Events:
  60.  
  61. To have an event be ignored when the mouse makes it's movement path(as if the
  62. event isn't there), put this comment in the event:
  63.  
  64. MOUSE THROUGH
  65. --------------------------------------------------------------------------------
  66. You can do some extra things with the mouse using event "Script..." commands:
  67.  
  68. Mouse.set_pos(x, y) will set the mouse's position to the x and y specified.
  69.  
  70. Mouse.area?(x, y, width, height) will check if the mouse is inside the given
  71. rectangle, on-screen. This does not account for a scrolled map.
  72.  
  73. Mouse.grid will return where on the screen the mouse is, not accounting for
  74. a scrolled map. Returns an array: [x, y]
  75.  
  76. Mouse.true_grid will return where on the map the mouse is, accounting for a
  77. scrolled map. Returns an array: [x, y]
  78.  
  79. Mouse.click?(1 or 2) will return true/false depending on if a mouse button was
  80. clicked, in only the current frame. Use 1 for left-click, 2 for right-click.
  81.  
  82. Mouse.press?(1 or 2) will return true/false depending on if a mouse button is
  83. currently being pressed. Use 1 for left-click, 2 for right-click.
  84. --------------------------------------------------------------------------------
  85. Extra Notes:
  86.  
  87. You can activate action button events by standing next to the event and clicking
  88. on it with the mouse.
  89. =end
  90.  
  91. module Jet
  92. module MouseSystem
  93.  
  94. # This is the image used to display the cursor in-game.
  95. CURSOR_IMAGE = "cursor-picture"
  96.  
  97. # If the above image does not exist, the icon at this index will be used.
  98. CURSOR_ICON = 147
  99.  
  100. # turning ths switch on will completely disable the mouse.
  101. TURN_MOUSE_OFF_SWITCH = 99
  102.  
  103. # Do you want the player to be able to move using the mouse?
  104. # This can be changed in-game using toggle_mouse_movement(true/false)
  105. ALLOW_MOUSE_MOVEMENT = true
  106.  
  107. # Do you want to check for diagonal movement as well? Please note this
  108. # enables regular diagonal movement with the keyboard as well.
  109. DO_DIAGONAL_MOVEMENT = false
  110.  
  111. # If the tile they click on for movement is not passable, do you want
  112. # to check the surround tiles for a movable area?
  113. CHECK_FOR_MOVES = true
  114.  
  115. # Would you like a black box to outline the exact tile the mouse is over?
  116. DEV_OUTLINE = true
  117.  
  118. end
  119.  
  120. module HoverText
  121.  
  122. # This is the font for the hovering mouse text.
  123. FONT = "Verdana"
  124.  
  125. # This is the font color for hovering mouse text.
  126. COLOR = Color.new(255, 255, 255, 255)
  127.  
  128. # This is the font size for hovering mouse text.
  129. SIZE = 20
  130.  
  131. end
  132.  
  133. module Pathfinder
  134.  
  135. # While mainly for coders, you may change this value to allow the
  136. # pathfinder more time to find a path. 1000 is default, as it is enough for
  137. # a 100x100 maze.
  138. MAXIMUM_ITERATIONS = 1000
  139.  
  140. end
  141. end
  142.  
  143. #===============================================================================
  144. # DON'T EDIT FURTHER UNLESS YOU KNOW WHAT TO DO.
  145. #===============================================================================
  146. module Mouse
  147.  
  148. Get_Message = Win32API.new('user32', 'GetMessage', 'plll', 'l')
  149. GetAsyncKeyState = Win32API.new("user32", "GetAsyncKeyState", 'i', 'i')
  150. GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
  151. GetCursorPo = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  152. SetCursorPos = Win32API.new('user32', 'SetCursorPos', 'nn', 'n')
  153. ScreenToClient = Win32API.new('user32', 'ScreenToClient', 'lp', 'i')
  154. GetClientRect = Win32API.new('user32', 'GetClientRect', 'lp', 'i')
  155. GetWindowRect = Win32API.new('user32', 'GetWindowRect', 'lp', 'i')
  156. a = Win32API.new('kernel32', 'GetPrivateProfileString', 'pppplp', 'l')
  157. b = Win32API.new('user32', 'FindWindow', 'pp', 'i')
  158. a.call("Game", "Title", "", title = "\0" * 256, 256, ".//Game.ini")
  159. @handle = b.call("RGSS Player", title.unpack("C*").collect {|a| a.chr }.join.delete!("\0"))
  160.  
  161. Win32API.new('user32', 'ShowCursor', 'i', 'i').call(0)
  162.  
  163. module_function
  164.  
  165. def click?(button)
  166. return true if @keys.include?(button)
  167. return false
  168. end
  169.  
  170. def press?(button)
  171. return true if @press.include?(button)
  172. return false
  173. end
  174.  
  175. def set_pos(x_pos = 0, y_pos = 0)
  176. width,height = client_size
  177. if (x_pos.between?(0, width) && y_pos.between?(0, height))
  178. SetCursorPos.call(client_pos[0] + x_pos,client_pos[1] + y_pos)
  179. end
  180. end
  181.  
  182. def moved?
  183. @pos != @old_pos
  184. end
  185.  
  186. def set_cursor(image)
  187. (@cursor ||= Sprite_Cursor.new).set_cursor(image)
  188. end
  189.  
  190. def revert_cursor
  191. (@cursor ||= Sprite_Cursor.new).revert
  192. end
  193.  
  194. def update
  195. if !$game_switches.nil?
  196. if !$game_switches[Jet::MouseSystem::TURN_MOUSE_OFF_SWITCH]
  197. @keys, @press = [], []
  198. @pos = [-1, -1]
  199. @cursor.update
  200. return
  201. end
  202. end
  203. @old_pos = @pos.dup
  204. @pos = Mouse.pos
  205. @keys.clear
  206. @press.clear
  207. @keys.push(1) if GetAsyncKeyState.call(1)&0x01 == 1
  208. @keys.push(2) if GetAsyncKeyState.call(2)&0x01 == 1
  209. @keys.push(3) if GetAsyncKeyState.call(4)&0x01 == 1
  210. @press.push(1) if pressed?(1)
  211. @press.push(2) if pressed?(2)
  212. @press.push(3) if pressed?(4)
  213. @cursor.update rescue @cursor = Sprite_Cursor.new
  214. end
  215.  
  216. def init
  217. @keys = []
  218. @press = []
  219. @pos = Mouse.pos
  220. @cursor = Sprite_Cursor.new
  221. end
  222.  
  223. def pressed?(key)
  224. return true unless GetKeyState.call(key).between?(0, 1)
  225. return false
  226. end
  227.  
  228. def global_pos
  229. pos = [0, 0].pack('ll')
  230. GetCursorPo.call(pos) != 0 ? (return pos.unpack('ll')) : (return [0, 0])
  231. end
  232.  
  233. def pos
  234. x, y = screen_to_client(*global_pos)
  235. width, height = client_size
  236. begin
  237. x = 0 if x <= 0; y = 0 if y <= 0
  238. x = width if x >= width; y = height if y >= height
  239. return x, y
  240. end
  241. end
  242.  
  243. def screen_to_client(x, y)
  244. return nil unless x && y
  245. pos = [x, y].pack('ll')
  246. if ScreenToClient.call(@handle, pos) != 0
  247. return pos.unpack('ll')
  248. else
  249. return [0, 0]
  250. end
  251. end
  252.  
  253. def client_size
  254. rect = [0, 0, 0, 0].pack('l4')
  255. GetClientRect.call(@handle, rect)
  256. right,bottom = rect.unpack('l4')[2..3]
  257. return right, bottom
  258. end
  259.  
  260. def client_pos
  261. rect = [0, 0, 0, 0].pack('l4')
  262. GetWindowRect.call(@handle, rect)
  263. left, upper = rect.unpack('l4')[0..1]
  264. return left + 4, upper + 30
  265. end
  266.  
  267. def grid
  268. [(@pos[0]/32),(@pos[1]/32)]
  269. end
  270.  
  271. def true_grid
  272. xy = @pos
  273. x = ((xy[0] + ($game_map.display_x * 32)) / 32).floor
  274. y = ((xy[1] + ($game_map.display_y * 32)) / 32).floor
  275. [x, y]
  276. end
  277.  
  278. def grid_by_pos
  279. [pos[0] / 32, pos[1] / 32]
  280. end
  281.  
  282. def true_grid_by_pos
  283. xy = pos
  284. x = ((xy[0] + ($game_map.display_x * 32)) / 32).floor
  285. y = ((xy[1] + ($game_map.display_y * 32)) / 32).floor
  286. [x, y]
  287. end
  288.  
  289. def area?(x, y, width, height)
  290. @pos[0].between?(x, width + x) && @pos[1].between?(y, height + y)
  291. end
  292.  
  293. class Sprite_Cursor < Sprite
  294.  
  295. def initialize
  296. super(nil)
  297. self.z = 50000
  298. @bitmap_cache = initial_bitmap
  299. if Jet::MouseSystem::DEV_OUTLINE
  300. @outline = Sprite.new(nil)
  301. @outline.bitmap = Bitmap.new(32, 32)
  302. @outline.bitmap.fill_rect(0, 0, 32, 32, Color.new(0, 0, 0, 190))
  303. @outline.bitmap.fill_rect(1, 1, 30, 30, Color.new(0, 0, 0, 0))
  304. end
  305. end
  306.  
  307. def initial_bitmap
  308. begin
  309. self.bitmap = Cache.picture(Jet::MouseSystem::CURSOR_IMAGE)
  310. rescue
  311. self.bitmap = Bitmap.new(24, 24)
  312. icon_index = Jet::MouseSystem::CURSOR_ICON
  313. rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  314. self.bitmap.blt(0, 0, Cache.system("Iconset"), rect, 255)
  315. end
  316. self.bitmap.dup
  317. end
  318.  
  319. def set_cursor(image)
  320. if image.is_a?(Integer)
  321. self.bitmap = Bitmap.new(24, 24)
  322. icon_index = image
  323. rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  324. self.bitmap.blt(0, 0, Cache.system("Iconset"), rect, 255)
  325. else
  326. self.bitmap = Cache.picture(image)
  327. end
  328. end
  329.  
  330. def revert
  331. self.bitmap = @bitmap_cache.dup
  332. end
  333.  
  334. def update
  335. super
  336. self.x, self.y = *Mouse.pos
  337. self.visible = $game_switches[Jet::MouseSystem::TURN_MOUSE_OFF_SWITCH]
  338. if !@outline.nil?
  339. @outline.visible = SceneManager.scene_is?(Scene_Map)
  340. x = Mouse.true_grid_by_pos[0] * 32
  341. x -= $game_map.display_x.floor * 32
  342. x -= ($game_map.display_x % 1) * 32
  343. y = Mouse.true_grid_by_pos[1] * 32
  344. y -= $game_map.display_y.floor * 32
  345. y -= ($game_map.display_y % 1) * 32
  346. @outline.x = x
  347. @outline.y = [y, 1].max
  348. end
  349. end
  350. end
  351. end
  352.  
  353. Mouse.init
  354.  
  355. class << Input
  356.  
  357. alias jet5888_press? press?
  358. def press?(arg)
  359. if arg == Input::C
  360. return true if Mouse.press?(1)
  361. elsif arg == Input::B
  362. return true if Mouse.press?(2)
  363. end
  364. jet5888_press?(arg)
  365. end
  366.  
  367. alias jet5888_repeat? repeat?
  368. def repeat?(arg)
  369. if arg == Input::C
  370. return true if Mouse.click?(1)
  371. elsif arg == Input::B
  372. return true if Mouse.click?(2)
  373. end
  374. jet5888_repeat?(arg)
  375. end
  376.  
  377. alias jet5888_trigger? trigger?
  378. def trigger?(arg)
  379. if arg == Input::C
  380. return true if Mouse.click?(1)
  381. elsif arg == Input::B
  382. return true if Mouse.click?(2)
  383. end
  384. jet5888_trigger?(arg)
  385. end
  386.  
  387. if Jet::MouseSystem::DO_DIAGONAL_MOVEMENT
  388.  
  389. alias jet3845_dir8 dir8
  390. def dir8(*args, &block)
  391. if (orig = jet3845_dir8(*args, &block)) == 0
  392. if !$game_temp.nil? && SceneManager.scene_is?(Scene_Map)
  393. if !(a = $game_temp.mouse_character).nil? && a.movable?
  394. if !$game_temp.mouse_path.nil? && !$game_temp.mouse_path.empty?
  395. return $game_temp.mouse_path.shift
  396. end
  397. end
  398. end
  399. end
  400. $game_temp.mouse_path = nil if !$game_temp.nil?
  401. return orig
  402. end
  403.  
  404. else
  405.  
  406. alias jet3845_dir4 dir4
  407. def dir4(*args, &block)
  408. if (orig = jet3845_dir4(*args, &block)) == 0
  409. if !$game_temp.nil? && SceneManager.scene_is?(Scene_Map)
  410. if !(a = $game_temp.mouse_character).nil? && a.movable?
  411. if !$game_temp.mouse_path.nil? && !$game_temp.mouse_path.empty?
  412. return $game_temp.mouse_path.shift
  413. end
  414. end
  415. end
  416. end
  417. $game_temp.mouse_path = nil if !$game_temp.nil?
  418. return orig
  419. end
  420. end
  421.  
  422. alias jet8432_update update
  423. def update(*args, &block)
  424. jet8432_update(*args, &block)
  425. Mouse.update
  426. end
  427. end
  428.  
  429. class Window_Selectable
  430.  
  431. alias jet1084_update update
  432. def update(*args, &block)
  433. jet1084_update(*args, &block)
  434. update_mouse if self.active && self.visible && Mouse.moved?
  435. end
  436.  
  437. def update_mouse
  438. return if !$game_switches[Jet::MouseSystem::TURN_MOUSE_OFF_SWITCH]
  439. orig_index = @index
  440. rects = []
  441. add_x = self.x + 16 - self.ox
  442. add_y = self.y + 16 - self.oy
  443. if !self.viewport.nil?
  444. add_x += self.viewport.rect.x - self.viewport.ox
  445. add_y += self.viewport.rect.y - self.viewport.oy
  446. end
  447. self.item_max.times {|i|
  448. @index = i
  449. mouse_update_cursor
  450. rects << cursor_rect.dup
  451. }
  452. @index = orig_index
  453. rects.each_with_index {|rect, i|
  454. if Mouse.area?(rect.x + add_x, rect.y + add_y, rect.width, rect.height)
  455. @index = i
  456. end
  457. }
  458. update_cursor
  459. call_update_help
  460. end
  461.  
  462. def mouse_update_cursor
  463. if @cursor_all
  464. cursor_rect.set(0, 0, contents.width, row_max * item_height)
  465. elsif @index < 0
  466. cursor_rect.empty
  467. else
  468. cursor_rect.set(item_rect(@index))
  469. end
  470. end
  471. end
  472.  
  473. class Window_NameInput
  474.  
  475. def item_max
  476. 90
  477. end
  478. end
  479.  
  480. class Scene_File
  481.  
  482. alias jet3467_update update
  483. def update(*args, &block)
  484. update_mouse if Mouse.moved?
  485. jet3467_update(*args, &block)
  486. end
  487.  
  488. alias jet7222_top_index top_index=
  489. def top_index=(*args, &block)
  490. @last_cursor_move = 0 if @last_cursor_move.nil?
  491. @last_cursor_move -= 1
  492. return if @last_cursor_move > 0 && Mouse.moved?
  493. jet7222_top_index(*args, &block)
  494. @last_cursor_move = 10
  495. end
  496.  
  497. def update_mouse
  498. self.item_max.times {|i|
  499. ix = @savefile_windows[i].x
  500. iy = @savefile_windows[i].y + 48 - @savefile_viewport.oy
  501. iw = @savefile_windows[i].width
  502. ih = @savefile_windows[i].height
  503. if Mouse.area?(ix, iy, iw, ih)
  504. @savefile_windows[@index].selected = false
  505. @savefile_windows[i].selected = true
  506. @index = i
  507. end
  508. }
  509. ensure_cursor_visible
  510. end
  511. end
  512.  
  513. class Game_Temp
  514.  
  515. attr_accessor :mouse_character, :mouse_movement, :mouse_path
  516.  
  517. end
  518.  
  519. class Game_CharacterBase
  520.  
  521. def mouse_path(target_x, target_y, did_dir = false)
  522. return if target_x == self.x && target_y == self.y
  523. f = $game_map.find_path(target_x.to_i, target_y.to_i, @x.to_i, @y.to_i, self)
  524. if f.empty? && !did_dir && Jet::MouseSystem::CHECK_FOR_MOVES
  525. [[0, 1], [0, -1], [1, 0], [-1, 0]].each {|a|
  526. next if !f.empty?
  527. f = $game_map.find_path(target_x.to_i + a[0], target_y.to_i + a[1],
  528. @x.to_i, @y.to_i, self)
  529. }
  530. end
  531. $game_temp.mouse_path = f
  532. end
  533. end
  534.  
  535. class Game_Player
  536.  
  537. def move_by_input
  538. return if !movable? || $game_map.interpreter.running?
  539. dir = (Jet::MouseSystem::DO_DIAGONAL_MOVEMENT ? Input.dir8 : Input.dir4)
  540. if dir % 2 == 0 || !Jet::MouseSystem::DO_DIAGONAL_MOVEMENT
  541. move_straight(dir) if dir > 0 && dir % 2 == 0
  542. else
  543. horz = case dir; when 1,7; 4; when 3,9; 6; end
  544. vert = case dir; when 7,9; 8; when 1,3; 2; end
  545. move_diagonal(horz, vert)
  546. end
  547. end
  548.  
  549. alias jet3745_check_action_event check_action_event
  550. def check_action_event(*args, &block)
  551. return false unless Input.jet5888_trigger?(:C)
  552. jet3745_check_action_event(*args, &block)
  553. end
  554.  
  555. alias jet3745_get_on_off_vehicle get_on_off_vehicle
  556. def get_on_off_vehicle(*args, &block)
  557. if !Input.jet5888_trigger?(:C)
  558. [:boat, :ship, :airship].each {|a|
  559. if $game_map.send(a).pos?(*Mouse.true_grid)
  560. jet3745_get_on_off_vehicle(*args, &block)
  561. return
  562. end
  563. }
  564. elsif Input.jet5888_trigger?(:C)
  565. jet3745_get_on_off_vehicle(*args, &block)
  566. end
  567. end
  568.  
  569. def get_on_vehicle_mouse(veh)
  570. return if vehicle
  571. @vehicle_type = veh.type
  572. if vehicle
  573. turn_toward_character(veh)
  574. @vehicle_getting_on = true
  575. force_move_forward unless in_airship?
  576. @followers.gather
  577. end
  578. @vehicle_getting_on
  579. end
  580. end
  581.  
  582. class Window_MousePopUp < Window_Base
  583.  
  584. def initialize(event, text)
  585. rect = Bitmap.new(1, 1).text_size(text)
  586. width = rect.width
  587. height = rect.height
  588. super(event.screen_x - width / 2, event.screen_y - 48, width + 32, height + 32)
  589. self.opacity = 0
  590. self.contents.font.name = Jet::HoverText::FONT
  591. self.contents.font.color = Jet::HoverText::COLOR
  592. self.contents.font.size = Jet::HoverText::SIZE
  593. @text = text
  594. @event = event
  595. refresh
  596. end
  597.  
  598. def refresh
  599. contents.clear
  600. draw_text(0, 0, contents.width, contents.height, @text)
  601. end
  602.  
  603. def update
  604. super
  605. self.visible = !@event.erased? && Mouse.true_grid_by_pos == [@event.x, @event.y]
  606. self.x = @event.screen_x - contents.width / 2 - 8
  607. self.y = @event.screen_y - 64
  608. end
  609. end
  610.  
  611. class Game_Event
  612.  
  613. attr_accessor :text_box
  614.  
  615. def check_for_comment(regexp)
  616. return false if empty?
  617. for item in @list
  618. if item.code == 108 or item.code == 408
  619. if !item.parameters[0][regexp].nil?
  620. return $1.nil? ? true : $1
  621. end
  622. end
  623. end
  624. return false
  625. end
  626.  
  627. def mouse_empty?
  628. return true if empty?
  629. return @list.reject {|a| [108, 408].include?(a.code) }.size <= 1
  630. end
  631.  
  632. alias jet3745_setup_page setup_page
  633. def setup_page(*args, &block)
  634. jet3745_setup_page(*args, &block)
  635. @text_box = nil
  636. @mouse_activated = nil
  637. @mouse_cursor = nil
  638. end
  639.  
  640. def mouse_activated?
  641. @mouse_activated ||= check_for_comment(/MOUSE[ ]*CLICK/i)
  642. end
  643.  
  644. def text_box
  645. @text_box ||= (
  646. if (a = check_for_comment(/MOUSE[ ]*TEXT[ ]*(.+)/i))
  647. Window_MousePopUp.new(self, a)
  648. else
  649. false
  650. end
  651. )
  652. end
  653.  
  654. def through
  655. if $game_temp.mouse_movement && check_for_comment(/MOUSE[ ]*THROUGH/i)
  656. true
  657. else
  658. super
  659. end
  660. end
  661.  
  662. def mouse_cursor
  663. @mouse_cursor ||= (
  664. if (a = check_for_comment(/MOUSE[ ]*PIC[ ]*(\d+)/i))
  665. a.to_i
  666. elsif (a = check_for_comment(/MOUSE[ ]*PIC[ ]*(.+)/i))
  667. a
  668. else
  669. false
  670. end
  671. )
  672. end
  673.  
  674. def erased?
  675. @erased
  676. end
  677.  
  678. def movable?
  679. return false if moving?
  680. return false if $game_message.busy? || $game_message.visible
  681. return true
  682. end
  683.  
  684. def check_mouse_change
  685. if mouse_cursor
  686. Mouse.set_cursor(@mouse_cursor)
  687. return true
  688. end
  689. return false
  690. end
  691.  
  692. alias jet3845_update update
  693. def update(*args, &block)
  694. jet3845_update(*args, &block)
  695. @text_box.update if text_box
  696. end
  697. end
  698.  
  699. class Game_Vehicle
  700.  
  701. attr_reader :type
  702.  
  703. end
  704.  
  705. class Game_System
  706.  
  707. attr_accessor :mouse_movement
  708.  
  709. alias jet2735_initialize initialize
  710. def initialize(*args, &block)
  711. jet2735_initialize(*args, &block)
  712. @mouse_movement = Jet::MouseSystem::ALLOW_MOUSE_MOVEMENT
  713. end
  714. end
  715.  
  716. class Game_Interpreter
  717.  
  718. def toggle_mouse_movement(bool)
  719. $game_system.mouse_movement = bool
  720. end
  721. end
  722.  
  723. class Scene_Map
  724.  
  725. alias jet3745_update update
  726. def update(*args, &block)
  727. jet3745_update
  728. check_mouse_movement
  729. check_mouse_icon_change
  730. end
  731.  
  732. alias jet5687_terminate terminate
  733. def terminate(*args, &block)
  734. $game_map.events.values.each {|a|
  735. a.text_box.dispose if a.text_box
  736. a.text_box = nil
  737. }
  738. Mouse.update
  739. jet5687_terminate(*args, &block)
  740. end
  741.  
  742. def mouse_char
  743. $game_temp.mouse_character
  744. end
  745.  
  746. def check_mouse_icon_change
  747. changed_mouse = false
  748. $game_map.events_xy(*Mouse.true_grid_by_pos).each {|event|
  749. changed_mouse = changed_mouse || event.check_mouse_change
  750. }
  751. Mouse.revert_cursor unless changed_mouse
  752. end
  753.  
  754. def check_mouse_movement
  755. $game_temp.mouse_character ||= $game_player
  756. if Mouse.click?(1)
  757. dont_move = false
  758. x, y = *Mouse.true_grid_by_pos
  759. evs = $game_map.events_xy(x, y)
  760. (evs + $game_map.vehicles).each {|event|
  761. if event.is_a?(Game_Vehicle)
  762. if (event.x - mouse_char.x).abs + (event.y - mouse_char.y).abs == 1
  763. mouse_char.get_on_vehicle_mouse(event)
  764. dont_move = true
  765. end
  766. elsif !!!mouse_char.vehicle
  767. if event.mouse_activated?
  768. event.start
  769. dont_move = true
  770. elsif (event.x - mouse_char.x).abs + (event.y - mouse_char.y).abs == 1
  771. if !event.mouse_empty? && [0, 1, 2].include?(event.trigger)
  772. mouse_char.turn_toward_character(event)
  773. event.start
  774. dont_move = true
  775. end
  776. else
  777. {UP: [0, -1], DOWN: [0, 1], LEFT: [-1, 0], RIGHT: [1, 0]}.each {|d, a|
  778. if event.check_for_comment(/MOUSE[ ]*MOVE[ ]*#{d.to_s}/i)
  779. x += a[0]; y += a[1]
  780. did_dir = true
  781. end
  782. }
  783. end
  784. end
  785. } if $game_system.mouse_movement
  786. if $game_system.mouse_movement
  787. mouse_char.mouse_path(x, y, did_dir ||= false) unless dont_move
  788. else
  789. evs.each {|event|
  790. if event.mouse_activated?
  791. event.start
  792. end
  793. }
  794. end
  795. end
  796. end
  797. end
  798.  
  799. class Game_Map
  800.  
  801. class Astar
  802.  
  803. class PriorityQueue
  804.  
  805. def initialize
  806. @list = []
  807. end
  808.  
  809. def add(priority, item)
  810. @list << [priority, @list.length, item]
  811. @list.sort!
  812. self
  813. end
  814.  
  815. def <<(pritem)
  816. add(*pritem)
  817. end
  818.  
  819. def next
  820. @list.shift[2]
  821. end
  822.  
  823. def empty?
  824. @list.empty?
  825. end
  826. end
  827.  
  828. def initialize(map)
  829. @map = map
  830. end
  831.  
  832. def do_find_path(goal, start, char)
  833. been_there = {}
  834. pqueue = PriorityQueue.new
  835. pqueue << [1, [start, [], 1]]
  836. iters = 0
  837. while !pqueue.empty?
  838. iters += 1
  839. return [] if iters > Jet::Pathfinder::MAXIMUM_ITERATIONS
  840. spot, path_so_far, cost_so_far = pqueue.next
  841. next if been_there[spot]
  842. newpath = [path_so_far, spot]
  843. if (spot == goal)
  844. path = []
  845. newpath.flatten.each_slice(2) {|i,j| path << [i,j]}
  846. return recreate_path(path)
  847. end
  848. been_there[spot] = 1
  849. spotsfrom(spot, char).each {|newspot|
  850. next if been_there[newspot]
  851. newcost = cost_so_far + 1
  852. pqueue << [newcost + estimate(newspot, goal), [newspot,newpath,newcost]]
  853. }
  854. end
  855. return []
  856. end
  857.  
  858. def estimate(spot, goal)
  859. [(spot[0] - goal[0]).abs, (spot[1] - goal[1]).abs].max
  860. end
  861.  
  862. def spotsfrom(spot, char)
  863. neighbors = []
  864. 4.times {|i|
  865. i += 1
  866. new_x = @map.round_x_with_direction(spot[0], i * 2)
  867. new_y = @map.round_y_with_direction(spot[1], i * 2)
  868. next unless char.passable?(spot[0], spot[1], i * 2)
  869. neighbors << [new_x, new_y]
  870. }
  871. if Jet::MouseSystem::DO_DIAGONAL_MOVEMENT
  872. [2, 8].each {|a|
  873. [4, 6].each {|b|
  874. new_x = @map.round_x_with_direction(spot[0], b)
  875. new_y = @map.round_y_with_direction(spot[1], a)
  876. next unless char.diagonal_passable?(spot[0], spot[1], b, a)
  877. neighbors << [new_x, new_y]
  878. }
  879. }
  880. end
  881. neighbors
  882. end
  883.  
  884. def recreate_path(path)
  885. rec_path = []
  886. hash = {[1, 0] => 6, [-1, 0] => 4, [0, 1] => 2, [0, -1] => 8,
  887. [-1, 1] => 1, [-1, -1] => 7, [1, 1] => 3, [1, -1] => 9}
  888. until path.empty?
  889. pos = path.shift
  890. nex = path[0]
  891. next if path.empty?
  892. ar = [nex[0] <=> pos[0], nex[1] <=> pos[1]]
  893. rec_path << hash[ar]
  894. end
  895. return rec_path
  896. end
  897. end
  898.  
  899.  
  900. def find_path(t_x, t_y, x, y, char = $game_player)
  901. @astar ||= Astar.new(self)
  902. @astar.do_find_path([t_x, t_y], [x, y], char)
  903. end
  904. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement