之前运行的行为树,都是一颗总树,那么实际上会有很多的总树,因此需要对行为树进行管理。

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end function this.OnUpdate()
local count = ; for i=,#this.trees do
local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

说明:

1.因为是运行在Sublime环境下的,所以这里使用while循环模拟每帧调用

2.关于AddTree和OnUpdate是否会冲突的问题。AddTree会增加trees的长度,但是在lua中for的三个表达式在循环开始前一次性求值,以后不再进行求值。比如#this.trees这个表达式只会在循环开始前执行一次,其结果用在后面的循环中。也就是说,如果在for循环过程中执行了AddTree方法,新增的元素会添加在后面,不会影响for循环的。

测试:

1.多行为树执行

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local parallel = BTParallel:New();
local action = self:GetBTActionUniversal();
local action2 = self:GetBTActionUniversal2(); self:SetStartTask(parallel); parallel:AddChild(action);
parallel:AddChild(action2);
end function this:GetBTActionUniversal()
local count = ;
local a = function ()
if (count <= ) then
count = count + ;
print("");
return BTTaskStatus.Running;
else
return BTTaskStatus.Success;
end
end
local universal = BTActionUniversal:New(nil, a);
return universal;
end function this:GetBTActionUniversal2()
local universal = BTActionUniversal:New(nil, function ()
return BTTaskStatus.Success;
end);
return universal;
end

TestBehaviorTree2.lua

 TestBehaviorTree2 = BTBehaviorTree:New();

 local this = TestBehaviorTree2;
this.name = "TestBehaviorTree2"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local sequence = BTSequence:New();
local log = BTLog:New("This is a other tree!!!");
local log2 = BTLog:New("This is a other tree 2!!!"); self:SetStartTask(repeater); repeater:AddChild(sequence); sequence:AddChild(log);
sequence:AddChild(log2);
end

TestMain.lua

 require "BehaviorTree/Core/Init"
require "BehaviorTree/Test/TestBehaviorTree"
require "BehaviorTree/Test/TestBehaviorTree2" local tree = TestBehaviorTree:New();
local tree2 = TestBehaviorTree2:New();
tree.name = "tree";
tree2.name = "tree2";
BTBehaviorManager.AddTree(tree);
BTBehaviorManager.AddTree(tree2);
BTBehaviorManager.RunTree();

输出如下:

[Unity插件]Lua行为树(十二):行为树管理-LMLPHP

2.for循环中添加行为树

TestBehaviorTree3.lua

 TestBehaviorTree3 = BTBehaviorTree:New();

 local this = TestBehaviorTree3;
this.name = "TestBehaviorTree3"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local log = BTLog:New("This is TestBehaviorTree3 tree!!!"); self:SetStartTask(repeater); repeater:AddChild(log);
end

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end local temp = false; function this.OnUpdate()
local count = ; for i=,#this.trees do
if (not temp) then
temp = true;
require "BehaviorTree/Test/TestBehaviorTree3"
local tree3 = TestBehaviorTree3:New();
tree3.name = "tree3";
this.AddTree(tree3);
end local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end print("------------------------------"); if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

输出如下。可以看到,在第一帧时,添加了行为树tree3,但是并不影响tree和tree2的执行,而在第二帧以及以后的帧时,tree3也开始执行了。

[Unity插件]Lua行为树(十二):行为树管理-LMLPHP

05-06 13:43