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

Project1

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

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

[复制链接]

Lv2.观梦者

故九江太守

梦石
0
星屑
559
在线时间
2160 小时
注册时间
2012-12-5
帖子
4463
跳转到指定楼层
1
发表于 2013-7-17 19:23:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 你最珍贵 于 2013-7-17 19:27 编辑

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









Lv2.观梦者

梦石
0
星屑
687
在线时间
791 小时
注册时间
2011-10-20
帖子
2394

开拓者

2
发表于 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]
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
687
在线时间
791 小时
注册时间
2011-10-20
帖子
2394

开拓者

3
发表于 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]
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-9-23 05:26

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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