zoukankan      html  css  js  c++  java
  • [Unity插件]Lua行为树(八):行为节点扩展

    先看一下之前的行为节点是怎么设计的:

    BTAction.lua

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

    BTLog.lua

     1 --[[
     2 参考BehaviorDesigner-Action-Log
     3 --]]
     4 BTLog = BTAction:New();
     5 
     6 local this = BTLog;
     7 this.name = "BTLog";
     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     print(self.text);
    19     return BTTaskStatus.Success;
    20 end

    由上可见,行为节点的具体逻辑都是放在OnUpdate中的,那么问题来了,如果想在OnUpdate前执行一段逻辑,OnUpdate后也执行一段逻辑,类似于状态机那样,那么就需要对行为节点进行扩展。

    BTAction.lua

     1 BTAction = BTTask:New();
     2 
     3 local this = BTAction;
     4 this.taskType = BTTaskType.Action;
     5 this.isEnter = false;
     6 
     7 function this:New()
     8     local o = {};
     9     setmetatable(o, self);
    10     self.__index = self;
    11     return o;
    12 end
    13 
    14 function this:OnUpdate()
    15     local status;
    16     if (not self.isEnter) then
    17         self.isEnter = true;
    18         self:Enter();
    19     end
    20     if (self.isEnter) then
    21         status = self:Execute();
    22         if (status ~= BTTaskStatus.Running) then
    23             self:Exit();
    24             self.isEnter = false;
    25         end
    26     end
    27     return status;
    28 end
    29 
    30 function this:Enter()
    31     print("BTAction:Enter");
    32 end
    33 
    34 function this:Execute()
    35     print("BTAction:Execute");
    36     return BTTaskStatus.Success;
    37 end
    38 
    39 function this:Exit()
    40     print("BTAction:Exit");
    41 end

    BTLog.lua

     1 --[[
     2 参考BehaviorDesigner-Action-Log
     3 --]]
     4 BTLog = BTAction:New();
     5 
     6 local this = BTLog;
     7 this.name = "BTLog";
     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:Enter()
    18     print(self.name .. ":Enter");
    19 end
    20 
    21 function this:Execute()
    22     print(self.text);
    23     return BTTaskStatus.Success;
    24 end
    25 
    26 function this:Exit()
    27     print(self.name .. ":Exit");
    28 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 log = BTLog:New("This is log!!!");
    17     local log2 = BTLog:New("This is log2!!!");
    18     log.name = "log";
    19     log2.name = "log2";
    20     
    21     self:SetStartTask(sequence);
    22 
    23     sequence:AddChild(log);
    24     sequence:AddChild(log2);
    25 end

    打印如下:

  • 相关阅读:
    HDU 1258 Sum It Up(Dfs)
    HDU 1501 Zipper(Dfs记忆化搜索)
    HDU 1075 What Are You Talking About (字典树)
    HDU 1251 统计难题(字典树)
    HDU 1518 Square(Dfs)
    %与mod的区别
    有向无向欧拉回路通路的判断
    HDU 1104 Remainder(BFS打印路径+数论)(%与mod的区别)
    写给还在怀念骑士(3.0 2.0 Online 私服)的刀狼
    HUD 1312 Red and Black(用深搜写的)
  • 原文地址:https://www.cnblogs.com/lyh916/p/9655819.html
Copyright © 2011-2022 走看看