不要直接以步数为半径生成移动范围,去尝试每条可行路径就行。
下面是我很久以前写的战旗获取可行动范围的方法,有点不堪入目,但是应该能看懂意思
楼主的需求需要再定义个行动力,以及地形对行动力的影响
module SLG_Module Node = Struct.new(:x,:y,:mp) def self.get_movable_area(event,maxp) cutflag=0 offx = [-1, 0, 1, 0] offy = [0, 1, 0, -1] pMap = Table.new($game_map.width,$game_map.height) temp = Node.new(event.x,event.y,maxp) rets = [] retable = Table.new($game_map.width,$game_map.height) retable[temp.x,temp.y] = 1 allpath = [temp.clone] while cutflag<allpath.size n=allpath.size i = cutflag while i < allpath.size for j in 0...4 temp.x=allpath[i].x+offx[j] temp.y=allpath[i].y+offy[j] temp.mp=allpath[i].mp-1 next if temp.mp<0 next unless event.passable?(temp.x,temp.y,0) next if pMap[temp.x,temp.y]>0 allpath << temp.clone rets << Point.new(temp.x,temp.y) retable[temp.x,temp.y] = 1 end pMap[allpath[i].x,allpath[i].y] = 1 i+=1 end cutflag=n end bit = SLG_Area.new bit.set_data(rets) return [bit,retable] end end
module SLG_Module
Node = Struct.new(:x,:y,:mp)
def self.get_movable_area(event,maxp)
cutflag=0
offx = [-1, 0, 1, 0]
offy = [0, 1, 0, -1]
pMap = Table.new($game_map.width,$game_map.height)
temp = Node.new(event.x,event.y,maxp)
rets = []
retable = Table.new($game_map.width,$game_map.height)
retable[temp.x,temp.y] = 1
allpath = [temp.clone]
while cutflag<allpath.size
n=allpath.size
i = cutflag
while i < allpath.size
for j in 0...4
temp.x=allpath[i].x+offx[j]
temp.y=allpath[i].y+offy[j]
temp.mp=allpath[i].mp-1
next if temp.mp<0
next unless event.passable?(temp.x,temp.y,0)
next if pMap[temp.x,temp.y]>0
allpath << temp.clone
rets << Point.new(temp.x,temp.y)
retable[temp.x,temp.y] = 1
end
pMap[allpath[i].x,allpath[i].y] = 1
i+=1
end
cutflag=n
end
bit = SLG_Area.new
bit.set_data(rets)
return [bit,retable]
end
end
|