[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. ...
随机推荐
- WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS
先放结论: ASIO:硬件支持+对应驱动程序 DS:兼容性最好,一般也是默认的. WASAPI:是Vista之后的,较佳选择输出方式. 再来详细看: ASIO.WDM都是指音频通道,就是音频数据走的路 ...
- RTB业务知识之2-Open-RTB全景
一.前言 openrtb是一套开源的竞价广告系统,来自IAB的贡献,非常好.有非常多的值得借鉴的地方,最近基于其所提供sdk api接口文档介绍,整理了相关的资料.主要包括其生态图体系.业务流程和主要 ...
- DS二叉树--二叉树之父子结点
题目描述 给定一颗二叉树的逻辑结构如下图,(先序遍历的结果,空树用字符‘0’表示,例如AB0C00D00),建立该二叉树的二叉链式存储结构. 编写程序输出该树的所有叶子结点和它们的父亲结点 输入 第一 ...
- git clone 指定分支 拉代码
1.git clone 不指定分支 git clone http://10.1.1.11/service/tmall-service.git 2.git clone 指定分支 git clone -b ...
- vagrant 本地添加box 支持带版本号
众所周知,vagrant添加box的时候要从外网下载,那速度...(说多了都是泪),所以只好用下载工具下载到本地之后再添加. 一般处理方案 vagrant box add boxName ./down ...
- vue中滚动事件绑定的函数无法调用问题
问题描述: 一个包含下拉加载的页面,刷新当前页然后滚动页面,能够正常触发滚动事件并调用回调函数,但是如果是进了某一个页面然后再进的该页面,滚动事件能够触发, 但是回调函数在滚动的时候只能被调用一次. ...
- MyBatis的入门案例
1.MyBatis的结构 2.MyBatis入门案例 a.创建java项目,并在其中导入相关开发包 b.导入约束文件 http://mybatis.org/dtd/mybatis-3-config.d ...
- Java NIO系列教程(三) Channel之Socket通道
目录: <Java NIO系列教程(二) Channel> <Java NIO系列教程(三) Channel之Socket通道> 在<Java NIO系列教程(二) Ch ...
- [UE4]小地图UI放在哪里创建合适?
在常见的FPS游戏中,玩家死亡以后,还是可以进行聊天和漫游的,因此聊天UI和小地图都应该放在PlayerState中创建
- [UE4]透明按钮
Background Color的透明度设置为0,就能让按钮背景完全透明,但是按钮里面的子控件并不会跟着透明