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

Project1

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

[已经过期] 如何让事件自动移动到作者想移动到的地方而且还不撞墙呢

[复制链接]

Lv1.梦旅人

剑仙·影羽

梦石
0
星屑
172
在线时间
224 小时
注册时间
2010-3-20
帖子
1580
跳转到指定楼层
1
发表于 2010-3-20 20:07:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
如何让事件自动移动到作者想移动到的地方而且还不撞墙呢

Lv2.观梦者 (管理员)

八云紫的式神

梦石
0
星屑
549
在线时间
1243 小时
注册时间
2008-1-1
帖子
4282

烫烫烫

2
发表于 2010-3-20 21:06:34 | 只看该作者
遇到墙传过还是绕道?
传过的话用自动穿透,绕道得写寻路算法……
rm for linux(wine)制作中,期待夏娜SAMA能实现到webrm上
回复 支持 反对

使用道具 举报

Lv1.梦旅人

剑仙·影羽

梦石
0
星屑
172
在线时间
224 小时
注册时间
2010-3-20
帖子
1580
3
 楼主| 发表于 2010-3-21 08:28:48 | 只看该作者
绕道
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
270 小时
注册时间
2010-2-4
帖子
1305
4
发表于 2010-3-21 09:07:34 | 只看该作者
回复 3# 火ZHI意志
手头正好有一个囧囧的a*算法,不过老爷机的话建议别用...额额
作者:whbm   出自:某游戏中抠的,具体忘了
  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 $game_player.passable?(x, y, d)
  21.    return true
  22. else
  23.    return false
  24. end
  25. end  #结束判定通行
  26. #--------------------------------------------------------------------------
  27. def get_g(now_point)  #开始计算G值
  28. d = now_point[2]
  29. return 0 if d == 5
  30. father_point = get_father_point(now_point)
  31. g = father_point[3] + 10
  32. return g
  33. end  #结束计算G值
  34. #--------------------------------------------------------------------------
  35. def get_h(now_point)  #开始计算H值
  36. now_x = now_point[0]
  37. now_y = now_point[1]
  38. #print @trg_x,now_x,@trg_y,now_y
  39. h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
  40. return h * 10
  41. end  #结束计算H值
  42. #--------------------------------------------------------------------------
  43. def get_f(now_point)  #开始计算F值
  44. f = now_point[3] + now_point[4]
  45. return f
  46. end  #结束计算F值
  47. #--------------------------------------------------------------------------
  48. def get_point(x, y) #取已知坐标点
  49. if @open_list.size != 0
  50.    @open_list.each do |point|
  51.      if point[0] == x and point[1] == y
  52.        return point
  53.        break
  54.      end
  55.    end
  56. end
  57. if @close_list.size != 0
  58.    @close_list.each do |point|
  59.      if point[0] == x and point[1] == y
  60.        return point
  61.        break
  62.      end
  63.    end
  64. end
  65. end  #结束取已知坐标点
  66. #--------------------------------------------------------------------------
  67. def get_father_point(now_point)  #取已知点的父节点
  68. d = now_point[2]
  69. return now_point if d == 5
  70. x = now_point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  71. y = now_point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  72. return get_point(x, y)
  73. end  #结束取已知点的父节点
  74. #--------------------------------------------------------------------------
  75. def new_point(x, y, d)  #开始建立新节点
  76. #print x,y,d
  77. point = [x, y, d]
  78. point.push get_g(point)
  79. point.push get_h(point)
  80. point.push get_f(point)
  81. return point
  82. end  #结束建立新节点
  83. #--------------------------------------------------------------------------
  84. def get_direction(self_x, self_y, trg_x, trg_y)
  85.   if trg_x > self_x
  86.     if trg_y - self_y > -  ( trg_x - self_x ) and
  87.       trg_y - self_y < ( trg_x - self_x )
  88.       return 6
  89.     end
  90.     if trg_y - self_y > ( trg_x - self_x )
  91.       return 2
  92.     end
  93.     if trg_y - self_y < - ( trg_x - self_x )
  94.       return 8
  95.     end
  96.   end
  97.   if trg_x < self_x
  98.     if trg_y - self_y > - ( self_x - trg_x ) and
  99.       trg_y - self_y < ( self_x - trg_x )
  100.       return 4
  101.     end
  102.     if trg_y - self_y > ( self_x - trg_x )
  103.       return 2
  104.     end
  105.     if trg_y - self_y < - ( self_x - trg_x )
  106.       return 8
  107.     end
  108.   end
  109. end
  110. #--------------------------------------------------------------------------
  111. def get_d_x_y(x, y, d)
  112.   d_x = x + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  113.   d_y = y + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  114.   return d_x, d_y
  115. end
  116. #--------------------------------------------------------------------------
  117. def find_short_path_other(self_x, self_y, trg_x, trg_y,
  118.                           real_self_x, real_self_y, real_trg_x, real_trg_y)
  119.   @self_x = self_x
  120.   @self_y = self_y
  121.   @now_x = self_x
  122.   @now_y = self_y
  123.   @trg_x = trg_x
  124.   @trg_y = trg_y
  125.   @path = []
  126.   direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
  127.   @now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
  128.   while fp_passable?(@now_x, @now_y, direction)
  129.     @path.push direction
  130.     @now_x = @now_trg_x
  131.     @now_y = @now_trg_y
  132.     @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
  133.   end
  134.   return @path
  135. end
  136. #--------------------------------------------------------------------------
  137. def find_short_path(self_x, self_y, trg_x, trg_y,
  138.                     real_self_x, real_self_y, real_trg_x, real_trg_y)  #开始搜索路径
  139.   
  140. return find_short_path_other(self_x, self_y, trg_x, trg_y,
  141.                               real_self_x, real_self_y, real_trg_x, real_trg_y) if not
  142.                   (fp_passable?(trg_x, trg_y + 1, 8) or
  143.                    fp_passable?(trg_x + 1, trg_y, 4) or
  144.                    fp_passable?(trg_x - 1, trg_y, 6) or
  145.                    fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
  146.                   
  147.                   
  148.   #根据屏幕限定搜索面积..加速
  149. @unable_xa = $game_map.display_x / 128 - 1
  150. @unable_ya = $game_map.display_y / 128 - 1
  151. @unable_xb = $game_map.display_x / 128 + 20
  152. @unable_yb = $game_map.display_y / 128 + 20


  153. @self_x = self_x
  154. @self_y = self_y
  155. @now_x = self_x
  156. @now_y = self_y
  157. @trg_x = trg_x
  158. @trg_y = trg_y
  159. @open_list = []
  160. @close_list = []
  161. #准备搜索
  162. #print @self_x,@self_y
  163. @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
  164. @open_list.push @now_point #将当前点加入关闭列表
  165. #开始搜索
  166. begin
  167. loop do
  168.    check_trg = check_around_point(@now_point)
  169.    if check_trg == true
  170.      @path = get_path
  171.      break
  172.    end
  173.    @now_point = get_lowest_f_point
  174.    if @now_point == [] or @now_point == nil
  175.      @path = []
  176.      break
  177.    end
  178. end
  179. rescue Hangup
  180.   retry
  181. end
  182. return @path
  183. end  #结束搜索路径
  184. #--------------------------------------------------------------------------
  185. def find_player_short_path(trg_x, trg_y,
  186.                            real_trg_x, real_trg_y)  #寻找角色的最短路径
  187. self_x = $game_player.x
  188. self_y = $game_player.y
  189. real_self_x = $game_player.screen_x
  190. real_self_y = $game_player.screen_y
  191. @goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
  192. if @goal_type == 1
  193.    trg_x = event.x
  194.    trg_y = event.y
  195. end
  196. return find_short_path(self_x, self_y, trg_x, trg_y,
  197.                         real_self_x, real_self_y, real_trg_x, real_trg_y)
  198. end  #结束角色的寻找路径
  199. #--------------------------------------------------------------------------
  200. def get_path  #取得最终的路径
  201. path = []
  202. now_point = @open_list[@open_list.size - 1]
  203. path.push(10 - now_point[2])
  204. last_point = now_point
  205. loop do
  206.    now_point = get_father_point(now_point)
  207.    break if now_point[2] == 5
  208.    path.push(10 - now_point[2])
  209. end
  210. return path.reverse
  211. end  #结束取得最终的路径
  212. #--------------------------------------------------------------------------
  213. def get_lowest_f_point  #开始取得最低F值的点
  214. if @open_list == []
  215.    return []
  216. end
  217. last_lowest_f_point = @open_list[0]
  218. @open_list.each do |point|
  219.    last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
  220. end
  221. return last_lowest_f_point
  222. end  #结束取得最低F值点
  223. #--------------------------------------------------------------------------
  224. def check_around_point(point)  #开始检查已知点的八方节点
  225. for d in [2, 4, 6, 8]
  226.    x = point[0] + (d == 6 ? 1 : (d == 4 ? -1 : 0))
  227.    y = point[1] + (d == 2 ? 1 : (d == 8 ? -1 : 0))
  228.    if in_close_list?(x, y) #在关闭列表中
  229.      next
  230.    elsif in_open_list?(x, y) #在开启列表中
  231.      get_new_g_point = new_point(x, y, 10 - d)
  232.      get_last_g_point = get_point(x, y)
  233.      if get_new_g_point[3] >= get_last_g_point[3]
  234.        next
  235.      else
  236.        #如果改变父节点是新G值更小则确定改变
  237.        @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
  238.      end
  239.    else
  240.      if fp_passable?(point[0], point[1], d, x, y)
  241.        # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
  242.        @open_list.push new_point(x, y, 10 - d)
  243.        #如果将目标点添加到了开启列表中就返回true
  244.        return true if x == @trg_x and y == @trg_y
  245.        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)
  246.      end
  247.    end
  248. end
  249. #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
  250. @close_list.push point
  251. @open_list.delete(point)
  252. #此刻没找到目标点并返回false
  253. return false
  254. end  #结束计算已知点的八方节点
  255. #--------------------------------------------------------------------------
  256. def in_open_list?(x, y)  #开始检查谋点是否在开启列表中
  257. @open_list.each do |point|
  258.    return true if point[0] == x and point[1] == y
  259. end
  260. return false
  261. end  #结束检查谋点是否在开启列表中
  262. #--------------------------------------------------------------------------
  263. def in_close_list?(x, y)  #开始检查谋点是否在关闭列表中
  264. @close_list.each do |point|
  265.    return true if point[0] == x and point[1] == y
  266. end
  267. return false
  268. end  #结束检查谋点是否在关闭列表中
  269. #--------------------------------------------------------------------------
  270. end
复制代码

评分

参与人数 1星屑 +200 收起 理由
「旅」 + 200 奖励~

查看全部评分

回复 支持 反对

使用道具 举报

Lv1.梦旅人

剑仙·影羽

梦石
0
星屑
172
在线时间
224 小时
注册时间
2010-3-20
帖子
1580
5
 楼主| 发表于 2010-3-21 12:10:11 | 只看该作者
虽然很感谢,但是我还是得问这到底要怎么用呢?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-4-28 10:27

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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