[Unity插件]Lua行为树(五):装饰节点Repeater
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行为树(十三):装饰节点完善
之前介绍了组合节点中三大常用的节点:BTSequence.BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求. 接下来可以完善一下装饰节点,增加几种新的节点. 1 ...
- [Unity插件]Lua行为树(七):行为树嵌套
在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...
- [Unity插件]Lua行为树(六):打印树结构
经过前面的文章,已经把行为树中的四种基本类型节点介绍了下.接下来可以整理一下,打印一下整棵行为树.注意点如下: 1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了 ...
- [Unity插件]Lua行为树(二):树结构
参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...
- [Unity插件]Lua行为树(四):条件节点和行为节点
条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...
- [Unity插件]Lua行为树(三):组合节点Sequence
Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...
- [Unity插件]Lua行为树(十一):组合节点Parallel
Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点.当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行. 例如说,给Seq ...
- [Unity插件]Lua行为树(十):通用行为和通用条件节点
在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...
- [Unity插件]Lua行为树(九):条件节点调整
先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...
随机推荐
- hanlp自然语言处理包的基本使用--python
hanlp拥有:中文分词.命名实体识别.摘要关键字.依存句法分析.简繁拼音转换.智能推荐. 这里主要介绍一下hanlp的中文分词.命名实体识别.依存句法分析,这里就不介绍具体的hanlp的安装了,百度 ...
- SparkStreaming整合kafka编程
1.下载spark-streaming-kafka插件包 由于Linux集群环境我使用spark是spark-2.1.1-bin-hadoop2.7,kafka是kafka_2.11-0.8.2.1, ...
- 解决web项目存在多个log4j.properties配置文件,导致日志级别配置不生效问题
java开启log4j的debug模式 -Dlog4j.debug=true tomcat启动debug模式: linux打开catalina.sh导入: export JAVA_OPTS=" ...
- 抓包及分析(wireshark&tcpdump)
1.简介 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Windows平台下使用Wireshark,如果是Linux的话,我直接用tcpdump了,因为我工 ...
- NPC问题及证明
致谢:http://www.docin.com/p-1902790324.html
- chrome flash
chrome://settings/content/flash 在Chrome地址栏中输入:chrome://settings/content/flash,进入Flash设置,勾选允许网站运行flas ...
- php mongo类
看了好多mongo类都不尽人意.最后发现根本不需要自己封装类.php mongo 的扩展自带的方法就已经很方便了 但是习惯性的把数据库连接部分封装起来.最后我就封装了一个单例模式的数据库类 使用单例模 ...
- 机器学习笔记——t分布知识点总结
(原创文章,转载请注明地址:http://www.cnblogs.com/wangkundentisy/p/6539058.html ) 1.t分布式统计分布的一种,同卡方分布(χ2分布).F分布并称 ...
- windows下python2和python3同时安装ipython
1.ipython简介: IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许 ...
- C语言强化——排序
1.完成堆排,对比堆排和qsort在排序1亿数的时间差异 #include<stdio.h> #include<time.h> #include<stdlib.h> ...