之前介绍了组合节点中三大常用的节点:BTSequence、BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求。

接下来可以完善一下装饰节点,增加几种新的节点。

1.BTInverter

 --[[
结果取反:
1.子节点返回Running,则节点返回Running
2.子节点返回Success,则节点返回Failure
3.子节点返回Failure,则节点返回Success
--]]
BTInverter = BTDecorator:New(); local this = BTInverter;
this.name = "BTInverter"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.childTasks = {};
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 self.executionStatus = self.currentChildTask:OnUpdate(); if (self.executionStatus == BTTaskStatus.Running) then
return BTTaskStatus.Running;
elseif (self.executionStatus == BTTaskStatus.Success) then
return BTTaskStatus.Failure;
else
return BTTaskStatus.Success;
end
end function this:Reset()
self.executionStatus = BTTaskStatus.Inactive;
BTParentTask.Reset(self);
end

测试:

 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 sequence = BTSequence:New();
local inverter = BTInverter:New();
local isNullOrEmpty = BTIsNullOrEmpty:New("");
local log = BTLog:New("This is a tree!!!"); self:SetStartTask(sequence); inverter:AddChild(isNullOrEmpty); sequence:AddChild(inverter);
sequence:AddChild(log);
end

输出:

2.BTReturnFailure

 --[[
结果返回失败:
1.子节点返回Running,则节点返回Running
2.其余情况,则节点返回Failure
--]]
BTReturnFailure = BTDecorator:New(); local this = BTReturnFailure;
this.name = "BTReturnFailure"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.childTasks = {};
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 self.executionStatus = self.currentChildTask:OnUpdate(); if (self.executionStatus == BTTaskStatus.Running) then
return BTTaskStatus.Running;
else
return BTTaskStatus.Failure;
end
end function this:Reset()
self.executionStatus = BTTaskStatus.Inactive;
BTParentTask.Reset(self);
end

测试:

 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 selector = BTSelector:New();
local returnFailure = BTReturnFailure:New();
local isNullOrEmpty = BTIsNullOrEmpty:New();
local log = BTLog:New("This is a tree!!!"); self:SetStartTask(selector); returnFailure:AddChild(isNullOrEmpty); selector:AddChild(returnFailure);
selector:AddChild(log);
end

输出:

3.BTUntilFailure

 --[[
结果返回失败:
1.子节点返回Failure,则节点返回Failure
2.其余情况,则节点返回Running
--]]
BTUntilFailure = BTDecorator:New(); local this = BTUntilFailure;
this.name = "BTUntilFailure"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o.childTasks = {};
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 self.executionStatus = self.currentChildTask:OnUpdate(); if (self.executionStatus ~= BTTaskStatus.Failure) then
return BTTaskStatus.Running;
else
return BTTaskStatus.Failure;
end
end function this:Reset()
self.executionStatus = BTTaskStatus.Inactive;
BTParentTask.Reset(self);
end

测试:

 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 selector = BTSelector:New();
local untilFailure = BTUntilFailure:New();
local action = self:GetBTActionUniversal();
local log = BTLog:New("This is a tree!!!"); self:SetStartTask(selector); untilFailure:AddChild(action); selector:AddChild(untilFailure);
selector:AddChild(log);
end function this:GetBTActionUniversal()
local count = ;
local a = function ()
if (count == ) then
count = count + ;
print("");
return BTTaskStatus.Success;
elseif (count == ) then
count = count + ;
print("");
return BTTaskStatus.Running;
else
print("");
return BTTaskStatus.Failure;
end
end
local universal = BTActionUniversal:New(nil, a);
return universal;
end

输出:

最后给出这个系列的源码:

https://pan.baidu.com/s/1QwjozJ3dEpqNRL04oLvfHw

[Unity插件]Lua行为树(十三):装饰节点完善的更多相关文章

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

    Repeater:重复执行子节点,直到一定次数 特点如下: 1.执行次数可以是无限循环,也可以是固定次数 2.一般来说,子节点的执行返回状态不会影响Repeater节点,但可以设置当子节点返回失败时, ...

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

    在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...

  3. [Unity插件]Lua行为树(六):打印树结构

    经过前面的文章,已经把行为树中的四种基本类型节点介绍了下.接下来可以整理一下,打印一下整棵行为树.注意点如下: 1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了 ...

  4. [Unity插件]Lua行为树(二):树结构

    参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...

  5. [Unity插件]Lua行为树(四):条件节点和行为节点

    条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...

  6. [Unity插件]Lua行为树(三):组合节点Sequence

    Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...

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

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

  8. [Unity插件]Lua行为树(十):通用行为和通用条件节点

    在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...

  9. [Unity插件]Lua行为树(九):条件节点调整

    先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...

随机推荐

  1. Windows Azure Storage (24) 启用Azure Blob日志

    <Windows Azure Platform 系列文章目录> 之前有一个业务需求,客户想知道Azure Storage是否有日志功能,可以检查某个Azure Blob文件在某个时间点被删 ...

  2. linux开机出现一下错误Give root password for maintenance (or type Control-D to continue):

    由于错误的编辑/etc/fstab文件 而引起的不能正常进入系统.假如你将某一个分区或者磁盘最后一个参数设置为1或2时,系统默认会在开机过程中检查这个磁盘的扇区.假如系统检查不到这个磁盘,或者这个磁盘 ...

  3. sklearn.cross_validation 0.18版本废弃警告及解决方法

    转载:cheneyshark 机器环境: scikit-learn==0.19.1 Python 2.7.13 train_test_split基本用法 在机器学习中,我们通常将原始数据按照比例分割为 ...

  4. bzoj5010: [Fjoi2017]矩阵填数

    Description 给定一个 h*w 的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w.在这个矩阵中你需要在每 个格子中填入 1..m 中的某个数.给这个矩阵填数的时候有一 ...

  5. 三星GT-N8010刷机教程

    本刷机教程只针对三星GT-N8010机器(以下简称GT-N8010),以下操作本人已在GT-N8010机器上亲测,且都成功,其它机器没有测试不能保证成功. 刷机有风险,请谨慎使用!请先备份资料和信息. ...

  6. 修改最后一次 已commit 的备注

    输入命令 git commit --amend 会展示出最后一次提交的 备注信息 按 i 进行编辑 按esc 退出编辑 再按 shift +: (注意是英文的冒号),切换到命令行 wq 保存 即可 参 ...

  7. MyBatis 手动映射结果集

    MyBatis可以自动将查询结果封装到bean中,前提条件是bean的属性名和查询的结果列名相同,就会一次对应存储. 如果查询结果的列名和bean的属性名不一致,则需要手动映射结果集 <!-- ...

  8. docker entrypoint入口文件详解

    docker entrypoint入口文件详解 pasting Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较 [k8s]args指令案例-彻底理解docke ...

  9. [UE4]非常实用的插值Lerp

    Alpha的数值范围是0到1. if(Alpha==0) ReturnValue=A if(Alpha==1) ReturnValue=B 如果Alpha在0到1之间,Alpha值越接近0则ReVal ...

  10. Windows配置多个git用户

    Window配置多个Git账户,SSH连接GitHub.GitLab 最新版本GIt配置对应多个Git仓库(不需要添加多个用户名和邮箱): 在本地git上添加一个用户名和邮箱,生成一对公钥和私钥,把公 ...