[Unity插件]Lua行为树(十一):组合节点Parallel

Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点。当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行。

例如说,给Sequence节点插入一个不断返回Running的行为节点,那么就会造成后面的子节点无法执行,而对于Parallel来说,是不会存在这种阻塞情况的。

Parallel.cs

 namespace BehaviorDesigner.Runtime.Tasks
{
[TaskDescription("Similar to the sequence task, the parallel task will run each child task until a child task returns failure. " +
"The difference is that the parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
"Like the sequence class, the parallel task will return success once all of its children tasks have return success. " +
"If one tasks returns failure the parallel task will end all of the child tasks and return failure.")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=27")]
[TaskIcon("{SkinColor}ParallelIcon.png")]
public class Parallel : Composite
{
// The index of the child that is currently running or is about to run.
private int currentChildIndex;
// The task status of every child task.
private TaskStatus[] executionStatus; public override void OnAwake()
{
// Create a new task status array that will hold the execution status of all of the children tasks.
executionStatus = new TaskStatus[children.Count];
} public override void OnChildStarted(int childIndex)
{
// One of the children has started to run. Increment the child index and set the current task status of that child to running.
currentChildIndex++;
executionStatus[childIndex] = TaskStatus.Running;
} public override bool CanRunParallelChildren()
{
// This task can run parallel children.
return true;
} public override int CurrentChildIndex()
{
return currentChildIndex;
} public override bool CanExecute()
{
// We can continue executing if we have more children that haven't been started yet.
return currentChildIndex < children.Count;
} public override void OnChildExecuted(int childIndex, TaskStatus childStatus)
{
// One of the children has finished running. Set the task status.
executionStatus[childIndex] = childStatus;
} public override TaskStatus OverrideStatus(TaskStatus status)
{
// Assume all of the children have finished executing. Loop through the execution status of every child and check to see if any tasks are currently running
// or failed. If a task is still running then all of the children are not done executing and the parallel task should continue to return a task status of running.
// If a task failed then return failure. The Behavior Manager will stop all of the children tasks. If no child task is running or has failed then the parallel
// task succeeded and it will return success.
bool childrenComplete = true;
for (int i = ; i < executionStatus.Length; ++i) {
if (executionStatus[i] == TaskStatus.Running) {
childrenComplete = false;
} else if (executionStatus[i] == TaskStatus.Failure) {
return TaskStatus.Failure;
}
}
return (childrenComplete ? TaskStatus.Success : TaskStatus.Running);
} public override void OnConditionalAbort(int childIndex)
{
// Start from the beginning on an abort
currentChildIndex = ;
for (int i = ; i < executionStatus.Length; ++i) {
executionStatus[i] = TaskStatus.Inactive;
}
} public override void OnEnd()
{
// Reset the execution status and the child index back to their starting values.
for (int i = ; i < executionStatus.Length; ++i) {
executionStatus[i] = TaskStatus.Inactive;
}
currentChildIndex = ;
}
}
}

BTParallel.lua

 BTParallel = BTComposite:New();

 local this = BTParallel;
this.name = "BTParallel"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.childTasks = {};
o.executionStatus = {};
return o;
end function this:OnUpdate()
if (not self:HasChild()) then
return BTTaskStatus.Failure;
end for i=,#self.childTasks do
local childTask = self.childTasks[i];
if (not self.executionStatus[i]) then --第一次执行
self.executionStatus[i] = childTask:OnUpdate();
if (self.executionStatus[i] == BTTaskStatus.Failure) then
return BTTaskStatus.Failure;
end
elseif (self.executionStatus[i] == BTTaskStatus.Running) then --第二次以及以后执行
self.executionStatus[i] = childTask:OnUpdate();
if (self.executionStatus[i] == BTTaskStatus.Failure) then
return BTTaskStatus.Failure;
end
end
end local childrenComplete = true;
for i=,#self.executionStatus do
if (self.executionStatus[i] == BTTaskStatus.Running) then
childrenComplete = false;
break;
end
end
if (childrenComplete) then
return BTTaskStatus.Success;
else
return BTTaskStatus.Running;
end
end

测试如下:

1.BTSequence和BTParallel的对比

BTSequence:

 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 sequence = BTSequence:New();
local action = self:GetBTActionUniversal();
local log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(sequence); sequence:AddChild(action);
sequence:AddChild(log);
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

[Unity插件]Lua行为树(十一):组合节点Parallel

BTParallel:

 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 log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(parallel); parallel:AddChild(action);
parallel:AddChild(log);
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

[Unity插件]Lua行为树(十一):组合节点Parallel

2.BTParallel中返回失败中断执行的情况

 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.Failure;
end);
return universal;
end

[Unity插件]Lua行为树(十一):组合节点Parallel

上一篇:C#获取当前主机硬件信息


下一篇:leetcode 9 Palindrome Number 回文数