try(destroyDialog autoFinderRoll)catch()
rollout autoFinderRoll "遍历寻路"
(
button btnCreateScene "创建场景" width:120
button btnSetGround "设置地面[灰色]" width:120
button btnSetWall "设置墙体[黑色]" width:120
button btnSetPlayer "设置玩家[黄色]" width:120
button btnSetTarget "设置目标[蓝色]" width:120
button btnStartFind "开始寻路" width:120
button btnReset "重置-路径[绿色]" width:120
button btnResetDeadPath "重置-死路[红色]" width:120
button btnCreateSceneRandom "创建随机场景运行" width:140
button btnRandomBoom "随机爆炸一次" width:140
local _playerObject
local _targetObject
local groundColor = gray
local wallColor = black
local playerColor = yellow
local targetColor = blue
local pathColor = green
local errorColor = red
on btnCreateScene pressed do
(
delete objects
xCount = 12
yCount = 12
size = 10
for x = 1 to xCount do
(
for y = 1 to yCount do
(
objName = x as string + "_" + y as string
p = Plane length:size width:size widthsegs:1 lengthsegs:1 name:objName
p.pos = [x * size, y * size, 0]
p.wirecolor = groundColor
--锁住物体不让手动移动
setTransformLockFlags p #all
)
)
max select
)
fn getNode objName =
(
o = getNodeByName objName
--if o != undefined do select o
return o
)
--在物体周边2个点中,获取可用的物体
fn getNearOkObjects curObj =
(
xy = FilterString curObj.name "_"
x = xy[1] as integer
y = xy[2] as integer
p1 = getNode ((x - 1) as string + "_" + (y + 1) as string)
p2 = getNode ((x - 0) as string + "_" + (y + 1) as string)
p3 = getNode ((x + 0) as string + "_" + (y + 1) as string)
p4 = getNode ((x - 1) as string + "_" + (y + 0) as string)
p6 = getNode ((x + 1) as string + "_" + (y + 0) as string)
p7 = getNode ((x - 1) as string + "_" + (y - 0) as string)
p8 = getNode ((x - 0) as string + "_" + (y - 1) as string)
p9 = getNode ((x + 1) as string + "_" + (y - 1) as string)
arr = #(p1,p2,p3,p4,p6,p7,p8,p9)
reult = #()
for i in arr do
(
if i == undefined do continue
if i.wireColor == playerColor do continue --不能是自己
if i.wireColor == errorColor do continue --死路
if i.wireColor == wallColor do continue --墙体
if i.wireColor == pathColor do continue --走过的
append reult i
)
return reult
)
on btnSetGround pressed do for i in selection do i.wirecolor = groundColor
on btnSetWall pressed do for i in selection do i.wirecolor = wallColor
on btnSetPlayer pressed do
(
for i in objects do if i.wirecolor == playerColor do i.wirecolor = groundColor
if selection[1] != undefined do
(
selection[1].wirecolor = playerColor
_playerObject = selection[1]
)
)
on btnSetTarget pressed do
(
for i in objects do if i.wirecolor == targetColor do i.wirecolor = groundColor
if selection[1] != undefined do
(
selection[1].wirecolor = targetColor
_targetObject = selection[1]
)
)
fn find =
(
times = 0
local _player = _playerObject
local _target = _targetObject
local state = #init
while true do
(
dis = distance _player _target
if dis < 20 do exit
times += 1
if times > 2000 do
(
state = #timeout
exit
)
--
nearObjs = getNearOkObjects _player
tmpDis = #()
for i in nearObjs do
(
dis = distance i _target
--print ("最近物体: " + i as string)
append tmpDis dis
)
if tmpDis.count == 0 do
(
print "死路,周围无可行走的点"
if times == 1 then
(
--1次表示绝路
state = #over
)
else
(
_player.wireColor = errorColor
state = #dead
)
exit
)
minDis = amin tmpDis
index = findItem tmpDis minDis
--找到了一个最短物体,玩家就变成了他
if index != 0 then
(
_player = nearObjs[index]
_player.wireColor = pathColor
--
sleep 0.001
windows.processPostedMessages()
redrawViews()
)
state = #ok
)
print ("循环次数:" + times as string)
if times == 0 then state = #zeroTimes
return state
)
on btnStartFind pressed do
(
for i = 1 to 500 do
(
if keyboard.escPressed do exit
btnReset.pressed()
state = find()
if state == #zeroTimes do exit
if state == #timeout do exit
if state == #ok do exit
if state == #over do exit
)
)
on btnResetDeadPath pressed do
(
for i in objects do if i.wireColor == errorColor do
i.wireColor = groundColor
)
on btnReset pressed do
(
for i in objects do
(
if i.wireColor == pathColor do i.wireColor = groundColor
if i.wireColor == playerColor do _playerObject = i
if i.wireColor == targetColor do _targetObject = i
)
try _targetObject.wireColor = targetColor catch()
)
on btnCreateSceneRandom pressed do
(
btnCreateSceneRandom.enabled = false
btnCreateScene.pressed()
for i in objects do
(
if random 1 2 == 1 then i.wirecolor = wallColor
else if random 1 3 == 1 then i.wirecolor = groundColor
)
while true do
(
p = objects[random 1 objects.count]
t = objects[random 1 objects.count]
if p != t do
(
_playerObject = p
_targetObject = t
_playerObject.wirecolor = playerColor
_targetObject.wirecolor = targetColor
exit
)
)
btnStartFind.pressed()
btnCreateSceneRandom.enabled = true
)
on btnRandomBoom pressed do
(
while true do
(
obj = objects[random 1 objects.count]
if obj.wireColor == wallColor or obj.wireColor == groundColor do
(
print ("爆炸:" + obj as string)
obj.wireColor = groundColor
--
s = Sphere radius:6 pos:obj.pos
sleep 0.1
windows.processPostedMessages()
redrawViews()
delete s
exit
)
)
)
)
createDialog autoFinderRoll 200 280
标签:do,string,maxscript,wirecolor,--,wireColor,自动,pressed,寻路
From: https://www.cnblogs.com/trykle/p/16772247.html