幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2370|回复: 12

[已解决] 求助..四方向鼠标全局控制问题

[复制链接]

1

主题

11

帖子

89

积分

②入门

积分
89
发表于 2010-1-20 15:56:34 | 显示全部楼层 |阅读模式
已解决。。

本帖子中包含更多资源

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

x
回复

使用道具 举报

1

主题

11

帖子

89

积分

②入门

积分
89
 楼主| 发表于 2010-1-20 15:57:17 | 显示全部楼层
本人是脚本白白。。希望大家能够解决一下
回复 支持 反对

使用道具 举报

38

主题

129

帖子

1151

积分

⑥精研

无既空,空既色

积分
1151
发表于 2010-1-20 16:22:27 | 显示全部楼层
  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.   #--------------------------------------------------------------------------
  32.   # ● 检查鼠标处是否有自定义的事件并返回类型
  33.   #--------------------------------------------------------------------------
  34.   def check_event_custom(mouse_x, mouse_y)
  35.     for event in $game_map.events.values #循环所有事件检查
  36.       event_width = RPG::Cache.character(event.character_name,event.character_hue).width / 4
  37.       event_height = RPG::Cache.character(event.character_name,event.character_hue).height / 4
  38.       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
  39.         for i in 0...event.list.size
  40.           if event.list.parameters[0] == "Item" #类型判断
  41.             event.flag = 1
  42.           elsif event.list.parameters[0] == "Npc" #类型判断
  43.             event.flag = 2
  44.           else
  45.             event.flag = 0 if $game_player.get_mouse_sta != 2 #无标志
  46.           end
  47.           return event.flag #返回事件类型标志
  48.         end
  49.       end
  50.     end
  51.     return 0 if $game_player.get_mouse_sta != 2 #如果不是在跟随鼠标状态,则返回无标志
  52.     return $mouse_icon_id #使鼠标图不变化
  53.   end
  54.   #--------------------------------------------------------------------------
  55.   # ● 检查鼠标处是否有事件可以开启
  56.   #--------------------------------------------------------------------------
  57.   def check_event_custom_start(mouse_x, mouse_y)
  58.     for event in $game_map.events.values #循环所有事件检查
  59.       #事件角色图片宽度、高度
  60.       event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
  61.       event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
  62.       #判断是否鼠标在事件上
  63.       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
  64.         way_x = $game_player.x - event.x
  65.         way_y = $game_player.y - event.y
  66.         if ([1, -1].include?($game_player.x-event.x) and $game_player.y-event.y == 0) or ([1, -1].include?($game_player.y-event.y) and $game_player.x-event.x == 0)
  67.           for i in 0...event.list.size
  68.             if ["Item","Npc"].include?(event.list.parameters[0]) #当事件属于自定义事件
  69.               #判断主角朝向
  70.               if way_x == -1
  71.                 p_direction = 6 if way_y == 0
  72.               elsif way_x == 0
  73.                 p_direction = 2 if way_y == -1
  74.                 p_direction = 8 if way_y == 1
  75.               else
  76.                 p_direction = 4 if way_y == 0
  77.               end
  78.               event.start #开启事件
  79.               return 1, p_direction #返回即将开启事件以及角色朝向
  80.             end
  81.           end
  82.         end
  83.       end
  84.     end
  85.     return 0, 5 #返回不会开启事件以及角色朝向不变
  86.   end
  87.   #--------------------------------------------------------------------------
  88.   # ● 检查鼠标处是否存在自定义事件 for 寻路
  89.   #--------------------------------------------------------------------------
  90.   def check_event_custom_exist(mouse_x, mouse_y)
  91.     for event in $game_map.events.values #循环所有事件检查
  92.       #事件角色图片宽度、高度
  93.       event_width = RPG::Cache.character(event.character_name,event.character_hue).width/4
  94.       event_height = RPG::Cache.character(event.character_name,event.character_hue).height/4
  95.       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
  96.         for i in 0...event.list.size
  97.           return 1, event if ["Item", "Npc"].include?(event.list.parameters[0]) #返回存在自定义事件以及事件体
  98.         end
  99.       end
  100.     end
  101.     return 0, event #返回不存在自定义事件,以及事件体
  102.   end
  103. end
  104. #=================以下两个用来调整战斗时的手感问题,可以自己试试。
  105. $敌人选框扩大 = 20
  106. $角色选框扩大 = 30
  107. #==============================================================================
  108. # ● API调用
  109. #==============================================================================
  110. $ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
  111. $GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
  112. $ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
  113. $GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
  114. $Window_HWND = $GetActiveWindow.call
  115. $GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
  116. module Mouse   
  117. LEFT = 0x01
  118. RIGHT = 0x02
  119. def self.init(sprite = nil)
  120.    $ShowCursor.call(0)
  121.    
  122.    @show_cursor = false
  123.    
  124.    @mouse_sprite = Sprite.new
  125.    @mouse_sprite.z = 99999
  126.    @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/001-Weapon01.png')
  127.    @left_press = false
  128.    @right_press = false
  129.    @left_trigger = false
  130.    @right_trigger = false
  131.    @left_repeat = false
  132.    @right_repeat = false
  133.    @click_lock = false
  134.    
  135.    update
  136. end
  137. def self.exit
  138.    @mouse_sprite.bitmap.dispose
  139.    @mouse_sprite.dispose
  140.    @show_cursor = true
  141.    $ShowCursor.call(1)
  142. end
  143. def self.mouse_debug
  144.    return @mouse_debug.bitmap
  145. end
  146. def self.update
  147.    left_down = $GetKeyState.call(0x01)
  148.    right_down = $GetKeyState.call(0x02)
  149.    if Graphics.frame_count * 3 / Graphics.frame_rate != @total_sec
  150.      @total_sec = Graphics.frame_count * 3 / Graphics.frame_rate
  151.      @a = !@a
  152.    end
  153.    if $scene.is_a?(Scene_Map) == false
  154.      $mouse_icon_id = 0
  155.    end
  156.    if $mouse_icon_id != $mouse_icon_id_last
  157.      case $mouse_icon_id
  158.      when 1
  159.        if @a
  160.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem1')
  161.        else
  162.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/GetItem2')
  163.        end
  164.      when 2
  165.        if @a
  166.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo1')
  167.        else
  168.          @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/TalkTo2')
  169.        end
  170.      when 11
  171.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_LEFT')
  172.      when 12
  173.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_DOWN')
  174.      when 13
  175.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LOWER_RIGHT')
  176.      when 14
  177.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_LEFT')
  178.      when 16
  179.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_RIGHT')
  180.      when 17
  181.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_LEFT')
  182.      when 18
  183.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UP')
  184.      when 19
  185.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/Mouse_UPPER_RIGHT')
  186.      when 0
  187.        @mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/木剑')
  188.      end
  189.      $mouse_icon_id_last = $mouse_icon_id
  190.    end
  191.    @click_lock = false
  192.    mouse_x, mouse_y = self.get_mouse_pos
  193.    if @mouse_sprite != nil
  194.      @mouse_sprite.x = mouse_x
  195.      @mouse_sprite.y = mouse_y
  196.    end
  197.    if left_down[7] == 1
  198.      @left_repeat = (not @left_repeat)
  199.      @left_trigger = (not @left_press)
  200.      @left_press = true
  201.    else
  202.      @left_press = false
  203.      @left_trigger = false
  204.      @left_repeat = false
  205.    end
  206.    if right_down[7] == 1
  207.      @right_repeat = (not @right_repeat)
  208.      @right_trigger = (not @right_press)
  209.      @right_press = true
  210.    else
  211.      @right_press = false
  212.      @right_trigger = false
  213.      @right_repeat = false
  214.    end
  215. end
  216. def self.get_mouse_pos
  217.    point_var = [0, 0].pack('ll')
  218.    if $GetCursorPos.call(point_var) != 0
  219.      if $ScreenToClient.call($Window_HWND, point_var) != 0
  220.        x, y = point_var.unpack('ll')
  221.        if (x < 0) or (x > 10000) then x = 0 end
  222.        if (y < 0) or (y > 10000) then y = 0 end
  223.        if x > 640 then x = 640 end
  224.        if y > 480 then y = 480 end
  225.        return x, y
  226.      else
  227.        return 0, 0
  228.      end
  229.    else
  230.      return 0, 0
  231.    end
  232. end
  233. def self.press?(mouse_code)
  234.    if mouse_code == LEFT
  235.      if @click_lock
  236.        return false
  237.      else
  238.        return @left_press
  239.      end
  240.    elsif mouse_code == RIGHT
  241.      return @right_press
  242.    else
  243.      return false
  244.    end
  245. end
  246. def self.trigger?(mouse_code)
  247.    if mouse_code == LEFT
  248.      if @click_lock
  249.        return false
  250.      else
  251.        return @left_trigger
  252.      end
  253.    elsif mouse_code == RIGHT
  254.      return @right_trigger
  255.    else
  256.      return false
  257.    end
  258. end
  259. def self.repeat?(mouse_code)
  260.    if mouse_code == LEFT
  261.      if @click_lock
  262.        return false
  263.      else
  264.        return @left_repeat
  265.      end
  266.    elsif mouse_code == RIGHT
  267.      return @right_repeat
  268.    else
  269.      return false
  270.    end
  271. end
  272. def self.click_lock?
  273.    return @click_lock
  274. end
  275. def self.click_lock
  276.    @click_lock = true
  277. end
  278. def self.click_unlock
  279.    @click_lock = false
  280. end
  281. end
  282. module Input
  283. if @self_update == nil
  284.    @self_update = method('update')
  285.    @self_press = method('press?')
  286.    @self_trigger = method('trigger?')
  287.    @self_repeat = method('repeat?')
  288. end
  289. def self.update
  290.    @self_update.call
  291.    Mouse.update
  292. end
  293. def self.press?(key_code)
  294.    if @self_press.call(key_code)
  295.      return true
  296.    end
  297.    if key_code == C
  298.      return Mouse.press?(Mouse::LEFT)
  299.    elsif key_code == B
  300.      return Mouse.press?(Mouse::RIGHT)
  301.    else
  302.      return @self_press.call(key_code)
  303.    end
  304. end
  305. def self.trigger?(key_code)
  306.    if @self_trigger.call(key_code)
  307.      return true
  308.    end
  309.    if key_code == C
  310.      return Mouse.trigger?(Mouse::LEFT)
  311.    elsif key_code == B
  312.      return Mouse.trigger?(Mouse::RIGHT)
  313.    else
  314.      return @self_trigger.call(key_code)
  315.    end
  316. end
  317. def self.repeat?(key_code)
  318.    if @self_repeat.call(key_code)
  319.      return true
  320.    end
  321.    if key_code == C
  322.      return Mouse.repeat?(Mouse::LEFT)
  323.    elsif key_code == B
  324.      return Mouse.repeat?(Mouse::RIGHT)
  325.    else
  326.      return @self_repeat.call(key_code)
  327.    end
  328. end
  329. end
  330. class Window_Selectable
  331. if @self_alias == nil
  332.    alias self_update update
  333.    @self_alias = true
  334. end
  335. def update
  336.    self_update
  337.    if self.active and @item_max > 0
  338.      index_var = @index
  339.      tp_index = @index
  340.      mouse_x, mouse_y = Mouse.get_mouse_pos
  341.      mouse_not_in_rect = true
  342.      for i in 0...@item_max
  343.        @index = i
  344.        update_cursor_rect
  345.        top_x = self.cursor_rect.x + self.x + 16
  346.        top_y = self.cursor_rect.y + self.y + 16
  347.        bottom_x = top_x + self.cursor_rect.width
  348.        bottom_y = top_y + self.cursor_rect.height
  349.        if (mouse_x > top_x) and (mouse_y > top_y) and
  350.           (mouse_x < bottom_x) and (mouse_y < bottom_y)
  351.          mouse_not_in_rect = false
  352.          if tp_index != @index
  353.            tp_index = @index
  354.            $game_system.se_play($data_system.cursor_se)
  355.          end
  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. end
  368. end
  369. class Window_NameInput
  370. if @self_alias == nil
  371.    alias self_update update
  372.    @self_alias = true
  373. end
  374. def update
  375.    self_update
  376.    if self.active
  377.      index_var = @index
  378.      mouse_x, mouse_y = Mouse.get_mouse_pos
  379.      mouse_not_in_rect = true
  380.      for i in (0...CHARACTER_TABLE.size).to_a.push(180)
  381.        @index = i
  382.        update_cursor_rect
  383.        top_x = self.cursor_rect.x + self.x + 16
  384.        top_y = self.cursor_rect.y + self.y + 16
  385.        bottom_x = top_x + self.cursor_rect.width
  386.        bottom_y = top_y + self.cursor_rect.height
  387.        if (mouse_x > top_x) and (mouse_y > top_y) and
  388.           (mouse_x < bottom_x) and (mouse_y < bottom_y)
  389.          mouse_not_in_rect = false
  390.          break
  391.        end
  392.      end
  393.      if mouse_not_in_rect
  394.        @index = index_var
  395.        update_cursor_rect
  396.        Mouse.click_lock
  397.      else
  398.        Mouse.click_unlock
  399.      end
  400.    end
  401. end
  402. end
  403. class Window_InputNumber
  404. if @self_alias == nil
  405.    alias self_update update
  406.    @self_alias = true
  407. end
  408. def update
  409.    self_update
  410.    mouse_x, mouse_y = Mouse.get_mouse_pos
  411.    if self.active and @digits_max > 0
  412.      index_var = @index
  413.      mouse_not_in_rect = true
  414.      for i in 0...@digits_max
  415.        @index = i
  416.        update_cursor_rect
  417.        top_x = self.cursor_rect.x + self.x + 16
  418.        bottom_x = top_x + self.cursor_rect.width
  419.        if (mouse_x > top_x) and (mouse_x < bottom_x)
  420.          mouse_not_in_rect = false
  421.          break
  422.        end
  423.      end
  424.      if mouse_not_in_rect
  425.        @index = index_var
  426.        update_cursor_rect
  427.        Mouse.click_lock
  428.      else
  429.        Mouse.click_unlock
  430.      end
  431.    end
  432.    if @last_mouse_y == nil
  433.      @last_mouse_y = mouse_y
  434.    end
  435.    check_pos = (@last_mouse_y - mouse_y).abs
  436.    if check_pos > 10
  437.      $game_system.se_play($data_system.cursor_se)
  438.      place = 10 ** (@digits_max - 1 - @index)
  439.      n = @number / place % 10
  440.      @number -= n * place
  441.      n = (n + 1) % 10 if mouse_y < @last_mouse_y
  442.      n = (n + 9) % 10 if mouse_y > @last_mouse_y
  443.      @number += n * place
  444.      refresh
  445.      @last_mouse_y = mouse_y
  446.    end
  447. end
  448. end
  449. class Scene_File
  450. if @self_alias == nil
  451.    alias self_update update
  452.    @self_alias = true
  453. end
  454. def update
  455.    mouse_x, mouse_y = Mouse.get_mouse_pos
  456.    Mouse.click_lock
  457.    idx = 0
  458.    for i in @savefile_windows
  459.      top_x = i.x + 16
  460.      top_y = i.y + 16
  461.      bottom_x = top_x + i.width
  462.      bottom_y = top_y + i.height
  463.      if (mouse_x > top_x) and (mouse_y > top_y) and
  464.         (mouse_x < bottom_x) and (mouse_y < bottom_y)
  465.        i.selected = true
  466.        if @file_index != idx
  467.          @file_index = idx
  468.          $game_system.se_play($data_system.cursor_se)
  469.        end            
  470.        Mouse.click_unlock
  471.      else
  472.        i.selected = false
  473.      end
  474.      idx += 1
  475.    end
  476.    self_update
  477. end
  478. end
  479. class Arrow_Enemy
  480. if @self_alias == nil
  481.    alias self_update update
  482.    @self_alias = true
  483. end
  484. def update
  485.    mouse_x, mouse_y = Mouse.get_mouse_pos
  486.    idx = 0
  487.    for i in $game_troop.enemies do
  488.      if i.exist?
  489.        top_x = i.screen_x - self.ox
  490.        top_y = i.screen_y - self.oy
  491.        bottom_x = top_x + self.src_rect.width
  492.        bottom_y = top_y + self.src_rect.height
  493.        if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
  494.           (mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
  495.          if @index != idx
  496.            $game_system.se_play($data_system.cursor_se)
  497.            @index = idx
  498.          end
  499.        end
  500.      end
  501.      idx += 1
  502.    end
  503.    self_update
  504. end
  505. end
  506. class Arrow_Actor
  507. if @self_alias == nil
  508.    alias self_update update
  509.    @self_alias = true
  510. end
  511. def update
  512.    mouse_x, mouse_y = Mouse.get_mouse_pos
  513.    idx = 0
  514.    for i in $game_party.actors do
  515.      if i.exist?
  516.        top_x = i.screen_x - self.ox
  517.        top_y = i.screen_y - self.oy
  518.        bottom_x = top_x + self.src_rect.width
  519.        bottom_y = top_y + self.src_rect.height
  520.        if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
  521.           (mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
  522.          if @index != idx
  523.            $game_system.se_play($data_system.cursor_se)
  524.            @index = idx
  525.          end
  526.        end
  527.      end
  528.      idx += 1
  529.    end
  530.    self_update
  531. end
  532. end
  533. #==============================================================================
  534. # ■ Game_Player
  535. #------------------------------------------------------------------------------
  536. #  处理主角的类。事件启动的判定、以及地图的滚动等功能。
  537. # 本类的实例请参考 $game_player。
  538. #   鼠标控制角色的主程序
  539. #==============================================================================
  540. class Game_Player
  541. if @self_alias == nil
  542.    alias self_update update
  543.    @self_alias = true
  544. end
  545. #--------------------------------------------------------------------------
  546. # ● 得到鼠标的状态
  547. #--------------------------------------------------------------------------
  548. def get_mouse_sta
  549.    return @mouse_sta
  550. end
  551. #--------------------------------------------------------------------------
  552. # ● 完整鼠标系统
  553. #--------------------------------------------------------------------------
  554. def update
  555.    mouse_x, mouse_y = Mouse.get_mouse_pos
  556.    @mtp_x = mouse_x
  557.    @mtp_y = mouse_y
  558.    unless $game_system.map_interpreter.running? and @mouse_sta == 2 #鼠标状态不为跟随状态
  559.      #得到鼠标图标方向
  560.      $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)
  561.    else
  562.      #令鼠标图标为正常
  563.      $mouse_icon_id = 0 if @mouse_sta != 2
  564.    end
  565.    
  566.    #单击鼠标时进行判断寻路或跟随
  567.    if Mouse.trigger?(Mouse::LEFT) #当点击鼠标时
  568.      unless $game_system.map_interpreter.running? or
  569.             @move_route_forcing or $game_temp.message_window_showing #各种无效情况的排除
  570.        #初始化
  571.        @mouse_sta = 1
  572.        p_direction = 5
  573.        #检查鼠标处能否开启事件
  574.        event_start,p_direction = $game_map.check_event_custom_start(mouse_x, mouse_y)
  575.        #若在移动中再次点击鼠标左键(即双击左键),则改鼠标状态为跟随状态
  576.        @mouse_sta = 2 if @paths_id != nil and @paths_id != @paths.size
  577.        if @mouse_sta != 2
  578.          #鼠标状态不为跟随状态则取数据并初始化路径
  579.          trg_x = (mouse_x + $game_map.display_x / 4) / 32
  580.          trg_y = (mouse_y + $game_map.display_y / 4) / 32
  581.          @paths = []
  582.          @paths_id = 0
  583.          if event_start == 0 #若不能开启事件
  584.            if trg_x != $game_player.x or trg_y != $game_player.y #若目标不为自身则开始寻路
  585.              find_path = Find_Path.new
  586.              @paths = find_path.find_player_short_path(trg_x, trg_y, @mtp_x, @mtp_y)
  587.            end
  588.          else #若能开启事件则改变角色朝向
  589.            @direction = p_direction
  590.          end
  591.        end
  592.      end
  593.    end
  594.    #开始移动
  595.    if @mouse_sta != nil and @mouse_sta == 1 #若鼠标状态为寻路状态
  596.      unless moving? or $game_system.map_interpreter.running? or
  597.             @move_route_forcing or $game_temp.message_window_showing #排除无效情况
  598.        if @paths_id != nil and @paths != nil and @paths_id <= @paths.size #若没有完成路径
  599.          case @paths[@paths_id] #判断路径
  600.          when 6
  601.            @last_move_x = true
  602.            move_right
  603.            @paths_id += 1
  604.            @direction = 6
  605.          when 4
  606.            @last_move_x = true
  607.            move_left
  608.            @paths_id += 1
  609.            @direction = 4
  610.          when 2
  611.            @last_move_x = false
  612.            move_down
  613.            @direction = 2
  614.            @paths_id += 1
  615.          when 8
  616.            @last_move_x = false
  617.            move_up
  618.            @direction = 8
  619.            @paths_id += 1
  620.          end
  621.        end
  622.      end
  623.    elsif @paths != nil and @mouse_sta == 2 #当鼠标状态为跟随,且在移动中
  624.      if Mouse.press?(Mouse::LEFT) #持续按住鼠标
  625.        unless moving? or $game_system.map_interpreter.running? or
  626.               @move_route_forcing or $game_temp.message_window_showing #排除无效情况
  627.          #跟随方向判断并跟随
  628.          if @mtp_x > self.screen_x
  629.            if @mtp_y - self.screen_y > - ( @mtp_x - self.screen_x ) and
  630.               @mtp_y - self.screen_y < @mtp_x - self.screen_x
  631.              move_right
  632.              $mouse_icon_id = 16
  633.              @direction = 6
  634.            end
  635.            if @mtp_y - self.screen_y > @mtp_x - self.screen_x
  636.              move_down
  637.              $mouse_icon_id = 12
  638.              @direction = 2
  639.            end
  640.            if @mtp_y - self.screen_y < - ( @mtp_x - self.screen_x )
  641.              move_up
  642.              $mouse_icon_id = 18
  643.              @direction = 8
  644.            end
  645.          end
  646.          if @mtp_x < self.screen_x
  647.            if @mtp_y - self.screen_y > - ( self.screen_x - @mtp_x ) and
  648.               @mtp_y - self.screen_y < self.screen_x - @mtp_x
  649.              move_left
  650.              $mouse_icon_id = 14
  651.              @direction = 4
  652.            end
  653.            if @mtp_y - self.screen_y > self.screen_x - @mtp_x
  654.              move_down
  655.              $mouse_icon_id = 12
  656.              @direction = 2
  657.            end
  658.            if @mtp_y - self.screen_y < - ( self.screen_x - @mtp_x )
  659.              move_up
  660.              $mouse_icon_id = 18
  661.              @direction = 8
  662.            end
  663.          end
  664.        end
  665.      else #没状态的情况
  666.        $mouse_icon_id = 0
  667.        @mouse_sta = 0
  668.        @paths_id = @paths.size #终止寻路移动
  669.      end
  670.    end
  671. self_update
  672. end
  673. end
  674. Mouse.init
  675. END { Mouse.exit }
  676. #==============================================================================
  677. # ■ Find_Path
  678. #------------------------------------------------------------------------------
  679. #  寻路算法--完整鼠标系统(四方向)专用版
  680. #   By whbm
  681. #==============================================================================
  682. class Find_Path
  683. #--------------------------------------------------------------------------
  684. def initialize  #初始化
  685. @open_list = []
  686. @close_lise = []
  687. @path = []
  688. end  #结束初始化
  689. #--------------------------------------------------------------------------
  690. def fp_passable?(x, y, d, tr_x = -2, tr_y = -2)  #开始判定通行
  691. return false if (tr_x == @unable_xa or
  692.                  tr_x == @unable_xb or
  693.                  tr_y == @unable_ya or
  694.                  tr_y == @unable_yb)
  695. if $game_player.passable?(x, y, d)
  696.    return true
  697. else
  698.    return false
  699. end
  700. end  #结束判定通行
  701. #--------------------------------------------------------------------------
  702. def get_g(now_point)  #开始计算G值
  703. d = now_point[2]
  704. return 0 if d == 5
  705. father_point = get_father_point(now_point)
  706. g = father_point[3] + 10
  707. return g
  708. end  #结束计算G值
  709. #--------------------------------------------------------------------------
  710. def get_h(now_point)  #开始计算H值
  711. now_x = now_point[0]
  712. now_y = now_point[1]
  713. #print @trg_x,now_x,@trg_y,now_y
  714. h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
  715. return h * 10
  716. end  #结束计算H值
  717. #--------------------------------------------------------------------------
  718. def get_f(now_point)  #开始计算F值
  719. f = now_point[3] + now_point[4]
  720. return f
  721. end  #结束计算F值
  722. #--------------------------------------------------------------------------
  723. def get_point(x, y) #取已知坐标点
  724. if @open_list.size != 0
  725.    @open_list.each do |point|
  726.      if point[0] == x and point[1] == y
  727.        return point
  728.        break
  729.      end
  730.    end
  731. end
  732. if @close_list.size != 0
  733.    @close_list.each do |point|
  734.      if point[0] == x and point[1] == y
  735.        return point
  736.        break
  737.      end
  738.    end
  739. end
  740. end  #结束取已知坐标点
  741. #--------------------------------------------------------------------------
  742. def get_father_point(now_point)  #取已知点的父节点
  743. d = now_point[2]
  744. return now_point if d == 5
  745. x = now_point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  746. y = now_point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  747. return get_point(x, y)
  748. end  #结束取已知点的父节点
  749. #--------------------------------------------------------------------------
  750. def new_point(x, y, d)  #开始建立新节点
  751. #print x,y,d
  752. point = [x, y, d]
  753. point.push get_g(point)
  754. point.push get_h(point)
  755. point.push get_f(point)
  756. return point
  757. end  #结束建立新节点
  758. #--------------------------------------------------------------------------
  759. def get_direction(self_x, self_y, trg_x, trg_y)
  760.   if trg_x > self_x
  761.     if trg_y - self_y > -  ( trg_x - self_x ) and
  762.       trg_y - self_y < ( trg_x - self_x )
  763.       return 6
  764.     end
  765.     if trg_y - self_y > ( trg_x - self_x )
  766.       return 2
  767.     end
  768.     if trg_y - self_y < - ( trg_x - self_x )
  769.       return 8
  770.     end
  771.   end
  772.   if trg_x < self_x
  773.     if trg_y - self_y > - ( self_x - trg_x ) and
  774.       trg_y - self_y < ( self_x - trg_x )
  775.       return 4
  776.     end
  777.     if trg_y - self_y > ( self_x - trg_x )
  778.       return 2
  779.     end
  780.     if trg_y - self_y < - ( self_x - trg_x )
  781.       return 8
  782.     end
  783.   end
  784. end
  785. #--------------------------------------------------------------------------
  786. def get_d_x_y(x, y, d)
  787.   d_x = x + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  788.   d_y = y + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  789.   return d_x, d_y
  790. end
  791. #--------------------------------------------------------------------------
  792. def find_short_path_other(self_x, self_y, trg_x, trg_y,
  793.                           real_self_x, real_self_y, real_trg_x, real_trg_y)
  794.   @self_x = self_x
  795.   @self_y = self_y
  796.   @now_x = self_x
  797.   @now_y = self_y
  798.   @trg_x = trg_x
  799.   @trg_y = trg_y
  800.   @path = []
  801.   direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
  802.   @now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
  803.   while fp_passable?(@now_x, @now_y, direction)
  804.     @path.push direction
  805.     @now_x = @now_trg_x
  806.     @now_y = @now_trg_y
  807.     @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
  808.   end
  809.   return @path
  810. end
  811. #--------------------------------------------------------------------------
  812. def find_short_path(self_x, self_y, trg_x, trg_y,
  813.                     real_self_x, real_self_y, real_trg_x, real_trg_y)  #开始搜索路径
  814.   
  815. return find_short_path_other(self_x, self_y, trg_x, trg_y,
  816.                               real_self_x, real_self_y, real_trg_x, real_trg_y) if not
  817.                   (fp_passable?(trg_x, trg_y + 1, 8) or
  818.                    fp_passable?(trg_x + 1, trg_y, 4) or
  819.                    fp_passable?(trg_x - 1, trg_y, 6) or
  820.                    fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
  821.                   
  822.                   
  823.   #根据屏幕限定搜索面积..加速
  824. @unable_xa = $game_map.display_x / 128 - 1
  825. @unable_ya = $game_map.display_y / 128 - 1
  826. @unable_xb = $game_map.display_x / 128 + 20
  827. @unable_yb = $game_map.display_y / 128 + 20
  828. @self_x = self_x
  829. @self_y = self_y
  830. @now_x = self_x
  831. @now_y = self_y
  832. @trg_x = trg_x
  833. @trg_y = trg_y
  834. @open_list = []
  835. @close_list = []
  836. #准备搜索
  837. #print @self_x,@self_y
  838. @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
  839. @open_list.push @now_point #将当前点加入关闭列表
  840. #开始搜索
  841. begin
  842. loop do
  843.    check_trg = check_around_point(@now_point)
  844.    if check_trg == true
  845.      @path = get_path
  846.      break
  847.    end
  848.    @now_point = get_lowest_f_point
  849.    if @now_point == [] or @now_point == nil
  850.      @path = []
  851.      break
  852.    end
  853. end
  854. rescue Hangup
  855.   retry
  856. end
  857. return @path
  858. end  #结束搜索路径
  859. #--------------------------------------------------------------------------
  860. def find_player_short_path(trg_x, trg_y,
  861.                            real_trg_x, real_trg_y)  #寻找角色的最短路径
  862. self_x = $game_player.x
  863. self_y = $game_player.y
  864. real_self_x = $game_player.screen_x
  865. real_self_y = $game_player.screen_y
  866. @goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
  867. if @goal_type == 1
  868.    trg_x = event.x
  869.    trg_y = event.y
  870. end
  871. return find_short_path(self_x, self_y, trg_x, trg_y,
  872.                         real_self_x, real_self_y, real_trg_x, real_trg_y)
  873. end  #结束角色的寻找路径
  874. #--------------------------------------------------------------------------
  875. def get_path  #取得最终的路径
  876. path = []
  877. now_point = @open_list[@open_list.size - 1]
  878. path.push(10 - now_point[2])
  879. last_point = now_point
  880. loop do
  881.    now_point = get_father_point(now_point)
  882.    break if now_point[2] == 5
  883.    path.push(10 - now_point[2])
  884. end
  885. return path.reverse
  886. end  #结束取得最终的路径
  887. #--------------------------------------------------------------------------
  888. def get_lowest_f_point  #开始取得最低F值的点
  889. if @open_list == []
  890.    return []
  891. end
  892. last_lowest_f_point = @open_list[0]
  893. @open_list.each do |point|
  894.    last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
  895. end
  896. return last_lowest_f_point
  897. end  #结束取得最低F值点
  898. #--------------------------------------------------------------------------
  899. def check_around_point(point)  #开始检查已知点的八方节点
  900. for d in [2, 4, 6, 8]
  901.    x = point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  902.    y = point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  903.    if in_close_list?(x, y) #在关闭列表中
  904.      next
  905.    elsif in_open_list?(x, y) #在开启列表中
  906.      get_new_g_point = new_point(x, y, 10 - d)
  907.      get_last_g_point = get_point(x, y)
  908.      if get_new_g_point[3] >= get_last_g_point[3]
  909.        next
  910.      else
  911.        #如果改变父节点是新G值更小则确定改变
  912.        @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
  913.      end
  914.    else
  915.      if fp_passable?(point[0], point[1], d, x, y)
  916.        # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
  917.        @open_list.push new_point(x, y, 10 - d)
  918.        #如果将目标点添加到了开启列表中就返回true
  919.        return true if x == @trg_x and y == @trg_y
  920.        return true if @goal_type == 1 and ([1, -1].include?(x - @trg_x) and y - @trg_y == 0) or ([1, -1].include?(y - @trg_y) and x - @trg_x == 0)
  921.      end
  922.    end
  923. end
  924. #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
  925. @close_list.push point
  926. @open_list.delete(point)
  927. #此刻没找到目标点并返回false
  928. return false
  929. end  #结束计算已知点的八方节点
  930. #--------------------------------------------------------------------------
  931. def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
  932. @open_list.each do |point|
  933.    return true if point[0] == x and point[1] == y
  934. end
  935. return false
  936. end  #结束检查谋点是否在开启列表中
  937. #--------------------------------------------------------------------------
  938. def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
  939. @close_list.each do |point|
  940.    return true if point[0] == x and point[1] == y
  941. end
  942. return false
  943. end  #结束检查谋点是否在关闭列表中
  944. #--------------------------------------------------------------------------
  945. end
复制代码
回复 支持 反对

使用道具 举报

1

主题

11

帖子

89

积分

②入门

积分
89
 楼主| 发表于 2010-1-20 16:24:46 | 显示全部楼层
这个就使那个改的。。
回复 支持 反对

使用道具 举报

38

主题

129

帖子

1151

积分

⑥精研

无既空,空既色

积分
1151
发表于 2010-1-20 16:25:47 | 显示全部楼层
你用的其他脚本用冲突·
回复 支持 反对

使用道具 举报

1

主题

11

帖子

89

积分

②入门

积分
89
 楼主| 发表于 2010-1-20 16:30:57 | 显示全部楼层
那我再试试吧
回复 支持 反对

使用道具 举报

1

主题

11

帖子

89

积分

②入门

积分
89
 楼主| 发表于 2010-1-20 16:35:46 | 显示全部楼层
是脚本的问题。。。
我新建了一个,也是一样的问题
回复 支持 反对

使用道具 举报

38

主题

129

帖子

1151

积分

⑥精研

无既空,空既色

积分
1151
发表于 2010-1-20 16:51:37 | 显示全部楼层
对了你684行那里有一个和字出来了把它删了再看下
回复 支持 反对

使用道具 举报

1

主题

11

帖子

89

积分

②入门

积分
89
 楼主| 发表于 2010-1-20 18:28:35 | 显示全部楼层
还是不行。。楼上的JJ你建个NPC试试那个脚本。。。貌似就不行了
回复 支持 反对

使用道具 举报

38

主题

129

帖子

1151

积分

⑥精研

无既空,空既色

积分
1151
发表于 2010-1-20 18:45:38 | 显示全部楼层
你把工程给我看下可以不,我现在就是用这个的没问题啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-5-15 04:44 , Processed in 0.026487 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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