条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了。

它们的继承关系如下:

Conditional->Task

Action->Task

代码如下:

BTAction.lua

 BTAction = BTTask:New();

 local this = BTAction;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end

BTConditional.lua

 BTConditional = BTTask:New();

 local this = BTConditional;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end

BTLog.lua

 --[[
参考BehaviorDesigner-Action-Log
--]]
BTLog = BTAction:New(); local this = BTLog; function this:New(text)
local o = {};
setmetatable(o, self);
self.__index = self;
o.text = text;
return o;
end function this:OnUpdate()
print(self.text);
return BTTaskStatus.Success;
end

BTIsNullOrEmpty.lua

 --[[
参考BehaviorDesigner-Conditional-IsNullOrEmpty
--]]
BTIsNullOrEmpty = BTConditional:New(); local this = BTIsNullOrEmpty; function this:New(text)
local o = {};
setmetatable(o, self);
self.__index = self;
o.text = text;
return o;
end function this:OnUpdate()
if (not self.text or self.text == "") then
return BTTaskStatus.Success;
else
return BTTaskStatus.Failure;
end
end

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;

 function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
this:Init();
return o;
end function this:Init()
local sequence = BTSequence:New();
local isNullOrEmpty = BTIsNullOrEmpty:New("");
local log = BTLog:New("This is a empty string");
sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log);
this:PushTask(sequence);
end

输出如下:

最新文章

  1. SQLiteHelper
  2. The median of multi ascending array
  3. (转) Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么?
  4. margin小结
  5. Effective C++ -----条款11: 在operator=中处理“自我赋值”
  6. DataGridView 行、列的隐藏和删除
  7. 将博客搬至CSDN(放弃)
  8. 【T-SQL系列】新的排序函数
  9. java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
  10. Android Activity 注意笔记
  11. [itint5]两有序数组的交和并
  12. 在后台代码中引入XAML的方法
  13. Apache Struts 安全措施绕过漏洞(CVE-2013-4310)
  14. Java常见面试题总结
  15. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)
  16. 卡特兰数(Catalan)简介
  17. Java线程:线程状态
  18. html5 javascript 小型计算器
  19. go语言调度器源代码情景分析之五:汇编指令
  20. Linux中jdk的安装配置

热门文章

  1. json server的简单使用(附:使用nodejs快速搭建本地服务器)
  2. Java使用 SFTP实现文件上传下载
  3. STL基础--基本介绍
  4. ApplicationDomain
  5. Ubuntu 14.10 下SSH配置
  6. 【推荐】asp.net 页面的生命周期
  7. 量化交易(Quantitative Trading)
  8. Linux下统计局域网流量
  9. go语言学习--指针数组和数组指针
  10. 保存chrome主题背景的图片