之前介绍了组合节点中三大常用的节点: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. mysql程序之mysqld_safe详解

    mysqld_safe命令 mysqld_safe是在Unix上启动mysqld服务器的推荐方法.mysqld_safe增加了一些安全特性,例如在发生错误时重新启动服务器,并将运行时信息记录到错误日志 ...

  2. NVMe标准规范

    NVMe NVM Express(NVMe),或称非易失性内存主机控制器接口规范(Non-Volatile Memory express),,是一个逻辑设备接口规范.他是与AHCI类似的.基于设备逻辑 ...

  3. pandas初识

    pandas初识 1.生成DataFrame型的数据 import pandas as pd import numpy as np dates = pd.date_range('20130101',p ...

  4. Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014

    传统感念:每次修改完nginx配置文件,要重启nginx 动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息. Nginx: 反向代理和负载均衡 Consul:是用go编写 ...

  5. 车载文档记录(ROM)

    一,缩写词定义 1,ECU和ECM ECU: Engine Control Unit 发动机控制单元:从用途上讲则是汽车专用微机控制器.ECM: Engine Control Module 发动机控制 ...

  6. Action<T> Delegate

    来源:https://docs.microsoft.com/zh-cn/dotnet/api/system.action-1?view=netframework-4.7.2 Action<T&g ...

  7. 瑞萨S5D9实现UART环形缓冲

    队列的常见两种形式,普通队列和环形队列: 普通队列: 环形队列: 当有大量数据的时候,我们不能存储所有的数据,那么计算机处理数据的时候,只能先处理先来的,那么处理完后呢,就会把数据释放掉,再处理下一个 ...

  8. spring AOP的学习

    1.Spring常用的概念 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. Pointcut(切入点): ...

  9. jquery插件的几种写法

    /** * Created by peng on 2016/12/8. */jQuery.extend({ min: function(a, b) { return a < b ? a : b; ...

  10. error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ActionButton'.

    引用appcompat 类库提示 error: Error retrieving parent for item: No resource found that  matches the given ...