zoukankan      html  css  js  c++  java
  • [Unity插件]Lua行为树(九):条件节点调整

    先看一下之前的条件节点是怎么设计的:

    BTConditional.lua

     1 BTConditional = BTTask:New();
     2 
     3 local this = BTConditional;
     4 this.taskType = BTTaskType.Conditional;
     5 
     6 function this:New()
     7     local o = {};
     8     setmetatable(o, self);
     9     self.__index = self;
    10     return o;
    11 end

    BTIsNullOrEmpty.lua

     1 --[[
     2 参考BehaviorDesigner-Conditional-IsNullOrEmpty
     3 --]]
     4 BTIsNullOrEmpty = BTConditional:New();
     5 
     6 local this = BTIsNullOrEmpty;
     7 this.name = "BTIsNullOrEmpty";
     8 
     9 function this:New(text)
    10     local o = {};
    11     setmetatable(o, self);
    12     self.__index = self;
    13     o.text = text;
    14     return o;
    15 end
    16 
    17 function this:OnUpdate()
    18     if (not self.text or self.text == "") then
    19         return BTTaskStatus.Success;
    20     else
    21         return BTTaskStatus.Failure;
    22     end
    23 end

    由上可见,条件节点就是判断条件然后返回成功或者失败,而且也只会有这两种状态,这和if的逻辑是一样的,因此可以改进一下。

    BTConditional.lua

     1 BTConditional = BTTask:New();
     2 
     3 local this = BTConditional;
     4 this.taskType = BTTaskType.Conditional;
     5 
     6 function this:New()
     7     local o = {};
     8     setmetatable(o, self);
     9     self.__index = self;
    10     return o;
    11 end
    12 
    13 function this:OnUpdate()
    14     if (self:Check()) then
    15         return BTTaskStatus.Success;
    16     else
    17         return BTTaskStatus.Failure;
    18     end
    19 end
    20 
    21 function this:Check()
    22     return false;
    23 end

    BTIsNullOrEmpty.lua

     1 --[[
     2 参考BehaviorDesigner-Conditional-IsNullOrEmpty
     3 --]]
     4 BTIsNullOrEmpty = BTConditional:New();
     5 
     6 local this = BTIsNullOrEmpty;
     7 this.name = "BTIsNullOrEmpty";
     8 
     9 function this:New(text)
    10     local o = {};
    11     setmetatable(o, self);
    12     self.__index = self;
    13     o.text = text;
    14     return o;
    15 end
    16 
    17 function this:Check()
    18     if (not self.text or self.text == "") then
    19         return true;
    20     else
    21         return false;
    22     end
    23 end

    TestBehaviorTree.lua

     1 TestBehaviorTree = BTBehaviorTree:New();
     2 
     3 local this = TestBehaviorTree;
     4 this.name = "TestBehaviorTree";
     5 
     6 function this:New()
     7     local o = {};
     8     setmetatable(o, self);
     9     self.__index = self;
    10     o:Init();
    11     return o;
    12 end
    13 
    14 function this:Init()
    15     local sequence = BTSequence:New();
    16     local isNullOrEmpty = BTIsNullOrEmpty:New();
    17     local log = BTLog:New("This is log!!!");
    18     log.name = "log";
    19 
    20     self:SetStartTask(sequence);
    21 
    22     sequence:AddChild(isNullOrEmpty);
    23     sequence:AddChild(log);
    24 end

    打印如下:

  • 相关阅读:
    Linux中使用expect脚本实现远程机器自动登录_linux shell
    Linux: ssh命令 远程登录
    总结:PgSql备份pg_dump与还原pg_restore
    停电后,在UPS电源下服务器自动关机脚本
    总结:修改相关postgres用户密码
    问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
    delphi 播放mp3
    delphi 获取文件路径函数
    Delphi XE2 使用dbExpress连接MySQL数据库
    kbmmemsql 查询
  • 原文地址:https://www.cnblogs.com/lyh916/p/9656327.html
Copyright © 2011-2022 走看看