local SELECTOR =
local SEQUENCE =
local CONDITION =
local ACTION = local function Traverse(node, ...)
local t = node.type
if t == SELECTOR then
for i=, #node do
if Traverse(node[i], ...) then
return true
end
end
return false
elseif t == SEQUENCE then
for i=, #node do
if not Traverse(node[i], ...) then
return false
end
end
return true
elseif t == CONDITION then
for i=, #node do
if not node[i](...) then
return false
end
end
return true
elseif t == ACTION then
for i=, #node do
node[i](...)
end
return true
end
end local root =
{
type = SELECTOR,
{
type = SEQUENCE,
{
type = CONDITION,
function(i,j) return math.random() > i end,
function(i,j) return math.random() < j end,
},
{
type = ACTION,
function() print("random") end,
},
},
{
type = ACTION,
function() print("idle") end,
},
} local input1 = 0.2
local input2 = 0.7
Traverse(root, input1, input2)
有没有比 C++ 代码简单一万倍,有没有?
https://github.com/hangj/BehaviorTree