在行为树中,需要扩展的主要是行为节点和条件节点。一般来说,每当要创建一个节点时,就要新建一个节点文件。而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点,以传入方法的方式来避免新建文件。

BTActionUniversal.lua

 --[[
通用Action节点
--]]
BTActionUniversal = BTAction:New(); local this = BTActionUniversal;
this.name = "BTActionUniversal"; function this:New(enterFunc, executeFunc, exitFunc)
local o = {};
setmetatable(o, self);
self.__index = self;
o.enterFunc = enterFunc;
o.executeFunc = executeFunc;
o.exitFunc = exitFunc;
return o;
end function this:Enter()
if (self.enterFunc) then
self.enterFunc();
end
end function this:Execute()
if (self.executeFunc) then
return self.executeFunc();
else
return BTTaskStatus.Failure;
end
end function this:Exit()
if (self.exitFunc) then
self.exitFunc();
end
end

BTConditionalUniversal.lua

 --[[
通用Conditional节点
--]]
BTConditionalUniversal = BTConditional:New(); local this = BTConditionalUniversal;
this.name = "BTConditionalUniversal"; function this:New(checkFunc)
local o = {};
setmetatable(o, self);
self.__index = self;
o.checkFunc = checkFunc;
return o;
end function this:Check()
return self.checkFunc();
end

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local sequence = BTSequence:New();
local conditional = self:GetBTConditionalUniversal();
local action = self:GetBTActionUniversal();
local log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(sequence); sequence:AddChild(conditional);
sequence:AddChild(action);
sequence:AddChild(log);
end function this:GetBTConditionalUniversal()
local a = function ()
return ( > );
end
local universal = BTConditionalUniversal:New(a);
return universal;
end function this:GetBTActionUniversal()
local count = ;
local a = function () print(""); end
local b = function ()
if (count < ) then
count = count + ;
print("");
return BTTaskStatus.Running;
else
return BTTaskStatus.Success;
end
end
local c = function () print(""); end
local universal = BTActionUniversal:New(a, b, c);
return universal;
end

打印如下:

最新文章

  1. 测试 MathJax 排版功效
  2. 回收ImageView占用的图像内存
  3. centos添加硬盘
  4. sublime text 3 技巧
  5. nodejs学习之表单提交(1)
  6. 问卷调查&mdash;&mdash;答卷
  7. w-WAITING---
  8. Struts2+Uploadify文件上传使用详解
  9. 安装完 MySQL 后必须调整的 10 项配置(转)
  10. 关于Spring运用过程中jar包报错问题
  11. 纪录一个table元素里面的tr th td
  12. c++ 重载,覆盖,重定义 2
  13. CodeForces 702B Powers of Two
  14. git版本控制工具基本用法讲解(转)
  15. GCC编译过程与动态链接库和静态链接库
  16. DateTables的服务器分页
  17. 快捷键设置 keyiing.json
  18. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(5) SimpleDateFormat
  19. SharePoint 2013 Step by Step—— How to Upload Multiple Documents in Document Library
  20. ASP.NET Web配置指南

热门文章

  1. (转)oms系统安装php的redis扩展
  2. 几种always块的形态
  3. 坑人的 Javascript 模块化编程 sea.js
  4. 前端之js-本地存储-localStorage &amp;&amp; IndexedDB
  5. pyqt环境安装
  6. 【maven】之打包不带版本号的问题
  7. ribbon的注解使用报错--No instances available for [IP]
  8. 使用R语言-为矩阵(表格)的行列命名
  9. Java-Runoob-高级教程-实例-方法:11. Java 实例 – enum 和 switch 语句使用
  10. python urlib2报错gaierror: [Errno 11004] getaddrinfo failed