赞 | 0 |
VIP | 9 |
好人卡 | 0 |
积分 | 1 |
经验 | 18278 |
最后登录 | 2018-5-1 |
在线时间 | 210 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 85
- 在线时间
- 210 小时
- 注册时间
- 2013-7-26
- 帖子
- 346
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
@myownroc M君大神
终止命令是什么? 自动寻路的过程中 中途停止 。另外我用的是鼠标系统+八方向
写了个小地图系统 排得上用场,虽然搜索到了小地图寻路,不过那个太卡了,CPU暴涨,M君这个不错。
谢谢了- #寻路 调用方法 BFS.find_path($game_player,x,y)
- module BFS
- def self.find_path(character, tx, ty)
- ox, oy = character.x, character.y
- return if (tx > $game_map.width - 1) || (ty > $game_map.height - 1)
- return if !($game_map.passable?(tx, ty, 0, character))
- return if (tx == ox) && (ty == oy)
- checked = []
- father = []
- for i in 0...$game_map.width
- checked << []
- father << []
- for j in 0...$game_map.height
- checked[-1] << 0
- father[-1] << nil
- end
- end
- path = []
- found = false
- checked[ox][oy] = 1
- empty = false
- loop do
- empty = true
- for i in 0...$game_map.width
- for j in 0...$game_map.height
- if checked[i][j] == 1
- checked[i][j] = 2
- if $game_map.passable?(i, j + 1, 2, character) &&
- checked[i][j + 1] == 0
- checked[i][j + 1] = 1
- father[i][j + 1] = [i, j]
- end
- if $game_map.passable?(i - 1, j, 4, character) &&
- checked[i - 1][j] == 0
- checked[i - 1][j] = 1
- father[i - 1][j] = [i, j]
- end
- if $game_map.passable?(i, j - 1, 8, character) &&
- checked[i][j - 1] == 0
- checked[i][j - 1] = 1
- father[i][j - 1] = [i, j]
- end
- if $game_map.passable?(i + 1, j, 6, character) &&
- checked[i + 1][j] == 0
- checked[i + 1][j] = 1
- father[i + 1][j] = [i, j]
- end
- end
- if checked[tx][ty] == 1
- found = true
- break
- end
- end
- end
- for i in 0...$game_map.width
- empty = false if checked[i].include?(1)
- end
- if found || empty
- break
- end
- end
- return if !found
- if found
- x, y = tx, ty
- while [x, y] != [ox, oy]
- dx, dy = father[x][y][0] - x, father[x][y][1] - y
- step = 2 if dy == -1
- step = 4 if dx == 1
- step = 8 if dy == 1
- step = 6 if dx == -1
- path << step
- x, y = father[x][y][0], father[x][y][1]
- end
- path.reverse!
- end
- list = []
- path.each{|e|
- code = 1 if e == 2
- code = 2 if e == 4
- code = 4 if e == 8
- code = 3 if e == 6
- list << RPG::MoveCommand.new(code)
- }
- list << RPG::MoveCommand.new(0)
- skippable = false
- repeat = false
- commands = RPG::MoveRoute.new
- commands.skippable = skippable
- commands.repeat = repeat
- commands.list = list
- if character == nil
- return true
- end
- character.force_move_route(commands)
- return true
- end
- end
复制代码 |
|