设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2858|回复: 3
打印 上一主题 下一主题

[RMXP发布] 八方向鼠标寻路脚本

[复制链接]

Lv4.逐梦者

梦石
2
星屑
5550
在线时间
2566 小时
注册时间
2012-2-9
帖子
990

开拓者

跳转到指定楼层
1
发表于 2012-11-17 16:10:41 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
这个脚本也是很久以前就写好了,但是只发在别的同学的提问里了。貌似,没多少人围观呢!!!!
这货又来找存在感了。
原来的鼠标四方向寻路工程在这里,http://www.66rpg.com/articles/2828,但是,既然是鼠标嘛,鼠标怎么能够不用八方向呢!!!我玩过的大游戏都是八方向寻路的!!!!

于是,本代码就诞生鸟。
RUBY 代码复制
  1. #原来的鼠标四方向寻路来自66RPG,八方向由顾家枫修改。使用请带上作者信息。谢谢。
  2.  
  3. class Game_Event
  4.   attr_accessor :flag
  5. end
  6.  
  7. #==============================================================================
  8. # ■ Game_Map
  9. #------------------------------------------------------------------------------
  10. #  处理地图的类。包含卷动以及可以通行的判断功能。
  11. # 本类的实例请参考 $game_map 。
  12. #==============================================================================
  13.  
  14. class Game_Map
  15.   def check_event_custom(x, y)
  16.     for event in $game_map.events.values
  17.       if event.screen_x/32 == x and event.screen_y/32-1 == y
  18.         for i in 0...event.list.size
  19.           if event.list[i].parameters[0] == "enemy"
  20.             event.flag = 1
  21.           elsif event.list[i].parameters[0] == "npc"
  22.             event.flag = 2
  23.           elsif event.list[i].parameters[0] == "chest"
  24.             event.flag = 3
  25.           else
  26.             event.flag = 0
  27.           end
  28.           return event.flag
  29.         end
  30.       end
  31.     end
  32.   end
  33. end#==============================================================================
  34. # 本脚本来自[url=www.66rpg.com]www.66rpg.com[/url],源自geocities.jp,转载和使用请注明此内容
  35. #
  36. # 初始鼠标图案在35行和64行改,变换的鼠标图案在62行改
  37. # 这个脚本的思路是判断鼠标所在位置是否有事件,所以响应的情况只有一种。
  38. # 如果想有多种响应的话,思路复杂,比如拆分事件名称等,做出来的东西将繁琐。
  39. #                                                           —— JesseKiss
  40. #==============================================================================
  41. #=================以下两个用来调整战斗时的手感问题,可以自己试试。
  42. $敌人选框扩大 = 20
  43. $角色选框扩大 = 30
  44. #==============================================================================
  45. # API调用
  46. #==============================================================================
  47. $ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
  48. $GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
  49. $ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
  50. $GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
  51. $Window_HWND = $GetActiveWindow.call
  52. $GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
  53. #$FindWindow = Win32API.new("user32", "FindWindow", 'pp', 'i')
  54. #$HookStart  = Win32API.new("mouse_hook.dll", "HookStart", 'i', nil)
  55. #$HookEnd  = Win32API.new("mouse_hook.dll", "HookEnd", nil, nil)
  56. #$GetMouseStatus  = Win32API.new("mouse_hook.dll", "GetMouseStatus", 'i', 'i')
  57. #$Window_HWND = $FindWindow.call(nil, 'mousetry')
  58.  
  59. module Mouse  
  60.   LEFT = 0x01
  61.   RIGHT = 0x02
  62.  
  63.   def self.init(sprite = nil)
  64. #   $HookStart.call($Window_HWND)
  65.     $ShowCursor.call(0)
  66.  
  67.     @show_cursor = false
  68.  
  69.     @mouse_sprite = Sprite.new
  70.     @mouse_sprite.z = 99999
  71.     @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/028-Herb04.png')
  72.     #@mouse_sprite.bitmap.fill_rect(Rect.new(0, 0, 32, 32), Color.new(0, 0, 0))
  73.  
  74.     @left_press = false
  75.     @right_press = false
  76.     @left_trigger = false
  77.     @right_trigger = false
  78.     @left_repeat = false
  79.     @right_repeat = false
  80.     @click_lock = false
  81.     if @a == nil
  82.       @a = true
  83.       @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  84.     end
  85.     update
  86.   end
  87.   def self.exit
  88.     @mouse_sprite.bitmap.dispose
  89.     @mouse_sprite.dispose
  90.     @show_cursor = true
  91. #    $HookEnd.call
  92.     $ShowCursor.call(1)
  93.   end
  94.   def self.mouse_debug
  95.     return @mouse_debug.bitmap
  96.   end
  97.   def self.refresh
  98.   end
  99.   def self.update
  100.     left_down = $GetKeyState.call(0x01)
  101.     right_down = $GetKeyState.call(0x02)
  102.     if Graphics.frame_count * 3 / Graphics.frame_rate != @total_sec
  103.       @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  104.       @a = !@a
  105.     end
  106.     case $pop
  107.     when 1
  108.       if @a
  109.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/1.png')
  110.       else
  111.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/11.png')
  112.       end
  113.     when 2
  114.       if @a
  115.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/2.png')
  116.       else
  117.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/22.png')
  118.       end
  119.     when 3
  120.       if @a
  121.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/3.png')
  122.       else
  123.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/33.png')
  124.       end
  125.     else
  126.       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/028-Herb04.png')
  127.     end
  128.     @click_lock = false
  129.     mouse_x, mouse_y = self.get_mouse_pos
  130.     if @mouse_sprite != nil
  131.       @mouse_sprite.x = mouse_x
  132.       @mouse_sprite.y = mouse_y
  133.     end
  134.     if left_down[7] == 1
  135.       @left_repeat = (not @left_repeat)
  136.       @left_trigger = (not @left_press)
  137.       @left_press = true
  138.     else
  139.       @left_press = false
  140.       @left_trigger = false
  141.       @left_repeat = false
  142.     end
  143.     if right_down[7] == 1
  144.       @right_repeat = (not @right_repeat)
  145.       @right_trigger = (not @right_press)
  146.       @right_press = true
  147.     else
  148.       @right_press = false
  149.       @right_trigger = false
  150.       @right_repeat = false
  151.     end
  152.   end
  153.   def self.get_mouse_pos
  154.     point_var = [0, 0].pack('ll')
  155.     if $GetCursorPos.call(point_var) != 0
  156.       if $ScreenToClient.call($Window_HWND, point_var) != 0
  157.         x, y = point_var.unpack('ll')
  158.         if (x < 0) or (x > 10000) then x = 0 end
  159.         if (y < 0) or (y > 10000) then y = 0 end
  160.         if x > 640 then x = 640 end
  161.         if y > 480 then y = 480 end
  162.         return x, y
  163.       else
  164.         return 0, 0
  165.       end
  166.     else
  167.       return 0, 0
  168.     end
  169.   end
  170.   def self.press?(mouse_code)
  171.     if mouse_code == LEFT
  172.       if @click_lock
  173.         return false
  174.       else
  175.         return @left_press
  176.       end
  177.     elsif mouse_code == RIGHT
  178.       return @right_press
  179.     else
  180.       return false
  181.     end
  182.   end
  183.   def self.trigger?(mouse_code)
  184.     if mouse_code == LEFT
  185.       if @click_lock
  186.         return false
  187.       else
  188.         return @left_trigger
  189.       end
  190.     elsif mouse_code == RIGHT
  191.       return @right_trigger
  192.     else
  193.       return false
  194.     end
  195.   end
  196.   def self.repeat?(mouse_code)
  197.     if mouse_code == LEFT
  198.       if @click_lock
  199.         return false
  200.       else
  201.         return @left_repeat
  202.       end
  203.     elsif mouse_code == RIGHT
  204.       return @right_repeat
  205.     else
  206.       return false
  207.     end
  208.   end
  209.   def self.click_lock?
  210.     return @click_lock
  211.   end
  212.   def self.click_lock
  213.     @click_lock = true
  214.   end
  215.   def self.click_unlock
  216.     @click_lock = false
  217.   end
  218. end
  219. module Input
  220.   if @self_update == nil
  221.     @self_update = method('update')
  222.     @self_press = method('press?')
  223.     @self_trigger = method('trigger?')
  224.     @self_repeat = method('repeat?')
  225.   end
  226.   def self.update
  227.     @self_update.call
  228.     Mouse.update
  229.   end
  230.   def self.press?(key_code)
  231.     if @self_press.call(key_code)
  232.       return true
  233.     end
  234.     if key_code == C
  235.       return Mouse.press?(Mouse::LEFT)
  236.     elsif key_code == B
  237.       return Mouse.press?(Mouse::RIGHT)
  238.     else
  239.       return @self_press.call(key_code)
  240.     end
  241.   end
  242.   def self.trigger?(key_code)
  243.     if @self_trigger.call(key_code)
  244.       return true
  245.     end
  246.     if key_code == C
  247.       return Mouse.trigger?(Mouse::LEFT)
  248.     elsif key_code == B
  249.       return Mouse.trigger?(Mouse::RIGHT)
  250.     else
  251.       return @self_trigger.call(key_code)
  252.     end
  253.   end
  254.   def self.repeat?(key_code)
  255.     if @self_repeat.call(key_code)
  256.       return true
  257.     end
  258.     if key_code == C
  259.       return Mouse.repeat?(Mouse::LEFT)
  260.     elsif key_code == B
  261.       return Mouse.repeat?(Mouse::RIGHT)
  262.     else
  263.       return @self_repeat.call(key_code)
  264.     end
  265.   end
  266. end
  267. class Window_Selectable
  268.   if @self_alias == nil
  269.     alias self_update update
  270.     @self_alias = true
  271.   end
  272.   def update
  273.     #self.cursor_rect.empty
  274.     self_update
  275.     if self.active and @item_max > 0
  276.       index_var = @index
  277.       tp_index = @index
  278.       mouse_x, mouse_y = Mouse.get_mouse_pos
  279.       mouse_not_in_rect = true
  280.       for i in 0...@item_max
  281.         @index = i
  282.         update_cursor_rect
  283.         top_x = self.cursor_rect.x + self.x + 16
  284.         top_y = self.cursor_rect.y + self.y + 16
  285.         bottom_x = top_x + self.cursor_rect.width
  286.         bottom_y = top_y + self.cursor_rect.height
  287.         if (mouse_x > top_x) and (mouse_y > top_y) and
  288.            (mouse_x < bottom_x) and (mouse_y < bottom_y)
  289.           mouse_not_in_rect = false
  290.           if tp_index != @index
  291.             tp_index = @index
  292.             $game_system.se_play($data_system.cursor_se)
  293.           end
  294.           break
  295.         end
  296.       end
  297.       if mouse_not_in_rect
  298.         @index = index_var
  299.         update_cursor_rect
  300.         Mouse.click_lock
  301.       else
  302.         Mouse.click_unlock               
  303.       end
  304.     end
  305.   end
  306. end
  307. class Window_NameInput
  308.   if @self_alias == nil
  309.     alias self_update update
  310.     @self_alias = true
  311.   end
  312.   def update
  313.     #self.cursor_rect.empty
  314.     self_update
  315.     if self.active
  316.       index_var = @index
  317.       mouse_x, mouse_y = Mouse.get_mouse_pos
  318.       mouse_not_in_rect = true
  319.       for i in (0...CHARACTER_TABLE.size).to_a.push(180)
  320.         @index = i
  321.         update_cursor_rect
  322.         top_x = self.cursor_rect.x + self.x + 16
  323.         top_y = self.cursor_rect.y + self.y + 16
  324.         bottom_x = top_x + self.cursor_rect.width
  325.         bottom_y = top_y + self.cursor_rect.height
  326.         #
  327.         if (mouse_x > top_x) and (mouse_y > top_y) and
  328.            (mouse_x < bottom_x) and (mouse_y < bottom_y)
  329.           mouse_not_in_rect = false
  330.           break
  331.         end
  332.       end
  333.       if mouse_not_in_rect
  334.         @index = index_var
  335.         update_cursor_rect
  336.         Mouse.click_lock
  337.       else
  338.         Mouse.click_unlock
  339.       end
  340.     end
  341.   end
  342. end
  343. class Window_InputNumber
  344.   if @self_alias == nil
  345.     alias self_update update
  346.     @self_alias = true
  347.   end
  348.   def update
  349.     #self.cursor_rect.empty
  350.     self_update
  351.     mouse_x, mouse_y = Mouse.get_mouse_pos
  352.     if self.active and @digits_max > 0
  353.       index_var = @index
  354.       mouse_not_in_rect = true
  355.       for i in 0...@digits_max
  356.         @index = i
  357.         update_cursor_rect
  358.         top_x = self.cursor_rect.x + self.x + 16
  359.         bottom_x = top_x + self.cursor_rect.width
  360.         #
  361.         if (mouse_x > top_x) and (mouse_x < bottom_x)
  362.           mouse_not_in_rect = false
  363.           break
  364.         end
  365.       end
  366.       if mouse_not_in_rect
  367.         @index = index_var
  368.         update_cursor_rect
  369.         Mouse.click_lock
  370.       else
  371.         Mouse.click_unlock
  372.       end
  373.     end
  374.     if @last_mouse_y == nil
  375.       @last_mouse_y = mouse_y
  376.     end
  377.     check_pos = (@last_mouse_y - mouse_y).abs
  378.     if check_pos > 10
  379.       $game_system.se_play($data_system.cursor_se)
  380.       place = 10 ** (@digits_max - 1 - @index)
  381.       n = @number / place % 10
  382.       @number -= n * place
  383.       n = (n + 1) % 10 if mouse_y < @last_mouse_y
  384.       n = (n + 9) % 10 if mouse_y > @last_mouse_y
  385.       @number += n * place
  386.       refresh
  387.       @last_mouse_y = mouse_y
  388.     end
  389.   end
  390. end
  391. class Scene_File
  392.   if @self_alias == nil
  393.     alias self_update update
  394.     @self_alias = true
  395.   end
  396.   def update
  397.     mouse_x, mouse_y = Mouse.get_mouse_pos
  398.     Mouse.click_lock
  399.     idx = 0
  400.     for i in @savefile_windows
  401.       top_x = i.x + 16
  402.       top_y = i.y + 16
  403.       bottom_x = top_x + i.width
  404.       bottom_y = top_y + i.height
  405.       if (mouse_x > top_x) and (mouse_y > top_y) and
  406.          (mouse_x < bottom_x) and (mouse_y < bottom_y)
  407.         i.selected = true
  408.         if @file_index != idx
  409.           @file_index = idx
  410.           $game_system.se_play($data_system.cursor_se)
  411.         end            
  412.         Mouse.click_unlock
  413.       else
  414.         i.selected = false
  415.       end
  416.       idx += 1
  417.     end
  418.     self_update
  419.   end
  420. end
  421. class Arrow_Enemy
  422.   if @self_alias == nil
  423.     alias self_update update
  424.     @self_alias = true
  425.   end
  426.   def update
  427.     mouse_x, mouse_y = Mouse.get_mouse_pos
  428.     idx = 0
  429.     for i in $game_troop.enemies do
  430.       if i.exist?
  431.         top_x = i.screen_x - self.ox
  432.         top_y = i.screen_y - self.oy
  433.         bottom_x = top_x + self.src_rect.width
  434.         bottom_y = top_y + self.src_rect.height
  435.         if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
  436.            (mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
  437.           if @index != idx
  438.             $game_system.se_play($data_system.cursor_se)
  439.             @index = idx
  440.           end
  441.         end
  442.       end
  443.       idx += 1
  444.     end
  445.     self_update
  446.   end
  447. end
  448. class Arrow_Actor
  449.   if @self_alias == nil
  450.     alias self_update update
  451.     @self_alias = true
  452.   end
  453.   def update
  454.     mouse_x, mouse_y = Mouse.get_mouse_pos
  455.     idx = 0
  456.     for i in $game_party.actors do
  457.       if i.exist?
  458.         top_x = i.screen_x - self.ox
  459.         top_y = i.screen_y - self.oy
  460.         bottom_x = top_x + self.src_rect.width
  461.         bottom_y = top_y + self.src_rect.height
  462.         if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
  463.            (mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
  464.           if @index != idx
  465.             $game_system.se_play($data_system.cursor_se)
  466.             @index = idx
  467.           end
  468.         end
  469.       end
  470.       idx += 1
  471.     end
  472.     self_update
  473.   end
  474. end
  475. class Game_Player
  476.   if @self_alias == nil
  477.     alias self_update update
  478.     @self_alias = true
  479.   end
  480.   def update
  481.     mouse_x, mouse_y = Mouse.get_mouse_pos
  482.     unless $game_system.map_interpreter.running?
  483.       $pop = $game_map.check_event_custom(mouse_x/32,mouse_y/32)
  484.     else
  485.       $pop = 0
  486.     end
  487.     if @last_move_x == nil
  488.       @last_move_x = false
  489.     end
  490.     if Mouse.press?(Mouse::LEFT)
  491.       last_moving = moving?
  492.       last_direction = @direction
  493.       unless moving? or $game_system.map_interpreter.running? or
  494.              @move_route_forcing or $game_temp.message_window_showing
  495.         last_x = @x
  496.         if @last_move_x
  497.           @last_move_x = false
  498.         elsif mouse_x > screen_x + 16
  499.           if mouse_y<screen_y-32
  500.             move_upper_right
  501.           elsif mouse_y>screen_y
  502.             move_lower_right
  503.             else
  504.           move_right
  505.           end
  506.         elsif mouse_x < screen_x - 16
  507.                     if mouse_y<screen_y-32
  508.             move_upper_left
  509.           elsif mouse_y>screen_y
  510.             move_lower_left
  511.             else
  512.           move_left
  513.           end
  514.         end
  515.         last_y = @y
  516.         if last_x != @x
  517.           @last_move_x = true
  518.         elsif mouse_y > screen_y
  519.                               if mouse_x<screen_x-16
  520.             move_lower_left
  521.           elsif mouse_x>screen_x+16
  522.             move_lower_right
  523.             else
  524.           move_down
  525.           end
  526.         elsif mouse_y < screen_y - 32
  527.                               if mouse_x<screen_x-16
  528.             move_upper_left
  529.           elsif mouse_x>screen_x+16
  530.             move_upper_right
  531.             else
  532.           move_up
  533.           end
  534.         end
  535.  
  536.           if last_y != @y
  537.           @last_move_x = false
  538.         elsif not @last_move_x
  539.           case last_direction
  540.           when 2
  541.             turn_down
  542.           when 4
  543.             turn_left
  544.           when 6
  545.             turn_right
  546.           when 8
  547.             turn_up
  548.           end
  549.         end
  550.       end
  551.     end
  552.     self_update
  553.   end
  554. end
  555. Mouse.init
  556. END { Mouse.exit }

Lv2.观梦者

梦石
0
星屑
445
在线时间
377 小时
注册时间
2012-11-5
帖子
267
2
发表于 2013-4-10 12:34:52 | 只看该作者
本帖最后由 789456qwe 于 2013-4-10 12:37 编辑

八方向鼠标寻路脚本冲突
jhgk是脚本名

gfh.PNG (12.48 KB, 下载次数: 6)

gfh.PNG
回复 支持 反对

使用道具 举报

Lv2.观梦者

故九江太守

梦石
0
星屑
366
在线时间
2127 小时
注册时间
2012-12-5
帖子
4440
3
发表于 2013-7-17 17:56:07 | 只看该作者
给个范例成吗?提问帖的范例失效了貌似

点评

你好,工程范例发在这里了:http://bbs.66rpg.com/thread-321305-1-1.html  发表于 2013-7-17 23:03
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-6-16 01:12

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表