[Unity插件]Lua行为树(五):装饰节点Repeater

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

Repeater:重复执行子节点,直到一定次数

特点如下:

1.执行次数可以是无限循环,也可以是固定次数

2.一般来说,子节点的执行返回状态不会影响Repeater节点,但可以设置当子节点返回失败时,结束执行Repeater节点

Repeater的继承关系如下:

Repeater->Decorator->ParentTask->Task

因为继承装饰节点,所以其子节点只能有一个

Repeater.cs

 namespace BehaviorDesigner.Runtime.Tasks
{
[TaskDescription(@"The repeater task will repeat execution of its child task until the child task has been run a specified number of times. " +
"It has the option of continuing to execute the child task even if the child task returns a failure.")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=37")]
[TaskIcon("{SkinColor}RepeaterIcon.png")]
public class Repeater : Decorator
{
[Tooltip("The number of times to repeat the execution of its child task")]
public SharedInt count = ;
[Tooltip("Allows the repeater to repeat forever")]
public SharedBool repeatForever;
[Tooltip("Should the task return if the child task returns a failure")]
public SharedBool endOnFailure; // The number of times the child task has been run.
private int executionCount = ;
// The status of the child after it has finished running.
private TaskStatus executionStatus = TaskStatus.Inactive; public override bool CanExecute()
{
// Continue executing until we've reached the count or the child task returned failure and we should stop on a failure.
return (repeatForever.Value || executionCount < count.Value) && (!endOnFailure.Value || (endOnFailure.Value && executionStatus != TaskStatus.Failure));
} public override void OnChildExecuted(TaskStatus childStatus)
{
// The child task has finished execution. Increase the execution count and update the execution status.
executionCount++;
executionStatus = childStatus;
} public override void OnEnd()
{
// Reset the variables back to their starting values.
executionCount = ;
executionStatus = TaskStatus.Inactive;
} public override void OnReset()
{
// Reset the public properties back to their original values.
count = ;
endOnFailure = true;
}
}
}

BTDecorator.lua

 BTDecorator = BTParentTask:New();

 local this = BTDecorator;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:GetChild()
if (self:GetChildCount() ~= ) then
return nil;
end
return self.childTasks[];
end

BTRepeater.lua

 BTRepeater = BTDecorator:New();

 local this = BTRepeater;

 function this:New(count, endOnFailure)
local o = {};
setmetatable(o, self);
self.__index = self;
o.executionStatus = BTTaskStatus.Inactive;
o.count = count or -; --执行次数,-1为循环执行
o.endOnFailure = endOnFailure or false; --子节点返回Failure时,是否结束
o.executionCount = ; --当前执行次数
return o;
end function this:OnUpdate()
if (not self.currentChildTask) then
self.currentChildTask = self:GetChild();
if (not self.currentChildTask) then
return BTTaskStatus.Failure;
end
end local canExecute = false;
if (((self.count == -) or (self.executionCount < self.count)) and
((not self.endOnFailure) or (self.endOnFailure and self.executionStatus ~= BTTaskStatus.Failure))) then
canExecute = true;
end if (canExecute) then
self.executionStatus = self.currentChildTask:OnUpdate();
self.executionCount = self.executionCount + ;
end if (self.endOnFailure and self.executionStatus == BTTaskStatus.Failure) then
self:Reset();
return BTTaskStatus.Success;
elseif (self.executionCount == self.count) then
self:Reset();
return BTTaskStatus.Success;
else
return BTTaskStatus.Running;
end
end function this:Reset()
self.executionStatus = BTTaskStatus.Inactive;
self.executionCount = ;
BTParentTask.Reset(self);
end

测试:

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
this:Init();
return o;
end function this:Init()
--1.一定的执行次数
local repeater = BTRepeater:New();
local log = BTLog:New("This is a BTRepeater test");
repeater:AddChild(log); --2.循环执行
--[[local repeater = BTRepeater:New();
local log = BTLog:New("This is a BTRepeater test");
repeater:AddChild(log);--]] --3.endOnFailure false
--[[local repeater = BTRepeater:New(3);
local sequence = BTSequence:New();
local log = BTLog:New("First Child");
local isNullOrEmpty = BTIsNullOrEmpty:New("123");
local log2 = BTLog:New("This is a empty string");
sequence:AddChild(log);
sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log2);
repeater:AddChild(sequence);--]] --4.endOnFailure true
--[[local repeater = BTRepeater:New(3, true);
local sequence = BTSequence:New();
local log = BTLog:New("First Child");
local isNullOrEmpty = BTIsNullOrEmpty:New("123");
local log2 = BTLog:New("This is a empty string");
sequence:AddChild(log);
sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log2);
repeater:AddChild(sequence);--]] this:PushTask(repeater);
end

第一个输出:

[Unity插件]Lua行为树(五):装饰节点Repeater

第二个输出(循环执行):

[Unity插件]Lua行为树(五):装饰节点Repeater

第三个输出:

[Unity插件]Lua行为树(五):装饰节点Repeater

第四个输出:

[Unity插件]Lua行为树(五):装饰节点Repeater