[Unity插件]Lua行为树(四):条件节点和行为节点
2024-10-16 08:52:56
条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而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
输出如下:
最新文章
- SQLiteHelper
- The median of multi ascending array
- (转) Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么?
- margin小结
- Effective C++ -----条款11: 在operator=中处理“自我赋值”
- DataGridView 行、列的隐藏和删除
- 将博客搬至CSDN(放弃)
- 【T-SQL系列】新的排序函数
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
- Android Activity 注意笔记
- [itint5]两有序数组的交和并
- 在后台代码中引入XAML的方法
- Apache Struts 安全措施绕过漏洞(CVE-2013-4310)
- Java常见面试题总结
- POJ 1459	 Power Network(网络流 最大流 多起点,多汇点)
- 卡特兰数(Catalan)简介
- Java线程:线程状态
- html5 javascript 小型计算器
- go语言调度器源代码情景分析之五:汇编指令
- Linux中jdk的安装配置