加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 sky40 于 2014-7-8 20:42 编辑
基于八方寻路脚本的 事件寻路功能: 指定事件,从最近节点开始,依次走过目标(x,y)。
第20行的 通行判断应该怎么写(目的是 防止节点上有事件 造成卡死)
# 使用方法 直接调用脚本 # $goto_id = 事件id # $goto_xy = [[目标x,目标y],[目标x,目标y],[目标x,目标y]...] # Reset_goto.new class Reset_goto def initialize x = $game_map.events[$goto_id].x y = $game_map.events[$goto_id].y @path = [] #移除不可通行的节点 a =[] for i in 0...$goto_xy.size xx = $goto_xy[i][0] yy = $goto_xy[i][1] if !$game_player.passable?(xx, yy, 4) a.push $goto_xy[i] end end $goto_xy -= a if $goto_xy != [] #寻找最近节点 j = (x-$goto_xy[0][0]).abs + (y-$goto_xy[0][1]).abs k = 0 for i in 1...$goto_xy.size if j >= (x-$goto_xy[i][0]).abs + (y-$goto_xy[i][1]).abs or j == 0 j = (x-$goto_xy[i][0]).abs + (y-$goto_xy[i][1]).abs k = i end end #计算预设路线 if j != 0 @path += Find_Path.new.find_short_path(x,y,$goto_xy[k][0],$goto_xy[k][1]) for i in k...$goto_xy.size-1 @path += Find_Path.new.find_short_path($goto_xy[i][0],$goto_xy[i][1],$goto_xy[i+1][0],$goto_xy[i+1][1]) end end end if @path != [] move end end #开始预设路径行走 def move turns = [nil,5,1,6,2,nil,3,7,4,8] m = RPG::MoveRoute.new m.list[0].code = turns[@path[0]] for i in [email]1...@path.size[/email] j = turns[@path[i]] m.list.push(RPG::MoveCommand.new(j)) end m.list.push(RPG::MoveCommand.new(0)) m.repeat = false $game_map.events[$goto_id].force_move_route(m) end end
# 使用方法 直接调用脚本
# $goto_id = 事件id
# $goto_xy = [[目标x,目标y],[目标x,目标y],[目标x,目标y]...]
# Reset_goto.new
class Reset_goto
def initialize
x = $game_map.events[$goto_id].x
y = $game_map.events[$goto_id].y
@path = []
#移除不可通行的节点
a =[]
for i in 0...$goto_xy.size
xx = $goto_xy[i][0]
yy = $goto_xy[i][1]
if !$game_player.passable?(xx, yy, 4)
a.push $goto_xy[i]
end
end
$goto_xy -= a
if $goto_xy != []
#寻找最近节点
j = (x-$goto_xy[0][0]).abs + (y-$goto_xy[0][1]).abs
k = 0
for i in 1...$goto_xy.size
if j >= (x-$goto_xy[i][0]).abs + (y-$goto_xy[i][1]).abs or j == 0
j = (x-$goto_xy[i][0]).abs + (y-$goto_xy[i][1]).abs
k = i
end
end
#计算预设路线
if j != 0
@path += Find_Path.new.find_short_path(x,y,$goto_xy[k][0],$goto_xy[k][1])
for i in k...$goto_xy.size-1
@path += Find_Path.new.find_short_path($goto_xy[i][0],$goto_xy[i][1],$goto_xy[i+1][0],$goto_xy[i+1][1])
end
end
end
if @path != []
move
end
end
#开始预设路径行走
def move
turns = [nil,5,1,6,2,nil,3,7,4,8]
m = RPG::MoveRoute.new
m.list[0].code = turns[@path[0]]
for i in [email]1...@path.size[/email]
j = turns[@path[i]]
m.list.push(RPG::MoveCommand.new(j))
end
m.list.push(RPG::MoveCommand.new(0))
m.repeat = false
$game_map.events[$goto_id].force_move_route(m)
end
end
|