[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. ...
随机推荐
- C 500uS状态机架构
main int main(void) { InitSys(); SoftwareInit(); ) { if(P500usReq) { P500usReq = ; P500us(); } Modbu ...
- Java自定义数据验证注解Annotation
本文转载自:https://www.jianshu.com/p/616924cd07e6 Java注解Annotation用起来很方便,也越来越流行,由于其简单.简练且易于使用等特点,很多开发工具都提 ...
- windows编程之窗口抖动
仅仅让黑窗口抖动以供小白娱乐 #include<stdio.h> #include<windows.h> int main() { RECT rect;//RECT定义了一个矩 ...
- springmvc学习(五)
这次主要是记录一下 springmvc 关于异常处理 和 拦截的回顾 关于springmvc 异常处理:springmvc 提供了 HandlerExceptionResolver 异常处理解析接 ...
- eclipse安装、汉化、搭建安卓开发环境
1.eclipse与jdk的位数(32bit or 64bit )要对应,否则会提示Failed to load JNI shared library.提示这一种错误据说还有另外一种原因就是Path路 ...
- gmake缺失错误
原文:http://blog.csdn.net/syh_486_007/article/details/53862831 编译nachos程序的时候发现了这样一个错误gmake: command no ...
- SEO优化之“不要轻易使用泛解析”
原文地址:http://www.chinaz.com/web/2007/0505/8077.shtml 半夜三更的突然想起这个老想提出或者大家都知道的问题! 先续在这里,之后给予全面补充! 什么是泛解 ...
- IRQL Ring0实现
一,IRQL的定义Interrupt ReQuest Level DDK对IRQL的定义是:中断请求级(IRQL)标示中断的优先级.处理器在一个IRQL上执行线程代码,IRQL是帮助决定线程如 ...
- Oracle ORA 6510
解决方法待补充 咨询得到的解析是: plsql写的存储过程在导出后需要重新编译才能执行:需要处理好这方面的关系
- Java JDK版本切换--绝逼好使
转载: https://www.cnblogs.com/ll409546297/p/6593173.html 1.问题:同时装两个版本的jdk时出现的问题(本次是1.7和1.8的版本),因为eclip ...