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

Project1

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

[已经解决] 鼠标脚本范围限制

[复制链接]

Lv3.寻梦者

梦石
0
星屑
3186
在线时间
1863 小时
注册时间
2010-6-19
帖子
1205
跳转到指定楼层
1
发表于 2022-4-11 19:56:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 黑米馒头 于 2022-4-11 20:05 编辑

下面这个鼠标脚本支持640X480的游戏界面,然后我把窗口分辨率调到1024X768之后,游戏出问题了,好像范围还是在640X480里面,超出外面点起来就卡顿严重。求助!!!

原先鼠标移动的范围限制在640X480的,然后我把238,239行的


       if x > 640 then x = 640 end
       if y > 480 then y = 480 end
改成      
       if x > 1024 then x = 1024 end
       if y > 768 then y = 768 end
然后鼠标已经支持那么大的范围了,但是点击超出640X480的范围就卡顿起来了





鼠标脚本
  1. #==============================================================================
  2. # ■ 完整鼠标系统(八方向)
  3. #------------------------------------------------------------------------------
  4. #  使用时务必配合专用寻路算法
  5. #   By whbm
  6. #==============================================================================
  7. #下面做一下介绍与使用说明:
  8. #    在屏幕上单击鼠标的时候,会自动进行寻路,这里为了速度更快并且为了进行迷
  9. #宫时的难度寻路被限定在当时的屏幕内部。(否则玩家直接点到终点,呵呵....知道
  10. #后果了吧)
  11. #    在角色移动过程中再次单击鼠标(即双击)并在单击第二次鼠标的时候不松手,
  12. #角色将会跟随鼠标方向移动。(这个应该好理解,不用多说吧)当角色贴着欲被启动
  13. #的事件的时候,单击NPC即可启动事件。若未贴着,将会产生自动寻路到NPC附近的某
  14. #点,那时在单击NPC即可。
  15. #    当鼠标停在某些事件上时候会发现有不同的图标,设置方法为:宝箱事件请在事
  16. #件的执行内容中添加 注释,注释内容为 Item 注意大小写。NPC事件请在事件的执行
  17. #内容中添加 注释注释内容为 NPC 注意大小写。若不箱改变某事件的图标,则不要写
  18. #那两个注释。
  19. #    当把脚本转到您工程的时候千万别忘了那个寻路用的脚本。
  20. #==============================================================================
  21. class Game_Event
  22.   attr_accessor :flag
  23. end

  24. #==============================================================================
  25. # ■ Game_Map
  26. #------------------------------------------------------------------------------
  27. #  处理地图的类。包含卷动以及可以通行的判断功能。
  28. # 本类的实例请参考 $game_map 。
  29. #==============================================================================
  30. class Game_Map
  31.   def show_rate(event)
  32.     return 1
  33.   end
  34.   #--------------------------------------------------------------------------
  35.   # ● 检查鼠标处是否有自定义的事件并返回类型
  36.   #--------------------------------------------------------------------------
  37.   def check_event_custom(mouse_x, mouse_y)
  38.     for event in $game_map.events.values #循环所有事件检查
  39.       rate =  show_rate(event)
  40.       event_width = (RPG::Cache.character(event.character_name,event.character_hue).width / $c3_每一步的帧数) * rate
  41.       event_height = (RPG::Cache.character(event.character_name,event.character_hue).height / 8) * rate
  42.       if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
  43.         for i in 0...event.list.size
  44.           if event.list[i].parameters[0] == "Item" #类型判断
  45.             event.flag = 1
  46.           elsif event.list[i].parameters[0] == "Npc" #类型判断
  47.             event.flag = 2
  48.           else
  49.             event.flag = 0 if $game_player.get_mouse_sta != 2 #无标志
  50.           end
  51.           return event.flag #返回事件类型标志
  52.         end
  53.       end
  54.     end
  55.     return 0 if $game_player.get_mouse_sta != 2 #如果不是在跟随鼠标状态,则返回无标志
  56.     return $mouse_icon_id #使鼠标图不变化
  57.   end
  58.   #--------------------------------------------------------------------------
  59.   # ● 检查鼠标处是否有事件可以开启
  60.   #--------------------------------------------------------------------------
  61.   def check_event_custom_start(mouse_x, mouse_y)
  62.     for event in $game_map.events.values #循环所有事件检查
  63.       #事件角色图片宽度、高度
  64.       rate =  show_rate(event)
  65.       event_width = (RPG::Cache.character(event.character_name,event.character_hue).width / $c3_每一步的帧数) * rate
  66.       event_height = (RPG::Cache.character(event.character_name,event.character_hue).height / 8) * rate
  67.       #判断是否鼠标在事件上
  68.       if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
  69.         way_x = $game_player.x - event.x
  70.         way_y = $game_player.y - event.y
  71.         if [1,0,-1].include?($game_player.x-event.x) and [1,0,-1].include?($game_player.y-event.y)
  72.           for i in 0...event.list.size
  73.             if ["Item","Npc"].include?(event.list[i].parameters[0]) #当事件属于自定义事件
  74.               #判断主角朝向
  75.               if way_x == -1
  76.                 p_direction = 3 if way_y == -1
  77.                 p_direction = 6 if way_y == 0
  78.                 p_direction = 9 if way_y == 1
  79.               elsif way_x == 0
  80.                 p_direction = 2 if way_y == -1
  81.                 p_direction = 8 if way_y == 1
  82.               else
  83.                 p_direction = 1 if way_y == -1
  84.                 p_direction = 4 if way_y == 0
  85.                 p_direction = 7 if way_y == 1
  86.               end
  87.               event.start #开启事件
  88.               return 1, p_direction #返回即将开启事件以及角色朝向
  89.             end
  90.           end
  91.         end
  92.       end
  93.     end
  94.     return 0, 5 #返回不会开启事件以及角色朝向不变
  95.   end
  96.   #--------------------------------------------------------------------------
  97.   # ● 检查鼠标处是否存在自定义事件 for 寻路
  98.   #--------------------------------------------------------------------------
  99.   def check_event_custom_exist(mouse_x, mouse_y)
  100.     for event in $game_map.events.values #循环所有事件检查
  101.       #事件角色图片宽度、高度
  102.       rate =  show_rate(event)
  103.       event_width = (RPG::Cache.character(event.character_name,event.character_hue).width / $c3_每一步的帧数) * rate
  104.       event_height = (RPG::Cache.character(event.character_name,event.character_hue).height / 8) * rate
  105.       if mouse_x > event.screen_x - event_width / 2 and mouse_x < event.screen_x + event_width / 2 and mouse_y + 32 > event.screen_y + 32 - event_height and mouse_y + 32 < event.screen_y + 32
  106.         for i in 0...event.list.size
  107.           return 1, event if ["Item", "Npc"].include?(event.list[i].parameters[0]) #返回存在自定义事件以及事件体
  108.         end
  109.       end
  110.     end
  111.     return 0, event #返回不存在自定义事件,以及事件体
  112.   end
  113. end
  114. #=================以下两个用来调整战斗时的手感问题,可以自己试试。
  115. $敌人选框扩大 = 20
  116. $角色选框扩大 = 30


  117. #==============================================================================
  118. # ● API调用
  119. #==============================================================================
  120. $ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
  121. $GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
  122. $ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
  123. $GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
  124. $Window_HWND = $GetActiveWindow.call
  125. $GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
  126. module Mouse  
  127. LEFT = 0x01
  128. RIGHT = 0x02

  129. def self.init(sprite = nil)
  130.    $ShowCursor.call(0)
  131.    
  132.    @show_cursor = false
  133.    
  134.    @mouse_sprite = Sprite.new
  135.    @mouse_sprite.z = 99999
  136.    @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/木剑')

  137.    @left_press = false
  138.    @right_press = false
  139.    @left_trigger = false
  140.    @right_trigger = false
  141.    @left_repeat = false
  142.    @right_repeat = false
  143.    @click_lock = false
  144.    
  145.    update
  146. end
  147. def self.exit
  148.    @mouse_sprite.bitmap.dispose
  149.    @mouse_sprite.dispose
  150.    @show_cursor = true
  151.    $ShowCursor.call(1)
  152. end
  153. def self.mouse_debug
  154.    return @mouse_debug.bitmap
  155. end
  156. def self.update
  157.    left_down = $GetKeyState.call(0x01)
  158.    right_down = $GetKeyState.call(0x02)
  159.    if Graphics.frame_count * 3 / Graphics.frame_rate != @total_sec
  160.      @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  161.      @a = !@a
  162.    end
  163.    if $scene.is_a?(Scene_Map) == false
  164.      $mouse_icon_id = 0
  165.    end
  166.    if $mouse_icon_id != $mouse_icon_id_last
  167.      case $mouse_icon_id
  168.      when 1
  169.        if @a
  170.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem1')
  171.        else
  172.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem2')
  173.        end
  174.      when 2
  175.        if @a
  176.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo1')
  177.        else
  178.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo2')
  179.        end
  180.      when 11
  181.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_LEFT')
  182.      when 12
  183.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_DOWN')
  184.      when 13
  185.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_RIGHT')
  186.      when 14
  187.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LEFT')
  188.      when 16
  189.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_RIGHT')
  190.      when 17
  191.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_LEFT')
  192.      when 18
  193.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UP')
  194.      when 19
  195.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_RIGHT')
  196.      when 0
  197.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/木剑')
  198.      end
  199.      $mouse_icon_id_last = $mouse_icon_id
  200.    end
  201.    @click_lock = false
  202.    mouse_x, mouse_y = self.get_mouse_pos
  203.    if @mouse_sprite != nil
  204.      @mouse_sprite.x = mouse_x
  205.      @mouse_sprite.y = mouse_y
  206.    end
  207.    if left_down[7] == 1
  208.      @left_repeat = (not @left_repeat)
  209.      @left_trigger = (not @left_press)
  210.      @left_press = true
  211.    else
  212.      @left_press = false
  213.      @left_trigger = false
  214.      @left_repeat = false
  215.    end
  216.    if right_down[7] == 1
  217.      @right_repeat = (not @right_repeat)
  218.      @right_trigger = (not @right_press)
  219.      @right_press = true
  220.    else
  221.      @right_press = false
  222.      @right_trigger = false
  223.      @right_repeat = false
  224.    end
  225. end
  226. def self.get_mouse_pos
  227.    point_var = [0, 0].pack('ll')
  228.    if $GetCursorPos.call(point_var) != 0
  229.      if $ScreenToClient.call($Window_HWND, point_var) != 0
  230.        x, y = point_var.unpack('ll')
  231.        if (x < 0) or (x > 10000) then x = 0 end
  232.        if (y < 0) or (y > 10000) then y = 0 end
  233.        if x > 640 then x = 640 end
  234.        if y > 480 then y = 480 end
  235.        return x, y
  236.      else
  237.        return 0, 0
  238.      end
  239.    else
  240.      return 0, 0
  241.    end
  242. end
  243. def self.press?(mouse_code)
  244.    if mouse_code == LEFT
  245.      if @click_lock
  246.        return false
  247.      else
  248.        return @left_press
  249.      end
  250.    elsif mouse_code == RIGHT
  251.      return @right_press
  252.    else
  253.      return false
  254.    end
  255. end
  256. def self.trigger?(mouse_code)
  257.    if mouse_code == LEFT
  258.      if @click_lock
  259.        return false
  260.      else
  261.        return @left_trigger
  262.      end
  263.    elsif mouse_code == RIGHT
  264.      return @right_trigger
  265.    else
  266.      return false
  267.    end
  268. end
  269. def self.repeat?(mouse_code)
  270.    if mouse_code == LEFT
  271.      if @click_lock
  272.        return false
  273.      else
  274.        return @left_repeat
  275.      end
  276.    elsif mouse_code == RIGHT
  277.      return @right_repeat
  278.    else
  279.      return false
  280.    end
  281. end
  282. def self.click_lock?
  283.    return @click_lock
  284. end
  285. def self.click_lock
  286.    @click_lock = true
  287. end
  288. def self.click_unlock
  289.    @click_lock = false
  290. end
  291. end
  292. module Input
  293. if @self_update == nil
  294.    @self_update = method('update')
  295.    @self_press = method('press?')
  296.    @self_trigger = method('trigger?')
  297.    @self_repeat = method('repeat?')
  298. end
  299. def self.update
  300.    @self_update.call
  301.    Mouse.update
  302. end
  303. def self.press?(key_code)
  304.    if @self_press.call(key_code)
  305.      return true
  306.    end
  307.    if key_code == C
  308.      return Mouse.press?(Mouse::LEFT)
  309.    elsif key_code == B
  310.      return Mouse.press?(Mouse::RIGHT)
  311.    else
  312.      return @self_press.call(key_code)
  313.    end
  314. end
  315. def self.trigger?(key_code)
  316.    if @self_trigger.call(key_code)
  317.      return true
  318.    end
  319.    if key_code == C
  320.      return Mouse.trigger?(Mouse::LEFT)
  321.    elsif key_code == B
  322.      return Mouse.trigger?(Mouse::RIGHT)
  323.    else
  324.      return @self_trigger.call(key_code)
  325.    end
  326. end
  327. def self.repeat?(key_code)
  328.    if @self_repeat.call(key_code)
  329.      return true
  330.    end
  331.    if key_code == C
  332.      return Mouse.repeat?(Mouse::LEFT)
  333.    elsif key_code == B
  334.      return Mouse.repeat?(Mouse::RIGHT)
  335.    else
  336.      return @self_repeat.call(key_code)
  337.    end
  338. end
  339. end
  340. class Window_Selectable
  341. if @self_alias == nil
  342.    alias self_update update
  343.    @self_alias = true
  344. end
  345. def update
  346.    self_update
  347.    if self.active and @item_max > 0
  348.      index_var = @index
  349.      tp_index = @index
  350.      mouse_x, mouse_y = Mouse.get_mouse_pos
  351.      mouse_not_in_rect = true
  352.      for i in 0...@item_max
  353.        @index = i
  354.        update_cursor_rect
  355.        top_x = self.cursor_rect.x + self.x + 16
  356.        top_y = self.cursor_rect.y + self.y + 16
  357.        bottom_x = top_x + self.cursor_rect.width
  358.        bottom_y = top_y + self.cursor_rect.height
  359.        if (mouse_x > top_x) and (mouse_y > top_y) and
  360.           (mouse_x < bottom_x) and (mouse_y < bottom_y)
  361.          mouse_not_in_rect = false
  362.          if tp_index != @index
  363.            tp_index = @index
  364.            $game_system.se_play($data_system.cursor_se)
  365.          end
  366.          break
  367.        end
  368.      end
  369.      if mouse_not_in_rect
  370.        @index = index_var
  371.        update_cursor_rect
  372.        Mouse.click_lock
  373.      else
  374.        Mouse.click_unlock               
  375.      end
  376.    end
  377. end
  378. end
  379. class Window_NameInput
  380. if @self_alias == nil
  381.    alias self_update update
  382.    @self_alias = true
  383. end
  384. def update
  385.    self_update
  386.    if self.active
  387.      index_var = @index
  388.      mouse_x, mouse_y = Mouse.get_mouse_pos
  389.      mouse_not_in_rect = true
  390.      for i in (0...CHARACTER_TABLE.size).to_a.push(180)
  391.        @index = i
  392.        update_cursor_rect
  393.        top_x = self.cursor_rect.x + self.x + 16
  394.        top_y = self.cursor_rect.y + self.y + 16
  395.        bottom_x = top_x + self.cursor_rect.width
  396.        bottom_y = top_y + self.cursor_rect.height
  397.        if (mouse_x > top_x) and (mouse_y > top_y) and
  398.           (mouse_x < bottom_x) and (mouse_y < bottom_y)
  399.          mouse_not_in_rect = false
  400.          break
  401.        end
  402.      end
  403.      if mouse_not_in_rect
  404.        @index = index_var
  405.        update_cursor_rect
  406.        Mouse.click_lock
  407.      else
  408.        Mouse.click_unlock
  409.      end
  410.    end
  411. end
  412. end
  413. class Window_InputNumber
  414. if @self_alias == nil
  415.    alias self_update update
  416.    @self_alias = true
  417. end
  418. def update
  419.    self_update
  420.    mouse_x, mouse_y = Mouse.get_mouse_pos
  421.    if self.active and @digits_max > 0
  422.      index_var = @index
  423.      mouse_not_in_rect = true
  424.      for i in 0...@digits_max
  425.        @index = i
  426.        update_cursor_rect
  427.        top_x = self.cursor_rect.x + self.x + 16
  428.        bottom_x = top_x + self.cursor_rect.width
  429.        if (mouse_x > top_x) and (mouse_x < bottom_x)
  430.          mouse_not_in_rect = false
  431.          break
  432.        end
  433.      end
  434.      if mouse_not_in_rect
  435.        @index = index_var
  436.        update_cursor_rect
  437.        Mouse.click_lock
  438.      else
  439.        Mouse.click_unlock
  440.      end
  441.    end
  442.    if @last_mouse_y == nil
  443.      @last_mouse_y = mouse_y
  444.    end
  445.    check_pos = (@last_mouse_y - mouse_y).abs
  446.    if check_pos > 10
  447.      $game_system.se_play($data_system.cursor_se)
  448.      place = 10 ** (@digits_max - 1 - @index)
  449.      n = @number / place % 10
  450.      @number -= n * place
  451.      n = (n + 1) % 10 if mouse_y < @last_mouse_y
  452.      n = (n + 9) % 10 if mouse_y > @last_mouse_y
  453.      @number += n * place
  454.      refresh
  455.      @last_mouse_y = mouse_y
  456.    end
  457. end
  458. end
  459. class Scene_File
  460. if @self_alias == nil
  461.    alias self_update update
  462.    @self_alias = true
  463. end
  464. def update
  465.    mouse_x, mouse_y = Mouse.get_mouse_pos
  466.    Mouse.click_lock
  467.    idx = 0
  468.    for i in @savefile_windows
  469.      top_x = i.x + 16
  470.      top_y = i.y + 16
  471.      bottom_x = top_x + i.width
  472.      bottom_y = top_y + i.height
  473.      if (mouse_x > top_x) and (mouse_y > top_y) and
  474.         (mouse_x < bottom_x) and (mouse_y < bottom_y)
  475.        i.selected = true
  476.        if @file_index != idx
  477.          @file_index = idx
  478.          $game_system.se_play($data_system.cursor_se)
  479.        end            
  480.        Mouse.click_unlock
  481.      else
  482.        i.selected = false
  483.      end
  484.      idx += 1
  485.    end
  486.    self_update
  487. end
  488. end
  489. class Arrow_Enemy
  490. if @self_alias == nil
  491.    alias self_update update
  492.    @self_alias = true
  493. end
  494. def update
  495.    mouse_x, mouse_y = Mouse.get_mouse_pos
  496.    idx = 0
  497.    for i in $game_troop.enemies do
  498.      if i.exist?
  499.        top_x = i.screen_x - self.ox
  500.        top_y = i.screen_y - self.oy
  501.        bottom_x = top_x + self.src_rect.width
  502.        bottom_y = top_y + self.src_rect.height
  503.        if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
  504.           (mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
  505.          if @index != idx
  506.            $game_system.se_play($data_system.cursor_se)
  507.            @index = idx
  508.          end
  509.        end
  510.      end
  511.      idx += 1
  512.    end
  513.    self_update
  514. end
  515. end
  516. class Arrow_Actor
  517. if @self_alias == nil
  518.    alias self_update update
  519.    @self_alias = true
  520. end
  521. def update
  522.    mouse_x, mouse_y = Mouse.get_mouse_pos
  523.    idx = 0
  524.    for i in $game_party.actors do
  525.      if i.exist?
  526.        top_x = i.screen_x - self.ox
  527.        top_y = i.screen_y - self.oy
  528.        bottom_x = top_x + self.src_rect.width
  529.        bottom_y = top_y + self.src_rect.height
  530.        if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
  531.           (mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
  532.          if @index != idx
  533.            $game_system.se_play($data_system.cursor_se)
  534.            @index = idx
  535.          end
  536.        end
  537.      end
  538.      idx += 1
  539.    end
  540.    self_update
  541. end
  542. end
  543. #==============================================================================
  544. # ■ Game_Player
  545. #------------------------------------------------------------------------------
  546. #  处理主角的类。事件启动的判定、以及地图的滚动等功能。
  547. # 本类的实例请参考 $game_player。
  548. #   鼠标控制角色的主程序
  549. #==============================================================================
  550. class Game_Player
  551. if @self_alias == nil
  552.    alias self_update update
  553.    @self_alias = true
  554. end
  555. #--------------------------------------------------------------------------
  556. # ● 得到鼠标的状态
  557. #--------------------------------------------------------------------------
  558. def get_mouse_sta
  559.    return @mouse_sta
  560. end
  561. #--------------------------------------------------------------------------
  562. # ● 完整鼠标系统
  563. #--------------------------------------------------------------------------
  564. def update
  565.    mouse_x, mouse_y = Mouse.get_mouse_pos
  566.    @mtp_x = mouse_x
  567.    @mtp_y = mouse_y
  568.    unless $game_system.map_interpreter.running? and @mouse_sta == 2 #鼠标状态不为跟随状态
  569.      #得到鼠标图标方向
  570.      $mouse_icon_id = $game_map.check_event_custom(mouse_x,mouse_y)  if not [11, 12, 13, 14, 16, 17, 18, 19].include?($mouse_icon_id)
  571.    else
  572.      #令鼠标图标为正常
  573.      $mouse_icon_id = 0 if @mouse_sta != 2
  574.    end
  575.    
  576.    #单击鼠标时进行判断寻路或跟随
  577.    if Mouse.trigger?(Mouse::LEFT) #当点击鼠标时
  578.      unless $game_system.map_interpreter.running? or
  579.             @move_route_forcing or $game_temp.message_window_showing #各种无效情况的排除
  580.        #初始化
  581.        @mouse_sta = 1
  582.        p_direction = 5
  583.        #检查鼠标处能否开启事件
  584.        event_start,p_direction = $game_map.check_event_custom_start(mouse_x, mouse_y)
  585.        #若在移动中再次点击鼠标左键(即双击左键),则改鼠标状态为跟随状态
  586.        @mouse_sta = 2 if @paths_id != nil and @paths_id != @paths.size
  587.        if @mouse_sta != 2
  588.          #鼠标状态不为跟随状态则取数据并初始化路径
  589.          trg_x = (mouse_x + $game_map.display_x / 4) / 32
  590.          trg_y = (mouse_y + $game_map.display_y / 4) / 32
  591.          @paths = []
  592.          @paths_id = 0
  593.          if event_start == 0 #若不能开启事件
  594.            if trg_x != $game_player.x or trg_y != $game_player.y #若目标不为自身则开始寻路
  595.              find_path = Find_Path.new
  596.              @paths = find_path.find_player_short_path(trg_x, trg_y, @mtp_x, @mtp_y)
  597.            end
  598.          else #若能开启事件则改变角色朝向
  599.            @direction = p_direction
  600.          end
  601.        end
  602.      end
  603.    end

  604.    #开始移动
  605.    if @mouse_sta != nil and @mouse_sta == 1 #若鼠标状态为寻路状态
  606.      unless moving? or $game_system.map_interpreter.running? or
  607.             @move_route_forcing or $game_temp.message_window_showing #排除无效情况
  608.        if @paths_id != nil and @paths != nil and @paths_id <= @paths.size #若没有完成路径
  609.          case @paths[@paths_id] #判断路径
  610.          when 6
  611.            @last_move_x = true
  612.            move_right
  613.            @paths_id += 1
  614.            @direction = 6
  615.          when 4
  616.            @last_move_x = true
  617.            move_left
  618.            @paths_id += 1
  619.            @direction = 4
  620.          when 2
  621.            @last_move_x = false
  622.            move_down
  623.            @direction = 2
  624.            @paths_id += 1
  625.          when 8
  626.            @last_move_x = false
  627.            move_up
  628.            @direction = 8
  629.            @paths_id += 1
  630.          #斜四方向
  631.          when 1
  632.            @last_move_x = false
  633.            move_lower_left
  634.            @direction = 1
  635.            @paths_id += 1
  636.          when 3
  637.            @last_move_x = false
  638.            move_lower_right
  639.            @direction = 3
  640.            @paths_id += 1
  641.          when 7
  642.            @last_move_x = false
  643.            move_upper_left
  644.            @direction = 7
  645.            @paths_id += 1
  646.          when 9
  647.            @last_move_x = false
  648.            move_upper_right
  649.            @direction = 9
  650.            @paths_id += 1
  651.          end
  652.        end
  653.      end
  654.    elsif @paths != nil and @mouse_sta == 2 #当鼠标状态为跟随,且在移动中
  655.      if Mouse.press?(Mouse::LEFT) #持续按住鼠标
  656.        unless moving? or $game_system.map_interpreter.running? or
  657.               @move_route_forcing or $game_temp.message_window_showing #排除无效情况
  658.          #跟随方向判断并跟随
  659.          rate =  $game_map.show_rate(self)
  660.          width = (RPG::Cache.character(self.character_name,self.character_hue).width / $c3_每一步的帧数) * rate
  661.          height = (RPG::Cache.character(self.character_name,self.character_hue).height / 8) * rate
  662.          self_ox = (self.screen_x - width / 2 + self.screen_x + width / 2) / 2
  663.          self_oy = (self.screen_y - height + self.screen_y) / 2
  664.          #self_ox = self.screen_x
  665.          #self_oy = self.screen_y
  666.          if (@mtp_x - self_ox)*(@mtp_x - self_ox) + (@mtp_y - self_oy)*(@mtp_y - self_oy) >= 961
  667.            if @mtp_x > self_ox
  668.              if self_oy - @mtp_y < 0.4 * (@mtp_x - self_ox) and
  669.                 @mtp_y - self_oy < 0.4 * (@mtp_x - self_ox)
  670.                move_right
  671.                $mouse_icon_id = 16
  672.                @direction = 6
  673.              end
  674.              if @mtp_y - self_oy > - 0.4 * ( self_ox - @mtp_x ) and
  675.                 @mtp_y - self_oy < - 2.4 * ( self_ox - @mtp_x )
  676.                move_lower_right
  677.                $mouse_icon_id = 13
  678.                @direction = 3
  679.              end
  680.              if @mtp_y - self_oy < 0.4 * ( self_ox - @mtp_x ) and
  681.                 @mtp_y - self_oy > 2.4 * ( self_ox - @mtp_x )
  682.                move_upper_right
  683.                $mouse_icon_id = 19
  684.                @direction = 9
  685.              end
  686.              if @mtp_y - self_oy > 2.4 * ( @mtp_x - self_ox )
  687.                move_down
  688.                $mouse_icon_id = 12
  689.                @direction = 2
  690.              end
  691.              if @mtp_y - self_oy < - 2.4 * ( @mtp_x - self_ox )
  692.                move_up
  693.                $mouse_icon_id = 18
  694.                @direction = 8
  695.              end
  696.            end
  697.            if @mtp_x < self_ox
  698.              if @mtp_y - self_oy > - 0.4 * ( self_ox - @mtp_x ) and
  699.                 @mtp_y - self_oy < 0.4 * ( self_ox - @mtp_x )
  700.                move_left
  701.                $mouse_icon_id = 14
  702.                @direction = 4
  703.              end
  704.              if @mtp_y - self_oy > 0.4 * ( self_ox - @mtp_x ) and
  705.                 @mtp_y - self_oy < 2.4 * ( self_ox - @mtp_x )
  706.                move_lower_left
  707.                $mouse_icon_id = 11
  708.                @direction = 1
  709.              end
  710.              if @mtp_y - self_oy < - 0.4 * ( self_ox - @mtp_x ) and
  711.                 @mtp_y - self_oy > - 2.4 * ( self_ox - @mtp_x )
  712.                move_upper_left
  713.                $mouse_icon_id = 17
  714.                @direction = 7
  715.              end
  716.              if @mtp_y - self_oy > 2.4 * ( self_ox - @mtp_x )
  717.                move_down
  718.                $mouse_icon_id = 12
  719.                @direction = 2
  720.              end
  721.              if @mtp_y - self_oy < - 2.4 * ( self_ox - @mtp_x )
  722.                move_up
  723.                $mouse_icon_id = 18
  724.                @direction = 8
  725.              end
  726.            end
  727.          end
  728.            #...................................................................
  729.            if @mtp_x > self_ox
  730.              if @mtp_y - self_oy > - 0.4 * ( @mtp_x - self_ox ) and
  731.                 @mtp_y - self_oy < 0.4 * ( @mtp_x - self_ox )
  732.                $mouse_icon_id = 16
  733.                @direction = 6
  734.              end
  735.              if @mtp_y - self_oy > 0.4 * ( @mtp_x - self_ox ) and
  736.                @mtp_y - self_oy < 2.4 * ( @mtp_x - self_ox )
  737.                $mouse_icon_id = 13
  738.                @direction = 3
  739.              end
  740.              if @mtp_y - self_oy < - 0.4 * ( @mtp_x - self_ox ) and
  741.                @mtp_y - self_oy > - 2.4 * ( @mtp_x - self_ox )
  742.                $mouse_icon_id = 19
  743.                @direction = 9
  744.              end
  745.              if @mtp_y - self_oy > 2.4 * ( @mtp_x - self_ox )
  746.                $mouse_icon_id = 12
  747.                @direction = 2
  748.              end
  749.              if @mtp_y - self_oy < - 2.4 * ( @mtp_x - self_ox )
  750.                $mouse_icon_id = 18
  751.                @direction = 8
  752.              end
  753.            end
  754.            if @mtp_x < self_ox
  755.              if @mtp_y - self_oy > - 0.4 * ( self_ox - @mtp_x ) and
  756.                 @mtp_y - self_oy < 0.4 * ( self_ox - @mtp_x )
  757.                $mouse_icon_id = 14
  758.                @direction = 4
  759.              end
  760.              if @mtp_y - self_oy > 0.4 * ( self_ox - @mtp_x ) and
  761.                 @mtp_y - self_oy < 2.4 * ( self_ox - @mtp_x )
  762.                $mouse_icon_id = 11
  763.                @direction = 1
  764.              end
  765.              if @mtp_y - self_oy < - 0.4 * ( self_ox - @mtp_x ) and
  766.                 @mtp_y - self_oy > - 2.4 * ( self_ox - @mtp_x )
  767.                $mouse_icon_id = 17
  768.                @direction = 7
  769.              end
  770.              if @mtp_y - self_oy > 2.4 * ( self_ox - @mtp_x )
  771.                $mouse_icon_id = 12
  772.                @direction = 2
  773.              end
  774.              if @mtp_y - self_oy < - 2.4 * ( self_ox - @mtp_x )
  775.                $mouse_icon_id = 18
  776.                @direction = 8
  777.              end
  778.            end
  779.            #...................................................................
  780.        end
  781.      else #没状态的情况
  782.        $mouse_icon_id = 0
  783.        @mouse_sta = 0
  784.        @paths_id = @paths.size #终止寻路移动
  785.      end
  786.    end
  787.    self_update
  788. end
  789. end
  790. Mouse.init
  791. END { Mouse.exit }
复制代码



寻路脚本

  1. #==============================================================================
  2. # ■ Find_Path
  3. #------------------------------------------------------------------------------
  4. #  寻路算法--完整鼠标系统(八方向)专用版
  5. #   By whbm
  6. #==============================================================================
  7. class Find_Path
  8. #--------------------------------------------------------------------------
  9. def initialize  #初始化
  10. @open_list = []
  11. @close_lise = []
  12. @path = []
  13. end  #结束初始化
  14. #--------------------------------------------------------------------------
  15. def fp_passable?(x, y, d, tr_x = -2, tr_y = -2)  #开始判定通行
  16. return false if (tr_x == @unable_xa or
  17.                  tr_x == @unable_xb or
  18.                  tr_y == @unable_ya or
  19.                  tr_y == @unable_yb)
  20. if [2, 4, 6, 8].include?(d)
  21.    if $game_player.passable?(x, y, d)
  22.      return true
  23.    else
  24.      return false
  25.    end
  26. else
  27.    case d
  28.    when 1
  29.      if ($game_player.passable?(x, y, 4) and
  30.        $game_player.passable?(x - 1, y, 2)) or
  31.         ($game_player.passable?(x, y, 2) and
  32.        $game_player.passable?(x, y + 1, 4))
  33.        return true
  34.      else
  35.        return false
  36.      end
  37.    when 3
  38.      if ($game_player.passable?(x, y, 6) and
  39.        $game_player.passable?(x + 1, y, 2)) or
  40.         ($game_player.passable?(x, y, 2) and
  41.        $game_player.passable?(x, y + 1, 6))
  42.        return true
  43.      else
  44.        return false
  45.      end
  46.    when 7
  47.      if ($game_player.passable?(x, y, 4) and
  48.        $game_player.passable?(x - 1, y, 8)) or
  49.         ($game_player.passable?(x, y, 8) and
  50.        $game_player.passable?(x, y - 1, 4))
  51.        return true
  52.      else
  53.        return false
  54.      end
  55.    when 9
  56.      if ($game_player.passable?(x, y, 6) and
  57.        $game_player.passable?(x + 1, y, 8)) or
  58.         ($game_player.passable?(x, y, 8) and
  59.        $game_player.passable?(x, y - 1, 6))
  60.        return true
  61.      else
  62.        return false
  63.      end
  64.    end
  65. end
  66. end  #结束判定通行
  67. #--------------------------------------------------------------------------
  68. def get_g(now_point)  #开始计算G值
  69. d = now_point[2]
  70. return 0 if d == 5
  71. father_point = get_father_point(now_point)
  72. g = father_point[3] + ((d == 1 or d == 3 or d == 7 or d == 9) ? 14 : 10)
  73. return g
  74. end  #结束计算G值
  75. #--------------------------------------------------------------------------
  76. def get_h(now_point)  #开始计算H值
  77. now_x = now_point[0]
  78. now_y = now_point[1]
  79. #print @trg_x,now_x,@trg_y,now_y
  80. h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
  81. return h * 10
  82. end  #结束计算H值
  83. #--------------------------------------------------------------------------
  84. def get_f(now_point)  #开始计算F值
  85. f = now_point[3] + now_point[4]
  86. return f
  87. end  #结束计算F值
  88. #--------------------------------------------------------------------------
  89. def get_point(x, y) #取已知坐标点
  90. if @open_list.size != 0
  91.    @open_list.each do |point|
  92.      if point[0] == x and point[1] == y
  93.        return point
  94.        break
  95.      end
  96.    end
  97. end
  98. if @close_list.size != 0
  99.    @close_list.each do |point|
  100.      if point[0] == x and point[1] == y
  101.        return point
  102.        break
  103.      end
  104.    end
  105. end
  106. end  #结束取已知坐标点
  107. #--------------------------------------------------------------------------
  108. def get_father_point(now_point)  #取已知点的父节点
  109. d = now_point[2]
  110. return now_point if d == 5
  111. x = now_point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
  112. y = now_point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
  113. return get_point(x, y)
  114. end  #结束取已知点的父节点
  115. #--------------------------------------------------------------------------
  116. def new_point(x, y, d)  #开始建立新节点
  117. #print x,y,d
  118. point = [x, y, d]
  119. point.push get_g(point)
  120. point.push get_h(point)
  121. point.push get_f(point)
  122. return point
  123. end  #结束建立新节点
  124. #--------------------------------------------------------------------------
  125. def get_direction(self_x, self_y, trg_x, trg_y)
  126.   if trg_x > self_x
  127.     if trg_y - self_y > - 0.4 * ( trg_x - self_x ) and
  128.       trg_y - self_y < 0.4 * ( trg_x - self_x )
  129.       return 6
  130.     end
  131.     if trg_y - self_y > 0.4 * ( trg_x - self_x ) and
  132.       trg_y - self_y < 2.4 * ( trg_x - self_x )
  133.       return 3
  134.     end
  135.     if trg_y - self_y < - 0.4 * ( trg_x - self_x ) and
  136.       trg_y - self_y > - 2.4 * ( trg_x - self_x )
  137.       return 9
  138.     end
  139.     if trg_y - self_y > 2.4 * ( trg_x - self_x )
  140.       return 2
  141.     end
  142.     if trg_y - self_y < - 2.4 * ( trg_x - self_x )
  143.       return 8
  144.     end
  145.   end
  146.   if trg_x < self_x
  147.     if trg_y - self_y > - 0.4 * ( self_x - trg_x ) and
  148.       trg_y - self_y < 0.4 * ( self_x - trg_x )
  149.       return 4
  150.     end
  151.     if trg_y - self_y > 0.4 * ( self_x - trg_x ) and
  152.       trg_y - self_y < 2.4 * ( self_x - trg_x )
  153.       return 1
  154.     end
  155.     if trg_y - self_y < - 0.4 * ( self_x - trg_x ) and
  156.       trg_y - self_y > - 2.4 * ( self_x - trg_x )
  157.       return 7
  158.     end
  159.     if trg_y - self_y > 2.4 * ( self_x - trg_x )
  160.       return 2
  161.     end
  162.     if trg_y - self_y < - 2.4 * ( self_x - trg_x )
  163.       return 8
  164.     end
  165.   end
  166. end
  167. #--------------------------------------------------------------------------
  168. def get_d_x_y(x, y, d)
  169.   d_x = x + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
  170.   d_y = y + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
  171.   return d_x, d_y
  172. end
  173. #--------------------------------------------------------------------------
  174. def find_short_path_other(self_x, self_y, trg_x, trg_y,
  175.                           real_self_x, real_self_y, real_trg_x, real_trg_y)
  176.   @self_x = self_x
  177.   @self_y = self_y
  178.   @now_x = self_x
  179.   @now_y = self_y
  180.   @trg_x = trg_x
  181.   @trg_y = trg_y
  182.   @path = []
  183.   direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
  184.   @now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
  185.   while fp_passable?(@now_x, @now_y, direction)
  186.     @path.push direction
  187.     @now_x = @now_trg_x
  188.     @now_y = @now_trg_y
  189.     @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
  190.   end
  191.   return @path
  192. end
  193. #--------------------------------------------------------------------------
  194. def find_short_path(self_x, self_y, trg_x, trg_y,
  195.                     real_self_x, real_self_y, real_trg_x, real_trg_y)  #开始搜索路径
  196.   
  197. return find_short_path_other(self_x, self_y, trg_x, trg_y,
  198.                               real_self_x, real_self_y, real_trg_x, real_trg_y) if not
  199.                   (fp_passable?(trg_x, trg_y + 1, 8) or
  200.                    fp_passable?(trg_x + 1, trg_y, 4) or
  201.                    fp_passable?(trg_x - 1, trg_y, 6) or
  202.                    fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
  203.                   
  204.                   
  205.   #根据屏幕限定搜索面积..加速
  206. @unable_xa = $game_map.display_x / 128 - 1
  207. @unable_ya = $game_map.display_y / 128 - 1
  208. @unable_xb = $game_map.display_x / 128 + 20
  209. @unable_yb = $game_map.display_y / 128 + 20


  210. @self_x = self_x
  211. @self_y = self_y
  212. @now_x = self_x
  213. @now_y = self_y
  214. @trg_x = trg_x
  215. @trg_y = trg_y
  216. @open_list = []
  217. @close_list = []
  218. #准备搜索
  219. #print @self_x,@self_y
  220. @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
  221. @open_list.push @now_point #将当前点加入关闭列表
  222. #开始搜索
  223. begin
  224. loop do
  225.    check_trg = check_around_point(@now_point)
  226.    if check_trg == true
  227.      @path = get_path
  228.      break
  229.    end
  230.    @now_point = get_lowest_f_point
  231.    if @now_point == [] or @now_point == nil
  232.      @path = []
  233.      break
  234.    end
  235. end
  236. rescue Hangup
  237.   retry
  238. end
  239. return @path
  240. end  #结束搜索路径
  241. #--------------------------------------------------------------------------
  242. def find_player_short_path(trg_x, trg_y,
  243.                            real_trg_x, real_trg_y)  #寻找角色的最短路径
  244. self_x = $game_player.x
  245. self_y = $game_player.y
  246. real_self_x = $game_player.screen_x
  247. real_self_y = $game_player.screen_y
  248. @goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
  249. if @goal_type == 1
  250.    trg_x = event.x
  251.    trg_y = event.y
  252. end
  253. return find_short_path(self_x, self_y, trg_x, trg_y,
  254.                         real_self_x, real_self_y, real_trg_x, real_trg_y)
  255. end  #结束角色的寻找路径
  256. #--------------------------------------------------------------------------
  257. def get_path  #取得最终的路径
  258. path = []
  259. now_point = @open_list[@open_list.size - 1]
  260. path.push(10 - now_point[2])
  261. last_point = now_point
  262. loop do
  263.    now_point = get_father_point(now_point)
  264.    break if now_point[2] == 5
  265.    path.push(10 - now_point[2])
  266. end
  267. return path.reverse
  268. end  #结束取得最终的路径
  269. #--------------------------------------------------------------------------
  270. def get_lowest_f_point  #开始取得最低F值的点
  271. if @open_list == []
  272.    return []
  273. end
  274. last_lowest_f_point = @open_list[0]
  275. @open_list.each do |point|
  276.    last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
  277. end
  278. return last_lowest_f_point
  279. end  #结束取得最低F值点
  280. #--------------------------------------------------------------------------
  281. def check_around_point(point)  #开始检查已知点的八方节点
  282. for d in [1, 2, 3, 4, 6, 7, 8, 9]
  283.    x = point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
  284.    y = point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
  285.    if in_close_list?(x, y) #在关闭列表中
  286.      next
  287.    elsif in_open_list?(x, y) #在开启列表中
  288.      get_new_g_point = new_point(x, y, 10 - d)
  289.      get_last_g_point = get_point(x, y)
  290.      if get_new_g_point[3] >= get_last_g_point[3]
  291.        next
  292.      else
  293.        #如果改变父节点是新G值更小则确定改变
  294.        @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
  295.      end
  296.    else
  297.      if fp_passable?(point[0], point[1], d, x, y)
  298.        # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
  299.        @open_list.push new_point(x, y, 10 - d)
  300.        #如果将目标点添加到了开启列表中就返回true
  301.        return true if x == @trg_x and y == @trg_y
  302.        return true if @goal_type == 1 and [1, 0, -1].include?(x - @trg_x) and [1, 0, -1].include?(y - @trg_y)
  303.      end
  304.    end
  305. end
  306. #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
  307. @close_list.push point
  308. @open_list.delete(point)
  309. #此刻没找到目标点并返回false
  310. return false
  311. end  #结束计算已知点的八方节点
  312. #--------------------------------------------------------------------------
  313. def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
  314. @open_list.each do |point|
  315.    return true if point[0] == x and point[1] == y
  316. end
  317. return false
  318. end  #结束检查谋点是否在开启列表中
  319. #--------------------------------------------------------------------------
  320. def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
  321. @close_list.each do |point|
  322.    return true if point[0] == x and point[1] == y
  323. end
  324. return false
  325. end  #结束检查谋点是否在关闭列表中
  326. #--------------------------------------------------------------------------
  327. end
复制代码

Lv6.析梦学徒

老鹰

梦石
40
星屑
33417
在线时间
6553 小时
注册时间
2012-5-26
帖子
3178

极短24评委极短23参与极短22参与极短21评委老司机慢点开短篇十吟唱者组别冠军开拓者剧作品鉴家

2
发表于 2022-4-12 09:07:32 | 只看该作者
本帖最后由 百里_飞柳 于 2022-4-12 09:16 编辑

RUBY 代码复制
  1. #根据屏幕限定搜索面积..加速
  2. @unable_xa = $game_map.display_x / 128 - 1
  3. @unable_ya = $game_map.display_y / 128 - 1
  4. @unable_xb = $game_map.display_x / 128 + 20
  5. @unable_yb = $game_map.display_y / 128 + 20


寻路脚本里的这几行
我不是很懂 XP 的 display_x 的值的含义,理论上应该都是当前显示区域的左上角,在整张地图中的位置,
而单个图块是32x32, /128 就是 /32/4
display_x / 4 是以整张地图为坐标轴,左上作为原点后,当前屏幕显示区域的左上角在该坐标组里的坐标?(像素值)
再 /32 就变成了图块的位置?(编辑器中的坐标)

而 + 20 就限定了搜索范围只有  20 格?
这样限定范围导致范围外的失效?

试试改大点呢

点评

奈斯,完美解决! 谢谢  发表于 2022-4-12 11:58

评分

参与人数 1星屑 +100 +1 收起 理由
guoxiaomi + 100 + 1 我很赞同

查看全部评分

回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-4-30 03:47

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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