[Unity插件]Lua行为树(十三):装饰节点完善
之前介绍了组合节点中三大常用的节点: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行为树(十三):装饰节点完善的更多相关文章
- [Unity插件]Lua行为树(五):装饰节点Repeater
Repeater:重复执行子节点,直到一定次数 特点如下: 1.执行次数可以是无限循环,也可以是固定次数 2.一般来说,子节点的执行返回状态不会影响Repeater节点,但可以设置当子节点返回失败时, ...
- [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. ...
随机推荐
- VS2015和SVN合作
SVN就是版本控制管理工具. 插件这里,还是遇到了一些问题,我知道有插件了,但是忽略了开始的版本问题,按时插件安装过程中没有出现意外,故以为“插件安装成功”,岂不知此“成功”非彼“成功”.由于我用的是 ...
- 安装plsql developer
需求:要连接oracle数据库,不想在本地安装oracle,太大,又占内存,所以用plsql developer.. 在网上看了很多博客,妈呀,被毒的不清,一直提示初始化失败,就是那个oci,dll ...
- 需要序列化的类中没有写serialVersionUID的解决办法
由于没赋值serialVersionUID 只是警告,不是错误,造成先前没留意设定serialVersionUID,网络两端上线运行一段时间也感觉正常.如果再增减修改field,没赋值好serialV ...
- 关键两招就解决Wampserver 打开localhost显示IIS7图片问题
我们在安装集成环境Wampserver之后,有时会遇到一个问题, 打开localhost显示一张IIS7图片,这个问题该如何解决呢,我在网上找了一些说的都很乱,我在这里简单整理了一下解决方法 1 ...
- 【mysql】linux查看mysql配置文件my.conf
mysql --help|grep 'my.cnf' /etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf 这些就是mysq ...
- uoj#119. 【UR #8】决战圆锥曲线
http://uoj.ac/problem/119 可以认为数据基本随机,于是可以直接用线段树维护,对每个询问在线段树上进行剪枝搜索. #include<bits/stdc++.h> ty ...
- MyBatis 的缓存机制
缓存机制可以减轻数据库的压力,原理是在第一查询时,将查询结果缓存起来,之后再查询同样的sql, 不是真的去查询数据库,而是直接返回缓存中的结果. 缓存可以降低数据库的压力,但同时可能无法得到最新的结果 ...
- AWS机器学习初探(2):文本翻译Translate、文本转语音Polly、语音转文本Transcribe
AWS机器学习初探(1):Comprehend - 自然语言处理服务 这几个服务的功能和使用都很直接和简单,因此放在一篇文章中介绍. 1. 文本翻译服务 Translate 1.1 功能介绍 AWS ...
- 学习笔记之Nearest-Neighbour Searching with PostGIS
PostgreSQL: Documentation: 10: 7.8. WITH Queries (Common Table Expressions) https://www.postgresql.o ...
- python的导包问题
有事会遇到在python代码中导入包错误问题,本文简单对python包的引入做简单介绍 简单说,我认为python导包一共有3种情况,分别是: 要导的包与当前文件在同一层要导的包在当前文件的底层(就是 ...