[Unity插件]Lua行为树(七):行为树嵌套

时间:2021-02-27 04:29:15

在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树。

以下面这棵行为树为例:

[Unity插件]Lua行为树(七):行为树嵌套

TestBehaviorTree2.lua

 TestBehaviorTree2 = BTBehaviorTree:New();

 local this = TestBehaviorTree2;
this.name = "TestBehaviorTree2"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
self: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

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
self:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local selector = BTSelector:New();
local sequence = BTSequence:New();
local isNullOrEmpty = BTIsNullOrEmpty:New("");
local log = BTLog:New("This is a empty string!!!");
local tree2 = TestBehaviorTree2:New(); self:SetStartTask(repeater); repeater:AddChild(selector); selector:AddChild(sequence);
selector:AddChild(tree2); sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log);
end

打印如下:

[Unity插件]Lua行为树(七):行为树嵌套

[Unity插件]Lua行为树(七):行为树嵌套

上面的执行结果是没有问题的,由此可见,将BTBehaviorTree当作节点,然后嵌套在行为树中是可行的。不过通过上面的打印,可以发现嵌套树先被打印,然后总树才被打印,而且嵌套树的层级也是不对的。

对于打印错误,这是节点添加顺序造成的问题,因此不应该在设置节点或者添加节点时进行打印,应该在整棵数构成完成后进行打印。

对于层级不对,可以在添加嵌套树后更新嵌套树下的节点层级。

Enum.lua

 --节点状态
BTTaskStatus = {
Inactive = , --尚未执行
Failure = , --返回失败
Success = , --返回成功
Running = , --执行中
} --节点类型
BTTaskType = {
Root = ,
Action = ,
Composite = ,
Conditional = ,
Decorator = ,
}

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.printTreeStr = ""; function this.RunTree(tree)
this.bTree = tree;
this.OnUpdate();
end function this.OnUpdate()
local status = this.bTree:OnUpdate();
while (status == BTTaskStatus.Running) do
status = this.bTree:OnUpdate();
end
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

BTBehaviorTree.lua

 --[[
树的根节点
--]]
BTBehaviorTree = BTTask:New(); local this = BTBehaviorTree;
this.taskType = BTTaskType.Root; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end --设置起始节点
function this:SetStartTask(task)
task.root = self;
task.parent = self;
task.layer = self.layer + ;
self.startTask = task;
end --更新子节点层级
function this:UpdateLayer()
self:UpdateChildLayer(self.startTask);
end function this:UpdateChildLayer(task)
local taskType = task.taskType;
task.layer = task.root.layer + task.layer - ; if (taskType == BTTaskType.Root) then
self:UpdateChildLayer(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
self:UpdateChildLayer(task.childTasks[i]);
end
else end
end function this:OnUpdate()
if (self.startTask) then
return self.startTask:OnUpdate();
end
end

打印如下:

[Unity插件]Lua行为树(七):行为树嵌套