先看一下之前的条件节点是怎么设计的:
BTConditional.lua
BTConditional = BTTask:New(); local this = BTConditional;
this.taskType = BTTaskType.Conditional; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end
BTIsNullOrEmpty.lua
--[[
参考BehaviorDesigner-Conditional-IsNullOrEmpty
--]]
BTIsNullOrEmpty = BTConditional:New(); local this = BTIsNullOrEmpty;
this.name = "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
由上可见,条件节点就是判断条件然后返回成功或者失败,而且也只会有这两种状态,这和if的逻辑是一样的,因此可以改进一下。
BTConditional.lua
BTConditional = BTTask:New(); local this = BTConditional;
this.taskType = BTTaskType.Conditional; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
return o;
end function this:OnUpdate()
if (self:Check()) then
return BTTaskStatus.Success;
else
return BTTaskStatus.Failure;
end
end function this:Check()
return false;
end
BTIsNullOrEmpty.lua
--[[
参考BehaviorDesigner-Conditional-IsNullOrEmpty
--]]
BTIsNullOrEmpty = BTConditional:New(); local this = BTIsNullOrEmpty;
this.name = "BTIsNullOrEmpty"; function this:New(text)
local o = {};
setmetatable(o, self);
self.__index = self;
o.text = text;
return o;
end function this:Check()
if (not self.text or self.text == "") then
return true;
else
return false;
end
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 isNullOrEmpty = BTIsNullOrEmpty:New();
local log = BTLog:New("This is log!!!");
log.name = "log"; self:SetStartTask(sequence); sequence:AddChild(isNullOrEmpty);
sequence:AddChild(log);
end
打印如下: