查看: 2039|回复: 2

[已经解决] 鼠标系统求改八方向寻路。。

[复制链接]

故九江太守

梦石
0
星屑
334
在线时间
2159 小时
注册时间
2012-12-5
回帖
4323
发表于 2013-7-17 19:23:41 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 你最珍贵 于 2013-7-17 19:27 编辑
  1. class Game_Event
  2.   attr_accessor :flag
  3. end

  4. #==============================================================================
  5. # ■ Game_Map
  6. #------------------------------------------------------------------------------
  7. #  处理地图的类。包含卷动以及可以通行的判断功能。
  8. # 本类的实例请参考 $game_map 。
  9. #==============================================================================

  10. class Game_Map
  11.   def check_event_custom(x, y)
  12.     for event in $game_map.events.values
  13.       if event.screen_x/32 == x and event.screen_y/32-1 == y
  14.         for i in 0...event.list.size
  15.           if event.list[i].parameters[0] == "enemy"
  16.             event.flag = 1
  17.           elsif event.list[i].parameters[0] == "npc"
  18.             event.flag = 2
  19.           elsif event.list[i].parameters[0] == "chest"
  20.             event.flag = 3
  21.           else
  22.             event.flag = 0
  23.           end
  24.           return event.flag
  25.         end
  26.       end
  27.     end
  28.   end
  29. end#==============================================================================
  30. # 本脚本来自[url]www.66rpg.com[/url],源自geocities.jp,转载和使用请注明此内容
  31. #
  32. # 初始鼠标图案在35行和64行改,变换的鼠标图案在62行改
  33. # 这个脚本的思路是判断鼠标所在位置是否有事件,所以响应的情况只有一种。
  34. # 如果想有多种响应的话,思路复杂,比如拆分事件名称等,做出来的东西将繁琐。
  35. #                                                           —— JesseKiss
  36. #==============================================================================
  37. #=================以下两个用来调整战斗时的手感问题,可以自己试试。
  38. $敌人选框扩大 = 20
  39. $角色选框扩大 = 30
  40. #==============================================================================
  41. # API调用
  42. #==============================================================================
  43. $ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
  44. $GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
  45. $ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
  46. $GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
  47. $Window_HWND = $GetActiveWindow.call
  48. $GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
  49. #$FindWindow = Win32API.new("user32", "FindWindow", 'pp', 'i')
  50. #$HookStart  = Win32API.new("mouse_hook.dll", "HookStart", 'i', nil)
  51. #$HookEnd  = Win32API.new("mouse_hook.dll", "HookEnd", nil, nil)
  52. #$GetMouseStatus  = Win32API.new("mouse_hook.dll", "GetMouseStatus", 'i', 'i')
  53. #$Window_HWND = $FindWindow.call(nil, 'mousetry')

  54. module Mouse  
  55.   LEFT = 0x01
  56.   RIGHT = 0x02

  57.   def self.init(sprite = nil)
  58. #   $HookStart.call($Window_HWND)
  59.     $ShowCursor.call(0)
  60.    
  61.     @show_cursor = false
  62.    
  63.     @mouse_sprite = Sprite.new
  64.     @mouse_sprite.z = 99999
  65.     @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/028-Herb04.png')
  66.     #@mouse_sprite.bitmap.fill_rect(Rect.new(0, 0, 32, 32), Color.new(0, 0, 0))

  67.     @left_press = false
  68.     @right_press = false
  69.     @left_trigger = false
  70.     @right_trigger = false
  71.     @left_repeat = false
  72.     @right_repeat = false
  73.     @click_lock = false
  74.     if @a == nil
  75.       @a = true
  76.       @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  77.     end
  78.     update
  79.   end
  80.   def self.exit
  81.     @mouse_sprite.bitmap.dispose
  82.     @mouse_sprite.dispose
  83.     @show_cursor = true
  84. #    $HookEnd.call
  85.     $ShowCursor.call(1)
  86.   end
  87.   def self.mouse_debug
  88.     return @mouse_debug.bitmap
  89.   end
  90.   def self.refresh
  91.   end
  92.   def self.update
  93.     left_down = $GetKeyState.call(0x01)
  94.     right_down = $GetKeyState.call(0x02)
  95.     if Graphics.frame_count * 3 / Graphics.frame_rate != @total_sec
  96.       @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  97.       @a = !@a
  98.     end
  99.     case $pop
  100.     when 1
  101.       if @a
  102.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/1.png')
  103.       else
  104.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/11.png')
  105.       end
  106.     when 2
  107.       if @a
  108.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/2.png')
  109.       else
  110.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/22.png')
  111.       end
  112.     when 3
  113.       if @a
  114.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/3.png')
  115.       else
  116.         @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/33.png')
  117.       end
  118.     else
  119.       @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Guang Biao.png')
  120.     end
  121.     @click_lock = false
  122.     mouse_x, mouse_y = self.get_mouse_pos
  123.     if @mouse_sprite != nil
  124.       @mouse_sprite.x = mouse_x
  125.       @mouse_sprite.y = mouse_y
  126.     end
  127.     if left_down[7] == 1
  128.       @left_repeat = (not @left_repeat)
  129.       @left_trigger = (not @left_press)
  130.       @left_press = true
  131.     else
  132.       @left_press = false
  133.       @left_trigger = false
  134.       @left_repeat = false
  135.     end
  136.     if right_down[7] == 1
  137.       @right_repeat = (not @right_repeat)
  138.       @right_trigger = (not @right_press)
  139.       @right_press = true
  140.     else
  141.       @right_press = false
  142.       @right_trigger = false
  143.       @right_repeat = false
  144.     end
  145.   end
  146.   def self.get_mouse_pos
  147.     point_var = [0, 0].pack('ll')
  148.     if $GetCursorPos.call(point_var) != 0
  149.       if $ScreenToClient.call($Window_HWND, point_var) != 0
  150.         x, y = point_var.unpack('ll')
  151.         if (x < 0) or (x > 10000) then x = 0 end
  152.         if (y < 0) or (y > 10000) then y = 0 end
  153.         if x > 640 then x = 640 end
  154.         if y > 480 then y = 480 end
  155.         return x, y
  156.       else
  157.         return 0, 0
  158.       end
  159.     else
  160.       return 0, 0
  161.     end
  162.   end
  163.   def self.press?(mouse_code)
  164.     if mouse_code == LEFT
  165.       if @click_lock
  166.         return false
  167.       else
  168.         return @left_press
  169.       end
  170.     elsif mouse_code == RIGHT
  171.       return @right_press
  172.     else
  173.       return false
  174.     end
  175.   end
  176.   def self.trigger?(mouse_code)
  177.     if mouse_code == LEFT
  178.       if @click_lock
  179.         return false
  180.       else
  181.         return @left_trigger
  182.       end
  183.     elsif mouse_code == RIGHT
  184.       return @right_trigger
  185.     else
  186.       return false
  187.     end
  188.   end
  189.   def self.repeat?(mouse_code)
  190.     if mouse_code == LEFT
  191.       if @click_lock
  192.         return false
  193.       else
  194.         return @left_repeat
  195.       end
  196.     elsif mouse_code == RIGHT
  197.       return @right_repeat
  198.     else
  199.       return false
  200.     end
  201.   end
  202.   def self.click_lock?
  203.     return @click_lock
  204.   end
  205.   def self.click_lock
  206.     @click_lock = true
  207.   end
  208.   def self.click_unlock
  209.     @click_lock = false
  210.   end
  211. end
  212. module Input
  213.   if @self_update == nil
  214.     @self_update = method('update')
  215.     @self_press = method('press?')
  216.     @self_trigger = method('trigger?')
  217.     @self_repeat = method('repeat?')
  218.   end
  219.   def self.update
  220.     @self_update.call
  221.     Mouse.update
  222.   end
  223.   def self.press?(key_code)
  224.     if @self_press.call(key_code)
  225.       return true
  226.     end
  227.     if key_code == C
  228.       return Mouse.press?(Mouse::LEFT)
  229.     elsif key_code == B
  230.       return Mouse.press?(Mouse::RIGHT)
  231.     else
  232.       return @self_press.call(key_code)
  233.     end
  234.   end
  235.   def self.trigger?(key_code)
  236.     if @self_trigger.call(key_code)
  237.       return true
  238.     end
  239.     if key_code == C
  240.       return Mouse.trigger?(Mouse::LEFT)
  241.     elsif key_code == B
  242.       return Mouse.trigger?(Mouse::RIGHT)
  243.     else
  244.       return @self_trigger.call(key_code)
  245.     end
  246.   end
  247.   def self.repeat?(key_code)
  248.     if @self_repeat.call(key_code)
  249.       return true
  250.     end
  251.     if key_code == C
  252.       return Mouse.repeat?(Mouse::LEFT)
  253.     elsif key_code == B
  254.       return Mouse.repeat?(Mouse::RIGHT)
  255.     else
  256.       return @self_repeat.call(key_code)
  257.     end
  258.   end
  259. end
  260. class Window_Selectable
  261.   if @self_alias == nil
  262.     alias self_update update
  263.     @self_alias = true
  264.   end
  265.   def update
  266.     #self.cursor_rect.empty
  267.     self_update
  268.     if self.active and @item_max > 0
  269.       index_var = @index
  270.       tp_index = @index
  271.       mouse_x, mouse_y = Mouse.get_mouse_pos
  272.       mouse_not_in_rect = true
  273.       for i in 0...@item_max
  274.         @index = i
  275.         update_cursor_rect
  276.         top_x = self.cursor_rect.x + self.x + 16
  277.         top_y = self.cursor_rect.y + self.y + 16
  278.         bottom_x = top_x + self.cursor_rect.width
  279.         bottom_y = top_y + self.cursor_rect.height
  280.         if (mouse_x > top_x) and (mouse_y > top_y) and
  281.            (mouse_x < bottom_x) and (mouse_y < bottom_y)
  282.           mouse_not_in_rect = false
  283.           if tp_index != @index
  284.             tp_index = @index
  285.             $game_system.se_play($data_system.cursor_se)
  286.           end
  287.           break
  288.         end
  289.       end
  290.       if mouse_not_in_rect
  291.         @index = index_var
  292.         update_cursor_rect
  293.         Mouse.click_lock
  294.       else
  295.         Mouse.click_unlock               
  296.       end
  297.     end
  298.   end
  299. end
  300. class Window_NameInput
  301.   if @self_alias == nil
  302.     alias self_update update
  303.     @self_alias = true
  304.   end
  305.   def update
  306.     #self.cursor_rect.empty
  307.     self_update
  308.     if self.active
  309.       index_var = @index
  310.       mouse_x, mouse_y = Mouse.get_mouse_pos
  311.       mouse_not_in_rect = true
  312.       for i in (0...CHARACTER_TABLE.size).to_a.push(180)
  313.         @index = i
  314.         update_cursor_rect
  315.         top_x = self.cursor_rect.x + self.x + 16
  316.         top_y = self.cursor_rect.y + self.y + 16
  317.         bottom_x = top_x + self.cursor_rect.width
  318.         bottom_y = top_y + self.cursor_rect.height
  319.         #
  320.         if (mouse_x > top_x) and (mouse_y > top_y) and
  321.            (mouse_x < bottom_x) and (mouse_y < bottom_y)
  322.           mouse_not_in_rect = false
  323.           break
  324.         end
  325.       end
  326.       if mouse_not_in_rect
  327.         @index = index_var
  328.         update_cursor_rect
  329.         Mouse.click_lock
  330.       else
  331.         Mouse.click_unlock
  332.       end
  333.     end
  334.   end
  335. end
  336. class Window_InputNumber
  337.   if @self_alias == nil
  338.     alias self_update update
  339.     @self_alias = true
  340.   end
  341.   def update
  342.     #self.cursor_rect.empty
  343.     self_update
  344.     mouse_x, mouse_y = Mouse.get_mouse_pos
  345.     if self.active and @digits_max > 0
  346.       index_var = @index
  347.       mouse_not_in_rect = true
  348.       for i in 0...@digits_max
  349.         @index = i
  350.         update_cursor_rect
  351.         top_x = self.cursor_rect.x + self.x + 16
  352.         bottom_x = top_x + self.cursor_rect.width
  353.         #
  354.         if (mouse_x > top_x) and (mouse_x < bottom_x)
  355.           mouse_not_in_rect = false
  356.           break
  357.         end
  358.       end
  359.       if mouse_not_in_rect
  360.         @index = index_var
  361.         update_cursor_rect
  362.         Mouse.click_lock
  363.       else
  364.         Mouse.click_unlock
  365.       end
  366.     end
  367.     if @last_mouse_y == nil
  368.       @last_mouse_y = mouse_y
  369.     end
  370.     check_pos = (@last_mouse_y - mouse_y).abs
  371.     if check_pos > 10
  372.       $game_system.se_play($data_system.cursor_se)
  373.       place = 10 ** (@digits_max - 1 - @index)
  374.       n = [url=home.php?mod=space&uid=27178]@Number[/url] / place % 10
  375.       [url=home.php?mod=space&uid=27178]@Number[/url] -= n * place
  376.       n = (n + 1) % 10 if mouse_y < @last_mouse_y
  377.       n = (n + 9) % 10 if mouse_y > @last_mouse_y
  378.       [url=home.php?mod=space&uid=27178]@Number[/url] += n * place
  379.       refresh
  380.       @last_mouse_y = mouse_y
  381.     end
  382.   end
  383. end
  384. class Scene_File
  385.   if @self_alias == nil
  386.     alias self_update update
  387.     @self_alias = true
  388.   end
  389.   def update
  390.     mouse_x, mouse_y = Mouse.get_mouse_pos
  391.     Mouse.click_lock
  392.     idx = 0
  393.     for i in @savefile_windows
  394.       top_x = i.x + 16
  395.       top_y = i.y + 16
  396.       bottom_x = top_x + i.width
  397.       bottom_y = top_y + i.height
  398.       if (mouse_x > top_x) and (mouse_y > top_y) and
  399.          (mouse_x < bottom_x) and (mouse_y < bottom_y)
  400.         i.selected = true
  401.         if @file_index != idx
  402.           @file_index = idx
  403.           $game_system.se_play($data_system.cursor_se)
  404.         end            
  405.         Mouse.click_unlock
  406.       else
  407.         i.selected = false
  408.       end
  409.       idx += 1
  410.     end
  411.     self_update
  412.   end
  413. end
  414. class Arrow_Enemy
  415.   if @self_alias == nil
  416.     alias self_update update
  417.     @self_alias = true
  418.   end
  419.   def update
  420.     mouse_x, mouse_y = Mouse.get_mouse_pos
  421.     idx = 0
  422.     for i in $game_troop.enemies do
  423.       if i.exist?
  424.         top_x = i.screen_x - self.ox
  425.         top_y = i.screen_y - self.oy
  426.         bottom_x = top_x + self.src_rect.width
  427.         bottom_y = top_y + self.src_rect.height
  428.         if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
  429.            (mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
  430.           if @index != idx
  431.             $game_system.se_play($data_system.cursor_se)
  432.             @index = idx
  433.           end
  434.         end
  435.       end
  436.       idx += 1
  437.     end
  438.     self_update
  439.   end
  440. end
  441. class Arrow_Actor
  442.   if @self_alias == nil
  443.     alias self_update update
  444.     @self_alias = true
  445.   end
  446.   def update
  447.     mouse_x, mouse_y = Mouse.get_mouse_pos
  448.     idx = 0
  449.     for i in $game_party.actors do
  450.       if i.exist?
  451.         top_x = i.screen_x - self.ox
  452.         top_y = i.screen_y - self.oy
  453.         bottom_x = top_x + self.src_rect.width
  454.         bottom_y = top_y + self.src_rect.height
  455.         if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
  456.            (mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
  457.           if @index != idx
  458.             $game_system.se_play($data_system.cursor_se)
  459.             @index = idx
  460.           end
  461.         end
  462.       end
  463.       idx += 1
  464.     end
  465.     self_update
  466.   end
  467. end
  468. class Game_Player
  469.   if @self_alias == nil
  470.     alias self_update update
  471.     @self_alias = true
  472.   end
  473.   def update
  474.     mouse_x, mouse_y = Mouse.get_mouse_pos
  475.     unless $game_system.map_interpreter.running?
  476.       $pop = $game_map.check_event_custom(mouse_x/32,mouse_y/32)
  477.     else
  478.       $pop = 0
  479.     end
  480.     if @last_move_x == nil
  481.       @last_move_x = false
  482.     end
  483.     if Mouse.press?(Mouse::LEFT)
  484.       last_moving = moving?
  485.       last_direction = @direction
  486.       unless moving? or $game_system.map_interpreter.running? or
  487.              @move_route_forcing or $game_temp.message_window_showing
  488.         last_x = @x
  489.         if @last_move_x
  490.           @last_move_x = false
  491.         elsif mouse_x > screen_x + 16
  492.           move_right
  493.         elsif mouse_x < screen_x - 16
  494.           move_left
  495.         end
  496.         last_y = @y
  497.         if last_x != @x
  498.           @last_move_x = true
  499.         elsif mouse_y > screen_y
  500.           move_down
  501.         elsif mouse_y < screen_y - 32
  502.           move_up
  503.         end
  504.         if last_y != @y
  505.           @last_move_x = false
  506.         elsif not @last_move_x
  507.           case last_direction
  508.           when 2
  509.             turn_down
  510.           when 4
  511.             turn_left
  512.           when 6
  513.             turn_right
  514.           when 8
  515.             turn_up
  516.           end
  517.         end
  518.       end
  519.     end
  520.     self_update
  521.   end
  522. end
  523. Mouse.init
  524. END { Mouse.exit }
复制代码









梦石
0
星屑
752
在线时间
800 小时
注册时间
2011-10-20
回帖
2257

开拓者

发表于 2013-7-18 14:01:40 | 显示全部楼层
寻路?。。。好吧我理解了:
last_x = @x        if @last_move_x          @last_move_x = false        elsif mouse_x > screen_x + 16          move_right        elsif mouse_x < screen_x - 16          move_left        end        last_y = @y        if last_x != @x          @last_move_x = true        elsif mouse_y > screen_y          move_down        elsif mouse_y < screen_y - 32          move_up        end        if last_y != @y          @last_move_x = false        elsif not @last_move_x          case last_direction          when 2            turn_down          when 4            turn_left          when 6            turn_right          when 8            turn_up          end
这里找到没
欢迎点此进入我的egames.wink.ws,有RMQQ堂等

[url=http://rpg.blue/thread-317273-1-1.html]短篇八-赶选

http://yun.baidu.com/share/link?shareid=2158225779&uk=169642147&third=0


历险?ARPG?赢回你的疆域?新的战斗模式?…………点击这里:[宋乱贼狂 for QQ堂]
http://rpg.blue/group-368-1.html
programing ....?
[url=http://rpg.blue/thrd-234658-1-1.html]
回复

使用道具 举报

梦石
0
星屑
752
在线时间
800 小时
注册时间
2011-10-20
回帖
2257

开拓者

发表于 2013-7-18 14:06:06 | 显示全部楼层
  1. last_x = @x      
  2. if @last_move_x   
  3.       @last_move_x = false
  4.        elsif mouse_x > screen_x + 16   
  5.        move_right      
  6.   elsif mouse_x < screen_x - 16   
  7.       move_left        end
  8.         last_y = @y      
  9.   if last_x != @x      
  10.     @last_move_x = true  
  11.       elsif mouse_y > screen_y     
  12.      move_down   
  13.      elsif mouse_y < screen_y - 32
  14.           move_up   
  15.     end
  16.        if last_y != @y   
  17.       @last_move_x = false
  18.         elsif not @last_move_x  
  19.         case last_direction  
  20.         when 2  
  21.           turn_down   
  22.        when 4   
  23.         turn_left  
  24.         when 6   
  25.          turn_right   
  26.        when 8   
  27.          turn_up   
  28.       end
复制代码
你应该看得懂,判断@x@y都分开了的,你把他们弄到一起来判断,就可以得8方向了,direction也需要改成when1357这样的8方向。

点评

恭喜你不是伸手党了  发表于 2013-7-18 15:51

评分

参与人数 1星屑 +10 收起 理由
你最珍贵 + 10 刚刚找到了一个范例~

查看全部评分

欢迎点此进入我的egames.wink.ws,有RMQQ堂等

[url=http://rpg.blue/thread-317273-1-1.html]短篇八-赶选

http://yun.baidu.com/share/link?shareid=2158225779&uk=169642147&third=0


历险?ARPG?赢回你的疆域?新的战斗模式?…………点击这里:[宋乱贼狂 for QQ堂]
http://rpg.blue/group-368-1.html
programing ....?
[url=http://rpg.blue/thrd-234658-1-1.html]
回复

使用道具 举报

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

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部